mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
fix weird edge case for is_none_type with python3.8
This commit is contained in:
+13
-1
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user