User-issued warning
Production Risk
Low — informational; investigate the condition being flagged.
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.
- 1A library calling warnings.warn() to alert about incorrect usage
- 2Application code issuing a custom warning
Library warning about an unusual configuration.
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
# 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
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.
import warnings
warnings.warn("Value too large", UserWarning)
print("continues")import warnings
with warnings.catch_warnings():
warnings.simplefilter("error", UserWarning)
call_library() # UserWarning becomes exceptionimport warnings
def api(val):
if val > 100:
warnings.warn("val unusually large", UserWarning, stacklevel=2)
return valPython Docs — Built-in Exceptions
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev