Skip to content

Commit bf4f1fe

Browse files
committed
Simplify cattrs._compat.is_typeddict
1 parent acd65e7 commit bf4f1fe

File tree

1 file changed

+4
-33
lines changed

1 file changed

+4
-33
lines changed

src/cattrs/_compat.py

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
ExtensionsTypedDict = None
2727

2828
try:
29-
from typing_extensions import _TypedDictMeta as ExtensionsTypedDictMeta
29+
from typing_extensions import is_typeddict
3030
except ImportError:
31-
ExtensionsTypedDictMeta = None
31+
assert sys.version_info >= (3, 10)
32+
from typing import is_typeddict
3233

3334
__all__ = [
3435
"ExtensionsTypedDict",
@@ -176,9 +177,8 @@ def get_final_base(type) -> Optional[type]:
176177
from typing_extensions import get_origin as te_get_origin
177178

178179
if is_py38:
179-
from typing import TypedDict, _TypedDictMeta
180+
from typing import TypedDict
180181
else:
181-
_TypedDictMeta = None
182182
TypedDict = ExtensionsTypedDict
183183

184184
def is_annotated(type) -> bool:
@@ -274,20 +274,6 @@ def copy_with(type, args):
274274
"""Replace a generic type's arguments."""
275275
return type.copy_with(args)
276276

277-
def is_typeddict(cls) -> bool:
278-
return (
279-
cls.__class__ is _TypedDictMeta
280-
or (is_generic(cls) and (cls.__origin__.__class__ is _TypedDictMeta))
281-
or (
282-
ExtensionsTypedDictMeta is not None
283-
and cls.__class__ is ExtensionsTypedDictMeta
284-
or (
285-
is_generic(cls)
286-
and (cls.__origin__.__class__ is ExtensionsTypedDictMeta)
287-
)
288-
)
289-
)
290-
291277
def get_notrequired_base(type) -> "Union[Any, Literal[NOTHING]]":
292278
if get_origin(type) in (NotRequired, Required):
293279
return get_args(type)[0]
@@ -312,7 +298,6 @@ def get_notrequired_base(type) -> "Union[Any, Literal[NOTHING]]":
312298
_AnnotatedAlias,
313299
_GenericAlias,
314300
_SpecialGenericAlias,
315-
_TypedDictMeta,
316301
_UnionGenericAlias,
317302
)
318303

@@ -389,20 +374,6 @@ def get_newtype_base(typ: Any) -> Optional[type]:
389374
return supertype
390375
return None
391376

392-
def is_typeddict(cls) -> bool:
393-
return (
394-
cls.__class__ is _TypedDictMeta
395-
or (is_generic(cls) and (cls.__origin__.__class__ is _TypedDictMeta))
396-
or (
397-
ExtensionsTypedDictMeta is not None
398-
and cls.__class__ is ExtensionsTypedDictMeta
399-
or (
400-
is_generic(cls)
401-
and (cls.__origin__.__class__ is ExtensionsTypedDictMeta)
402-
)
403-
)
404-
)
405-
406377
def get_notrequired_base(type) -> "Union[Any, Literal[NOTHING]]":
407378
if get_origin(type) in (NotRequired, Required):
408379
return get_args(type)[0]

0 commit comments

Comments
 (0)