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

Fixes for Python 3.14 and PEP 649 #492

Merged
merged 12 commits into from
Jan 31, 2025
Prev Previous commit
Next Next commit
Also test 3.14-only behavior
JelleZijlstra committed Sep 23, 2024
commit 90d672f7f38ea3526385f51f0dfdbd386cb05644
5 changes: 4 additions & 1 deletion src/typeguard/_decorators.py
Original file line number Diff line number Diff line change
@@ -117,7 +117,10 @@ def instrument(f: T_CallableOrType) -> FunctionType | str:
new_function.__module__ = f.__module__
new_function.__name__ = f.__name__
new_function.__qualname__ = f.__qualname__
new_function.__annotations__ = f.__annotations__
if sys.version_info >= (3, 14):
new_function.__annotate__ = f.__annotate__
else:
new_function.__annotations__ = f.__annotations__
new_function.__doc__ = f.__doc__
new_function.__defaults__ = f.__defaults__
new_function.__kwdefaults__ = f.__kwdefaults__
36 changes: 30 additions & 6 deletions tests/test_instrumentation.py
Original file line number Diff line number Diff line change
@@ -35,27 +35,38 @@ def method(request: FixtureRequest) -> str:
return request.param


@pytest.fixture(scope="module")
def dummymodule(method: str):
def _fixture_module(name: str, method: str):
config.debug_instrumentation = True
sys.path.insert(0, str(this_dir))
try:
sys.modules.pop("dummymodule", None)
sys.modules.pop(name, None)
if cached_module_path.exists():
cached_module_path.unlink()

if method == "typechecked":
return import_module("dummymodule")
return import_module(name)

with install_import_hook(["dummymodule"]):
with install_import_hook([name]):
with warnings.catch_warnings():
warnings.filterwarnings("error", module="typeguard")
module = import_module("dummymodule")
module = import_module(name)
return module
finally:
sys.path.remove(str(this_dir))


@pytest.fixture(scope="module")
def dummymodule(method: str):
return _fixture_module("dummymodule", method)


@pytest.fixture(scope="module")
def deferredannos(method: str):
if sys.version_info < (3, 14):
raise pytest.skip("Deferred annotations are only supported in Python 3.14+")
return _fixture_module("deferredannos", method)


def test_type_checked_func(dummymodule):
assert dummymodule.type_checked_func(2, 3) == 6

@@ -342,3 +353,16 @@ def test_suppress_annotated_assignment(dummymodule):
def test_suppress_annotated_multi_assignment(dummymodule):
with suppress_type_checks():
assert dummymodule.multi_assign_single_value() == (6, 6, 6)


class TestUsesForwardRef:
def test_success(self, deferredannos):
obj = deferredannos.NotYetDefined()
assert deferredannos.uses_forwardref(obj) is obj

def test_failure(self, deferredannos):
with pytest.raises(
TypeCheckError,
match=r'argument "x" \(int\) is not an instance of deferredannos.NotYetDefined',
):
deferredannos.uses_forwardref(1)