UserWarning
PythonWARNINGCommonWarningHIGH confidence

User-issued warning

Production Risk

Low — informational; investigate the condition being flagged.

What this means

The default warning category used by warnings.warn() when no category is specified. Used by libraries to alert users to non-fatal issues without raising exceptions.

Why it happens
  1. 1A library calling warnings.warn() to alert about incorrect usage
  2. 2Application code issuing a custom warning
How to reproduce

Library warning about an unusual configuration.

trigger — this will error
trigger — this will error
import warnings

def configure(value):
    if value > 100:
        warnings.warn(f"Value {value} is unusually large", UserWarning)
    return value

configure(200)

expected output

UserWarning: Value 200 is unusually large

Fix 1

Read and act on UserWarning messages

WHEN A UserWarning appears in output

Read and act on UserWarning messages
# Make warnings visible during development:
python -W default script.py

# Treat as errors to enforce resolution:
python -W error::UserWarning script.py

Why this works

UserWarnings are informational; investigate and fix the flagged condition.

Fix 2

Issue UserWarning in library code

WHEN Warning callers about incorrect but non-fatal usage

Issue UserWarning in library code
import warnings

def my_api(deprecated_param=None):
    if deprecated_param is not None:
        warnings.warn(
            "deprecated_param is ignored; use new_param",
            UserWarning,
            stacklevel=2  # Point to caller, not this function
        )

Why this works

stacklevel=2 makes the warning point at the caller's code, making it actionable.

Code examples
Triggerpython
import warnings
warnings.warn("Value too large", UserWarning)
print("continues")
Handle with catch_warningspython
import warnings
with warnings.catch_warnings():
    warnings.simplefilter("error", UserWarning)
    call_library()  # UserWarning becomes exception
Issue in library code with stacklevelpython
import warnings
def api(val):
    if val > 100:
        warnings.warn("val unusually large", UserWarning, stacklevel=2)
    return val
Sources
Official documentation ↗

Python Docs — Built-in Exceptions

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Python errors