Skip to content

Commit

Permalink
fix weird edge case for is_none_type with python3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Dec 7, 2021
1 parent 8c2c602 commit 7421579
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pydantic/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def is_union(tp: Type[Any]) -> bool:
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.
Expand All @@ -285,6 +285,18 @@ def is_union(tp: Type[Any]) -> bool:
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:
Expand Down

0 comments on commit 7421579

Please sign in to comment.