Skip to content

Commit

Permalink
Make get_origin(Literal[...]) == Literal.
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Apr 10, 2022
1 parent 01f1b91 commit 9574101
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions test_typing_inspect.py
Expand Up @@ -306,6 +306,8 @@ def test_origin(self):
self.assertEqual(get_origin(ClassVar[int]), None)
self.assertEqual(get_origin(Generic), Generic)
self.assertEqual(get_origin(Generic[T]), Generic)
# Cannot use assertEqual on Py3.5.2.
self.assertIs(get_origin(Literal[42]), Literal)
if PY39:
self.assertEqual(get_origin(list[int]), list)
if GENERIC_TUPLE_PARAMETRIZABLE:
Expand Down
8 changes: 5 additions & 3 deletions typing_inspect.py
Expand Up @@ -63,10 +63,10 @@
WITH_FINAL = False

try: # python 3.6
from typing_extensions import _Literal
from typing_extensions import Literal
except ImportError: # python 2.7
try:
from typing import _Literal
from typing import Literal
except ImportError:
WITH_LITERAL = False

Expand Down Expand Up @@ -212,7 +212,7 @@ def is_literal_type(tp):
if NEW_TYPING:
return (tp is Literal or
isinstance(tp, typingGenericAlias) and tp.__origin__ is Literal)
return WITH_LITERAL and type(tp) is _Literal
return WITH_LITERAL and type(tp) is type(Literal)


def is_typevar(tp):
Expand Down Expand Up @@ -313,6 +313,8 @@ def get_origin(tp):
return Union
if is_tuple_type(tp):
return Tuple
if is_literal_type(tp):
return Literal

return None

Expand Down

0 comments on commit 9574101

Please sign in to comment.