Skip to content

autoread_dotenv.warnings

autoread_dotenv.warnings

autoread_dotenv.warnings - Warning utilities.

AutoreadDotenvWarning

Bases: UserWarning

Category for every runtime warning autoread-dotenv emits.

A dedicated subclass of UserWarning lets callers silence only this package without also muting unrelated UserWarnings, via warnings.filterwarnings("ignore", category=AutoreadDotenvWarning) or pytest's filterwarnings config. Re-exported as autoread_dotenv.AutoreadDotenvWarning.

Note: PYTHONWARNINGS / -W cannot reference this category - the interpreter parses those filters before site-packages is importable, so only built-in categories resolve there. And because entrypoint() runs from sitecustomize at startup, an in-process filter only affects a later manual entrypoint() call, not the startup pass. See docs/configuration.md.

simple_warning

simple_warning() -> Generator[None, None, None]

Context manager for simplified warning formatting without tracebacks.

Source code in src/autoread_dotenv/warnings.py
@contextlib.contextmanager
def simple_warning() -> tp.Generator[None, None, None]:
    """Context manager for simplified warning formatting without tracebacks."""
    old_format = warnings.formatwarning

    def simple_format(
        message: Warning | str,
        category: type[Warning],  # noqa: ARG001
        filename: str,  # noqa: ARG001
        lineno: int,  # noqa: ARG001
        line: str | None = None,  # noqa: ARG001
    ) -> str:
        return f"Warning from {__name__}: {message}\n"

    # Keep runtime warnings concise and actionable: users see the warning text without
    # a full stacktrace-style traceback for a library-level configuration issue.
    warnings.formatwarning = simple_format  # ty: ignore[invalid-assignment]
    try:
        yield
    finally:
        warnings.formatwarning = old_format