Skip to content

Commit d481a51

Browse files
committed
Fixed TypedDict from typing_extensions not being recognized as one
Fixes #443.
1 parent 4c81a00 commit d481a51

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

docs/versionhistory.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This library adheres to
99
- Avoid creating reference cycles when type checking unions
1010
- Fixed ``Optional[...]`` being removed from the AST if it was located within a
1111
subscript (`#442 <https://github.com/agronholm/typeguard/issues/442>`_)
12+
- Fixed ``TypedDict`` from ``typing_extensions`` not being recognized as one
13+
(`#443 <https://github.com/agronholm/typeguard/issues/443>`_)
1214

1315
**4.1.5** (2023-09-11)
1416

src/typeguard/_checkers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,18 @@
4343
from ._memo import TypeCheckMemo
4444
from ._utils import evaluate_forwardref, get_stacklevel, get_type_name, qualified_name
4545

46+
if sys.version_info >= (3, 13):
47+
from typing import is_typeddict
48+
else:
49+
from typing_extensions import is_typeddict
50+
4651
if sys.version_info >= (3, 11):
4752
from typing import (
4853
Annotated,
4954
TypeAlias,
5055
get_args,
5156
get_origin,
5257
get_type_hints,
53-
is_typeddict,
5458
)
5559

5660
SubclassableAny = Any
@@ -61,7 +65,6 @@
6165
get_args,
6266
get_origin,
6367
get_type_hints,
64-
is_typeddict,
6568
)
6669
from typing_extensions import Any as SubclassableAny
6770

tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import re
33
import string
44
import sys
5+
import typing
56
from itertools import count
67

78
import pytest
9+
import typing_extensions
810

911
version_re = re.compile(r"_py(\d)(\d)\.py$")
1012

@@ -25,3 +27,13 @@ def sample_set() -> set:
2527
dummy_set = {letter, num}
2628
if next(iter(dummy_set)) == letter:
2729
return dummy_set
30+
31+
32+
@pytest.fixture(
33+
params=[
34+
pytest.param(typing, id="typing"),
35+
pytest.param(typing_extensions, id="typing_extensions"),
36+
]
37+
)
38+
def typing_provider(request):
39+
return request.param

tests/test_checkers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
TextIO,
2828
Tuple,
2929
Type,
30-
TypedDict,
3130
TypeVar,
3231
Union,
3332
)
@@ -473,8 +472,10 @@ class TestTypedDict:
473472
),
474473
],
475474
)
476-
def test_typed_dict(self, value, total: bool, error_re: Optional[str]):
477-
class DummyDict(TypedDict, total=total):
475+
def test_typed_dict(
476+
self, value, total: bool, error_re: Optional[str], typing_provider
477+
):
478+
class DummyDict(typing_provider.TypedDict, total=total):
478479
x: int
479480
y: str
480481

@@ -483,8 +484,8 @@ class DummyDict(TypedDict, total=total):
483484
else:
484485
check_type(value, DummyDict)
485486

486-
def test_inconsistent_keys_invalid(self):
487-
class DummyDict(TypedDict):
487+
def test_inconsistent_keys_invalid(self, typing_provider):
488+
class DummyDict(typing_provider.TypedDict):
488489
x: int
489490

490491
pytest.raises(

0 commit comments

Comments
 (0)