-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed as not planned
Closed as not planned
Copy link
Description
[UPDATE: The reported behavior happens only with --warn-return-any or --strict.]
Mypy gives warning: Returning Any from function declared to return "bool" for
class C:
def m(self) -> bool:
return NotImplementedSo, try this:
from typing import Union
class C:
def m(self) -> Union[bool, type(NotImplemented)]:
return NotImplementedwhich gives error
q.py:3: error: invalid type comment or annotation
q.py:3: note: Suggestion: use type[...] instead of type(...)
Following mypy's suggestion results in:
q.py:3: error: Return type becomes "Union[bool, Type[Any]]" due to an unfollowed import
q.py:4: warning: Returning Any from function declared to return "Union[bool, Type[Any]]"
which also gives a runtime error: TypeError: Parameters to generic types must be types. Got NotImplemented. or (when attempting type[...]) TypeError: 'type' object is not subscriptable.
(See also pull #4770, which attempts to fix some magic methods)
There seem to be two issues:
- Only magic methods can return
NotImplementedwithout a complaint from mypy - If the programmer tries to get rid of the mypy complaint by specifying
-> Union[bool, type(NotImplemented)], mypy doesn't like that and suggests changing it to-> Union[bool, type[NotImplemented]], which gets a runtime error (Type[NotImplemented]doesn't work either).
Strilanc, nguyenbathanh, alexshpilkin, cjgibson, jab and 2 more
Metadata
Metadata
Assignees
Labels
No labels