brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -W error turns every warning into an exception

Python
~/tips/tip-python-warnings-as-errors

python3 -W error script.py

Makes python raise warnings as errors, so deprecations fail loudly during development.

Deprecation warnings scroll past and get ignored until an upgrade breaks the code, so I like to see them fail loudly.

python3 -W error script.py
python3 -W error::DeprecationWarning -m pytest

-W error raises each warning as an exception with a traceback that points at the exact line. The second form limits it to one category, here DeprecationWarning. By default python hides that category outside __main__, and -W overrides this. Pytest accepts the same -W option. Expect third-party packages to trip it, so start with one category or add -W ignore::UserWarning:somepkg.

Further reading: Python command line documentation

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link