fix weird edge case for is_none_type with python3.8

This commit is contained in:
Samuel Colvin
2021-12-07 23:42:31 +00:00
parent 8c2c602c1b
commit 7421579480
+13 -1
View File
@@ -275,7 +275,7 @@ NoneType = None.__class__
NONE_TYPES: Tuple[Any, Any, Any] = (None, NoneType, Literal[None])
if sys.version_info < (3, 8): # noqa: C901 (ignore complexity)
if sys.version_info < (3, 8):
# Even though this implementation is slower, we need it for python 3.6/3.7:
# In python 3.6/3.7 "Literal" is not a builtin type and uses a different
# mechanism.
@@ -285,6 +285,18 @@ if sys.version_info < (3, 8): # noqa: C901 (ignore complexity)
def is_none_type(type_: Any) -> bool:
return type_ in NONE_TYPES
elif sys.version_info[:2] == (3, 8):
# We can use the fast implementation for 3.8 but there is a very weird bug
# where it can fail for `Literal[None]`.
# We just need to redefine a useless `Literal[None]` inside the function body to fix this
def is_none_type(type_: Any) -> bool:
Literal[None] # fix edge case
for none_type in NONE_TYPES:
if type_ is none_type:
return True
return False
else:
def is_none_type(type_: Any) -> bool: