Skip to content

More precise type checking of NotImplemented and operator overloading #363

Description

@JukkaL

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions