I have a class which looks like this:
class Foo:
def __init__(self, string: str) -> None:
self._string = string
def __add__(self, other: str) -> str:
if not isinstance(other, str):
return NotImplemented
return f'{self._string}foo{other}'
Mypy says the 8th line (return NotImplemented
) is unreachable, which is reasonable since I already type-hinted other
as a str
. However, at runtime, other
might not be a string, and if that's the case I would like to return NotImplemented
so that Python would raise an exception unless other
can handle the operation.
Is there a way, other than turning off --warn-unreachable
and use a comment, to nicely let mypy know that no one would ever hear its rambling at runtime and that I need an explicit check?