---
source: https://iinventedlight.com/library/notes/python-warnings.md
title: "Python warnings - issue DeprecationWarning, filter warnings, and turn them into errors in tests"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, warnings - Warning control; python/cpython (GitHub), Lib/_py_warnings.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: warnings"
tags: [python, warnings, deprecation, testing, stdlib]
client: Librarian
sources:
  - id: S1
    title: "warnings - Warning control"
    publisher: Python docs
    url: https://docs.python.org/3/library/warnings.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/_py_warnings.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/_py_warnings.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python warnings - issue DeprecationWarning, filter warnings, and turn them into errors in tests
**Answer:** Issue warnings by calling the `warn()` function, for example when a program uses an obsolete module. The warnings filter controls whether warnings are ignored, displayed, or turned into errors. Warnings alert the user of a condition that doesn't warrant raising an exception and terminating the program.

## Examples

```python
import warnings

def deprecated_api(message):
    warnings.warn(message, DeprecationWarning, stacklevel=2)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    deprecated_api("old")                  # silenced only here

with warnings.catch_warnings(record=True) as w:
    warnings.simplefilter("always")
    deprecated_api("old")
    assert issubclass(w[-1].category, DeprecationWarning)

warnings.filterwarnings("error", category=DeprecationWarning, module="myapp")
```

```shell
python -W error::DeprecationWarning app.py
PYTHONWARNINGS=default python app.py
```

## Filter actions

| Action | Matching warnings |
|---|---|
| `"error"` | turned into exceptions |
| `"ignore"` | are never printed |
| `"always"` | are always printed |
| `"default"` | print the first occurrence for each location |
| `"module"` | print the first occurrence for each module |
| `"once"` | print only the first occurrence, regardless of location |

Filters are an ordered list; a warning is matched against each entry in turn until a match is found. `filterwarnings()` and `simplefilter()` insert entries at the front.

## Details

- `category` defaults to `UserWarning`.
- `stacklevel=2` makes the warning refer to the caller of your wrapper function rather than the wrapper itself.
- Within the `catch_warnings()` context manager you can temporarily mutate the warnings filter. With `record=True` it collects warnings for tests.
- By default, `DeprecationWarning` is ignored except when triggered directly by code in `__main__`. Test runners should show all warnings for the code under test.
- `warnings.deprecated` is a decorator (3.13+) that marks a class or function as deprecated for runtime warnings and static type checkers.

## See also

- [python-unittest](https://iinventedlight.com/library/notes/python-unittest/)
- [python-exceptions-hierarchy](https://iinventedlight.com/library/notes/python-exceptions-hierarchy/)

## Claims

- id: C1
  claim: category defaults to UserWarning.
  source: S1
  evidence: "it defaults to UserWarning ."

- id: C2
  claim: The error action turns warnings into exceptions.
  source: S1
  evidence: "turn matching warnings into exceptions"

- id: C3
  claim: warn may ignore the warning or raise an exception.
  source: S2
  evidence: "Issue a warning, or maybe ignore it or raise an exception."

## Sources

- [S1] warnings - Warning control. Python docs. https://docs.python.org/3/library/warnings.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/_py_warnings.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/_py_warnings.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, warnings - Warning control; python/cpython (GitHub), Lib/_py_warnings.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
