From 7421579480148cdfbd7752b17b49a3dbbc7557bc Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Tue, 7 Dec 2021 23:42:31 +0000 Subject: [PATCH] fix weird edge case for is_none_type with python3.8 --- pydantic/typing.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pydantic/typing.py b/pydantic/typing.py index 6134943..409b35a 100644 --- a/pydantic/typing.py +++ b/pydantic/typing.py @@ -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: