Skip to content

Commit

Permalink
Fixed TypedDict checks causing internal TypeError on py3.8 (#219)
Browse files Browse the repository at this point in the history
This works with both native and typing_extensions TypedDicts.

Fixes #218.
  • Loading branch information
agronholm committed Dec 10, 2021
1 parent 2fe7a63 commit 4c98adc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This library adheres to `Semantic Versioning 2.0 <https://semver.org/#semantic-v

- Fixed ``TypeError`` when using typeguard within ``exec()`` (where ``__module__`` is ``None``)
(PR by Andy Jones)
- Fixed ``TypedDict`` causing ``TypeError: TypedDict does not support instance and class checks``
on Python 3.8 with standard library (not ``typing_extensions``) typed dicts

**2.13.2** (2021-11-23)

Expand Down
18 changes: 9 additions & 9 deletions src/typeguard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ def isasyncgenfunction(func):

if sys.version_info >= (3, 10):
from typing import is_typeddict
elif sys.version_info >= (3, 9):
def is_typeddict(tp) -> bool:
else:
_typed_dict_meta_types = ()
if sys.version_info >= (3, 8):
from typing import _TypedDictMeta
_typed_dict_meta_types += (_TypedDictMeta,)

return isinstance(tp, _TypedDictMeta)
else:
try:
from typing_extensions import _TypedDictMeta
_typed_dict_meta_types += (_TypedDictMeta,)
except ImportError:
def is_typeddict(tp) -> bool:
return False
else:
def is_typeddict(tp) -> bool:
return isinstance(tp, _TypedDictMeta)
pass

def is_typeddict(tp) -> bool:
return isinstance(tp, _typed_dict_meta_types)


if TYPE_CHECKING:
Expand Down
9 changes: 6 additions & 3 deletions tests/test_typeguard_py36.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import warnings
from typing import Any, AsyncGenerator, AsyncIterable, AsyncIterator, Callable, Dict

Expand All @@ -7,9 +8,9 @@
from typeguard import TypeChecker, typechecked

try:
from typing_extensions import TypedDict
except ImportError:
from typing import TypedDict
except ImportError:
from typing_extensions import TypedDict


@runtime_checkable
Expand Down Expand Up @@ -110,7 +111,9 @@ def foo(arg: ChildDict):
pass

foo({'x': 1})
pytest.raises(TypeError, foo, {'y': 1})
if sys.version_info[:2] != (3, 8):
# TypedDict is unusable for runtime checking on Python 3.8
pytest.raises(TypeError, foo, {'y': 1})

def test_mapping_is_not_typeddict(self):
"""Regression test for #216."""
Expand Down

0 comments on commit 4c98adc

Please sign in to comment.