Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use environment variable TYPEGUARD_DISABLE to disable typeguard. #475

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/userguide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ Suppressing the ``@typechecked`` decorator in production
If you're using the :func:`@typechecked <typechecked>` decorator to gradually introduce
run-time type checks to your code base, you can disable the checks in production by
running Python in optimized mode (as opposed to debug mode which is the default mode).
You can do this by either starting Python with the ``-O`` or ``-OO`` option, or by
setting the PYTHONOPTIMIZE_ environment variable. This will cause
You can do this by either starting Python with the ``-O`` or ``-OO`` option, or setting
any of the PYTHONOPTIMIZE_ or TYPEGUARD_DISABLE environment variables. This will cause
:func:`@typechecked <typechecked>` to become a no-op when the import hook is not being
used to instrument the code.

Expand Down
3 changes: 3 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Version history

This library adheres to
`Semantic Versioning 2.0 <https://semver.org/#semantic-versioning-200>`_.
**UNRELEASED**

- Setting the TYPEGUARD_DISABLE environment variable has same effect as __debug__=False.

**4.4.1** (2024-11-03)

Expand Down
8 changes: 5 additions & 3 deletions src/typeguard/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ast
import inspect
import os
import sys
from collections.abc import Sequence
from functools import partial
Expand Down Expand Up @@ -158,8 +159,9 @@ def typechecked(
:func:`@staticmethod <staticmethod>`, and :class:`@property <property>` decorated
methods in the class.

.. note:: When Python is run in optimized mode (``-O`` or ``-OO``, this decorator
is a no-op). This is a feature meant for selectively introducing type checking
.. note:: When Python is run in optimized mode (``-O`` or ``-OO``, or when the
environment variable `TYPEGUARD_DISABLE` is set, this decorator is a no-op).
This is a feature meant for selectively introducing type checking
into a code base where the checks aren't meant to be run in production.

:param target: the function or class to enable type checking for
Expand All @@ -182,7 +184,7 @@ def typechecked(
debug_instrumentation=debug_instrumentation,
)

if not __debug__:
if not __debug__ or "TYPEGUARD_DISABLE" in os.environ:
return target

if isclass(target):
Expand Down
16 changes: 10 additions & 6 deletions tests/test_typechecked.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,15 +614,19 @@ def method(self, x: int) -> None:


@pytest.mark.parametrize(
"flags, expected_return_code",
"flags, envs, expected_return_code",
[
pytest.param([], 1, id="debug"),
pytest.param(["-O"], 0, id="O"),
pytest.param(["-OO"], 0, id="OO"),
pytest.param([], {}, 1, id="debug"),
pytest.param([], {"TYPEGUARD_DISABLE": "1"}, 0, id="TYPEGUARD_DISABLE"),
pytest.param(["-O"], {}, 0, id="O"),
pytest.param(
["-O"], {"TYPEGUARD_DISABLE": "1"}, 0, id="TYPEGUARD_DISABLE and -O"
),
pytest.param(["-OO"], {}, 0, id="OO"),
],
)
def test_typechecked_disabled_in_optimized_mode(
tmp_path: Path, flags: List[str], expected_return_code: int
tmp_path: Path, flags: List[str], envs: dict[str, str], expected_return_code: int
):
code = dedent(
"""
Expand All @@ -638,7 +642,7 @@ def foo(x: int) -> None:
script_path = tmp_path / "code.py"
script_path.write_text(code)
process = subprocess.run(
[sys.executable, *flags, str(script_path)], capture_output=True
[sys.executable, *flags, str(script_path)], env=envs, capture_output=True
)
assert process.returncode == expected_return_code
if process.returncode == 1:
Expand Down