20
20
from attr import fields as attrs_fields
21
21
from attr import resolve_types
22
22
23
+ __all__ = ["ExceptionGroup" , "ExtensionsTypedDict" , "TypedDict" , "is_typeddict" ]
24
+
23
25
try :
24
26
from typing_extensions import TypedDict as ExtensionsTypedDict
25
27
except ImportError :
26
28
ExtensionsTypedDict = None
27
29
28
- try :
29
- from typing_extensions import _TypedDictMeta as ExtensionsTypedDictMeta
30
- except ImportError :
31
- ExtensionsTypedDictMeta = None
32
-
33
30
if sys .version_info >= (3 , 8 ):
34
31
from typing import Final , Protocol , get_args , get_origin
35
32
@@ -44,9 +41,20 @@ def get_origin(cl):
44
41
from typing_extensions import Final , Protocol
45
42
46
43
if sys .version_info >= (3 , 11 ):
47
- ExceptionGroup = ExceptionGroup
44
+ from builtins import ExceptionGroup
48
45
else :
49
- from exceptiongroup import ExceptionGroup as ExceptionGroup # noqa: PLC0414
46
+ from exceptiongroup import ExceptionGroup
47
+
48
+ try :
49
+ from typing_extensions import is_typeddict as _is_typeddict
50
+ except ImportError :
51
+ assert sys .version_info >= (3 , 10 )
52
+ from typing import is_typeddict as _is_typeddict
53
+
54
+
55
+ def is_typeddict (cls ):
56
+ """Thin wrapper around typing(_extensions).is_typeddict"""
57
+ return _is_typeddict (getattr (cls , "__origin__" , cls ))
50
58
51
59
52
60
def has (cls ):
@@ -157,7 +165,6 @@ def get_final_base(type) -> Optional[type]:
157
165
_AnnotatedAlias ,
158
166
_GenericAlias ,
159
167
_SpecialGenericAlias ,
160
- _TypedDictMeta ,
161
168
_UnionGenericAlias ,
162
169
)
163
170
@@ -234,20 +241,6 @@ def get_newtype_base(typ: Any) -> Optional[type]:
234
241
return supertype
235
242
return None
236
243
237
- def is_typeddict (cls ) -> bool :
238
- return (
239
- cls .__class__ is _TypedDictMeta
240
- or (is_generic (cls ) and (cls .__origin__ .__class__ is _TypedDictMeta ))
241
- or (
242
- ExtensionsTypedDictMeta is not None
243
- and cls .__class__ is ExtensionsTypedDictMeta
244
- or (
245
- is_generic (cls )
246
- and (cls .__origin__ .__class__ is ExtensionsTypedDictMeta )
247
- )
248
- )
249
- )
250
-
251
244
def get_notrequired_base (type ) -> "Union[Any, Literal[NOTHING]]" :
252
245
if get_origin (type ) in (NotRequired , Required ):
253
246
return get_args (type )[0 ]
@@ -364,9 +357,8 @@ def copy_with(type, args):
364
357
from typing_extensions import get_origin as te_get_origin
365
358
366
359
if sys .version_info >= (3 , 8 ):
367
- from typing import TypedDict , _TypedDictMeta
360
+ from typing import TypedDict
368
361
else :
369
- _TypedDictMeta = None
370
362
TypedDict = ExtensionsTypedDict
371
363
372
364
def is_annotated (type ) -> bool :
@@ -462,20 +454,6 @@ def copy_with(type, args):
462
454
"""Replace a generic type's arguments."""
463
455
return type .copy_with (args )
464
456
465
- def is_typeddict (cls ) -> bool :
466
- return (
467
- cls .__class__ is _TypedDictMeta
468
- or (is_generic (cls ) and (cls .__origin__ .__class__ is _TypedDictMeta ))
469
- or (
470
- ExtensionsTypedDictMeta is not None
471
- and cls .__class__ is ExtensionsTypedDictMeta
472
- or (
473
- is_generic (cls )
474
- and (cls .__origin__ .__class__ is ExtensionsTypedDictMeta )
475
- )
476
- )
477
- )
478
-
479
457
def get_notrequired_base (type ) -> "Union[Any, Literal[NOTHING]]" :
480
458
if get_origin (type ) in (NotRequired , Required ):
481
459
return get_args (type )[0 ]
0 commit comments