Skip to content

Commit 216a7b9

Browse files
committed
Fixed wrapper checks not being done when @typechecked is used on a class
Fixes #309.
1 parent bc215ed commit 216a7b9

3 files changed

Lines changed: 21 additions & 16 deletions

File tree

‎docs/versionhistory.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-v
77

88
- Improved the documentation
99
- Fixed assignment unpacking (``a, b = ...``) being checked incorrectly
10+
- Fixed ``@typechecked`` attempting to instrument wrapper decorators such as
11+
``@contextmanager`` when applied to a class
1012

1113
**3.0.0** (2023-03-15)
1214

‎src/typeguard/_decorators.py‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def instrument(f: T_CallableOrType) -> FunctionType | str:
4040
return "__module__ attribute is not set"
4141
elif f.__code__.co_filename == "<stdin>":
4242
return "cannot instrument functions defined in a REPL"
43+
elif hasattr(f, "__wrapped__"):
44+
return (
45+
"@typechecked only supports instrumenting functions wrapped with "
46+
"@classmethod, @staticmethod or @property"
47+
)
4348

4449
target_path = [item for item in f.__qualname__.split(".") if item != "<locals>"]
4550
module_source = inspect.getsource(sys.modules[f.__module__])
@@ -162,29 +167,15 @@ def typechecked(target: T_CallableOrType | None = None) -> Any:
162167
return target
163168

164169
# Find either the first Python wrapper or the actual function
165-
func: FunctionType
166170
wrapper_class: type[classmethod[Any]] | type[staticmethod[Any]] | None = None
167171
if isinstance(target, (classmethod, staticmethod)):
168172
wrapper_class = target.__class__
169173
target = target.__func__
170174

171-
if hasattr(target, "__wrapped__"):
172-
warn(
173-
f"Cannot instrument {function_name(target)} -- @typechecked only supports "
174-
f"instrumenting functions wrapped with @classmethod, @staticmethod or "
175-
f"@property",
176-
InstrumentationWarning,
177-
)
178-
return target
179-
elif isfunction(target):
180-
func = target
181-
else:
182-
raise TypeError("target is not a function or a supported wrapper")
183-
184-
retval = instrument(func)
175+
retval = instrument(target)
185176
if isinstance(retval, str):
186177
warn(
187-
f"{retval} -- not typechecking {function_name(func)}",
178+
f"{retval} -- not typechecking {function_name(target)}",
188179
InstrumentationWarning,
189180
)
190181
return target

‎tests/test_typechecked.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import sys
3+
from contextlib import contextmanager
34
from textwrap import dedent
45
from typing import (
56
Any,
@@ -580,3 +581,14 @@ def foo() -> Internal:
580581
return Internal()
581582

582583
assert isinstance(foo(), Internal)
584+
585+
586+
def test_existing_method_decorator():
587+
@typechecked
588+
class Foo:
589+
@contextmanager
590+
def method(self, x: int) -> None:
591+
yield x + 1
592+
593+
with Foo().method(6) as value:
594+
assert value == 7

0 commit comments

Comments
 (0)