Skip to content

Commit

Permalink
Fixed TypedDict from typing_extensions not being recognized as one
Browse files Browse the repository at this point in the history
Fixes #443.
  • Loading branch information
agronholm committed Mar 23, 2024
1 parent 4c81a00 commit d481a51
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Expand Up @@ -9,6 +9,8 @@ This library adheres to
- Avoid creating reference cycles when type checking unions
- Fixed ``Optional[...]`` being removed from the AST if it was located within a
subscript (`#442 <https://github.com/agronholm/typeguard/issues/442>`_)
- Fixed ``TypedDict`` from ``typing_extensions`` not being recognized as one
(`#443 <https://github.com/agronholm/typeguard/issues/443>`_)

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

Expand Down
7 changes: 5 additions & 2 deletions src/typeguard/_checkers.py
Expand Up @@ -43,14 +43,18 @@
from ._memo import TypeCheckMemo
from ._utils import evaluate_forwardref, get_stacklevel, get_type_name, qualified_name

if sys.version_info >= (3, 13):
from typing import is_typeddict
else:
from typing_extensions import is_typeddict

if sys.version_info >= (3, 11):
from typing import (
Annotated,
TypeAlias,
get_args,
get_origin,
get_type_hints,
is_typeddict,
)

SubclassableAny = Any
Expand All @@ -61,7 +65,6 @@
get_args,
get_origin,
get_type_hints,
is_typeddict,
)
from typing_extensions import Any as SubclassableAny

Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Expand Up @@ -2,9 +2,11 @@
import re
import string
import sys
import typing
from itertools import count

import pytest
import typing_extensions

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

Expand All @@ -25,3 +27,13 @@ def sample_set() -> set:
dummy_set = {letter, num}
if next(iter(dummy_set)) == letter:
return dummy_set


@pytest.fixture(
params=[
pytest.param(typing, id="typing"),
pytest.param(typing_extensions, id="typing_extensions"),
]
)
def typing_provider(request):
return request.param
11 changes: 6 additions & 5 deletions tests/test_checkers.py
Expand Up @@ -27,7 +27,6 @@
TextIO,
Tuple,
Type,
TypedDict,
TypeVar,
Union,
)
Expand Down Expand Up @@ -473,8 +472,10 @@ class TestTypedDict:
),
],
)
def test_typed_dict(self, value, total: bool, error_re: Optional[str]):
class DummyDict(TypedDict, total=total):
def test_typed_dict(
self, value, total: bool, error_re: Optional[str], typing_provider
):
class DummyDict(typing_provider.TypedDict, total=total):
x: int
y: str

Expand All @@ -483,8 +484,8 @@ class DummyDict(TypedDict, total=total):
else:
check_type(value, DummyDict)

def test_inconsistent_keys_invalid(self):
class DummyDict(TypedDict):
def test_inconsistent_keys_invalid(self, typing_provider):
class DummyDict(typing_provider.TypedDict):
x: int

pytest.raises(
Expand Down

0 comments on commit d481a51

Please sign in to comment.