|
48 | 48 | except ImportError: # pragma: no cover |
49 | 49 | ExtensionsTypedDict = None |
50 | 50 |
|
51 | | - |
52 | 51 | if sys.version_info >= (3, 11): |
53 | 52 | from builtins import ExceptionGroup |
54 | 53 | else: |
|
66 | 65 | assert sys.version_info >= (3, 11) |
67 | 66 | from typing import TypeAlias |
68 | 67 |
|
| 68 | +LITERALS = {Literal} |
| 69 | +try: |
| 70 | + from typing_extensions import Literal as teLiteral |
| 71 | + |
| 72 | + LITERALS.add(teLiteral) |
| 73 | +except ImportError: # pragma: no cover |
| 74 | + pass |
| 75 | + |
69 | 76 |
|
70 | 77 | def is_typeddict(cls): |
71 | 78 | """Thin wrapper around typing(_extensions).is_typeddict""" |
@@ -203,7 +210,12 @@ def get_final_base(type) -> Optional[type]: |
203 | 210 | from typing import _LiteralGenericAlias |
204 | 211 |
|
205 | 212 | def is_literal(type) -> bool: |
206 | | - return type.__class__ is _LiteralGenericAlias |
| 213 | + return type in LITERALS or ( |
| 214 | + isinstance( |
| 215 | + type, (_GenericAlias, _LiteralGenericAlias, _SpecialGenericAlias) |
| 216 | + ) |
| 217 | + and type.__origin__ in LITERALS |
| 218 | + ) |
207 | 219 |
|
208 | 220 | except ImportError: # pragma: no cover |
209 | 221 |
|
@@ -479,7 +491,9 @@ def is_counter(type): |
479 | 491 | ) |
480 | 492 |
|
481 | 493 | def is_literal(type) -> bool: |
482 | | - return type.__class__ is _GenericAlias and type.__origin__ is Literal |
| 494 | + return type in LITERALS or ( |
| 495 | + isinstance(type, _GenericAlias) and type.__origin__ in LITERALS |
| 496 | + ) |
483 | 497 |
|
484 | 498 | def is_generic(obj): |
485 | 499 | return isinstance(obj, _GenericAlias) or ( |
|
0 commit comments