Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More precise type checking of NotImplemented and operator overloading #363

Open
JukkaL opened this issue Aug 4, 2014 · 3 comments
Open
Labels
bug mypy got something wrong priority-1-normal

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 4, 2014

NotImplemented should be implicitly a valid return value of operator methods such as __lt__, __add__, but it should be flagged as an error to return it elsewhere (unless, maybe, the return type includes a suitable type such as typing.NotImplementedType).

Currently NotImplemented has type Any which allows it to be used in any context, which is wrong.

This should be fine:

def __lt__(self, x: int) -> bool:
    if isinstance(x, int):
        return ...
    return NotImplemented

This should not be fine:

def f() -> bool:
    return NotImplemented
@mthuurne
Copy link
Contributor

mthuurne commented Dec 7, 2020

(unless, maybe, the return type includes a suitable type such as typing.NotImplementedType).

There is no typing.NotImplementedType at the moment though. I think Literal[NotImplemented] would be more elegant anyway, but that is not accepted by mypy 0.790.

graingert added a commit to twisted/twisted that referenced this issue Dec 7, 2020
because NoReturn is an alias for Any python/mypy#363
@brianrodri
Copy link

Could we please get some official guidance on how to workaround this issue in the short term?

I understand the complexity at stake here after reading the referenced/referencing issues, but I think it'd be helpful to have an "official workaround" until this gets resolved.

From my understanding, the problem is:

def __eq__(self, other: Any) -> bool: ... # Doesn't work.
def __eq__(self, other: Any) -> Union[bool, NotImplemented]: ... # Doesn't work.
def __eq__(self, other: Any) -> Any: ... # Only option?

@0dminnimda
Copy link

0dminnimda commented Jul 5, 2022

Are there any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

No branches or pull requests

4 participants