You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromcollections.abcimportCallablefromtypesimportNotImplementedTypeclassMyClass:
passcallable_union: Callable|NotImplementedTypereveal_type(callable_union)
ifnotisinstance(callable_union, NotImplementedType):
reveal_type(callable_union) # Gets narrowed correctlyclass_union: MyClass|NotImplementedTypereveal_type(class_union)
ifnotisinstance(class_union, NotImplementedType):
reveal_type(class_union) # Mypy does not narrow the type
Leads to the following output:
test.py:10: note: Revealed type is "Union[def (*Any, **Any) -> Any, builtins._NotImplementedType]"
test.py:12: note: Revealed type is "def (*Any, **Any) -> Any"
test.py:15: note: Revealed type is "Union[test.MyClass, builtins._NotImplementedType]"
test.py:17: note: Revealed type is "Union[test.MyClass, builtins._NotImplementedType]"
_NotImplementedType in stdlib/builtins.pyi currently inherits from Any. Removing that inheritance as in the below example fixes the issue for me:
fromtypingimportfinal@finalclass_NotImplementedType:
# A little weird, but typing the __call__ as NotImplemented makes the error message# for NotImplemented() much better__call__: NotImplemented# type: ignore[valid-type] # pyright: ignore[reportGeneralTypeIssues]fixed_type: MyClass|_NotImplementedTypereveal_type(fixed_type)
ifnotisinstance(fixed_type, _NotImplementedType):
reveal_type(fixed_type) # Gets narrowed correctly
Uh oh!
There was an error while loading. Please reload this page.
Leads to the following output:
_NotImplementedType
instdlib/builtins.pyi
currently inherits fromAny
. Removing that inheritance as in the below example fixes the issue for me:Edit: The inheritance from
Any
is actually by design: python/mypy#4791 (comment)The text was updated successfully, but these errors were encountered: