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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Version history
This library adheres to
`Semantic Versioning 2.0 <https://semver.org/#semantic-versioning-200>`_.

**UNRELEASED**

- Fix display of module name for forward references
(`#492 <https://github.com/agronholm/typeguard/pull/492>`_; PR by @JelleZijlstra)

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

- Dropped Python 3.8 support
Expand All @@ -29,8 +34,6 @@ This library adheres to
(`#465 <https://github.com/agronholm/typeguard/pull/465>`_)
- Fixed basic support for intersection protocols
(`#490 <https://github.com/agronholm/typeguard/pull/490>`_; PR by @antonagestam)
- Fixed protocol checks running against the class of an instance and not the instance
itself (this produced wrong results for non-method member checks)

**4.3.0** (2024-05-27)

Expand Down
5 changes: 4 additions & 1 deletion src/typeguard/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
16 changes: 14 additions & 2 deletions src/typeguard/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
if TYPE_CHECKING:
from ._memo import TypeCheckMemo

if sys.version_info >= (3, 13):
if sys.version_info >= (3, 14):
from typing import get_args, get_origin

def evaluate_forwardref(forwardref: ForwardRef, memo: TypeCheckMemo) -> Any:
return forwardref.evaluate(
globals=memo.globals, locals=memo.locals, type_params=()
)

elif sys.version_info >= (3, 13):
from typing import get_args, get_origin

def evaluate_forwardref(forwardref: ForwardRef, memo: TypeCheckMemo) -> Any:
Expand Down Expand Up @@ -85,7 +93,11 @@ def get_type_name(type_: Any) -> str:

name += f"[{formatted_args}]"

module = getattr(type_, "__module__", None)
# For ForwardRefs, use the module stored on the object if available
if hasattr(type_, "__forward_module__"):
module = type_.__forward_module__
else:
module = getattr(type_, "__module__", None)
if module and module not in (None, "typing", "typing_extensions", "builtins"):
name = module + "." + name

Expand Down
36 changes: 30 additions & 6 deletions tests/test_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Loading