Skip to content

More precise type checking of NotImplemented and operator overloading #363

@JukkaL

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions