Skip to content

mypy thinks raise NotImplemented is okay #5710

Closed

Description

@benjaminp

A mistake I've seen several times is to write raise NotImplemented rather than raise NotImplementedError. mypy is silent about this bug:

$ mypy --version
mypy 0.630
$ cat example.py
def f():
    # type: () -> None
    raise NotImplemented
$ mypy example.py
$

Activity

  1. gvanrossum commented on Oct 1, 2018

    @gvanrossum
    Member

    It looks like this is because in typeshed, NotImplemented is given type Any -- if it had pretty much any other type that doesn't derive from BaseException it would be flagged.

    Unfortunately if we change it to what it really is (a magic singleton) then everything that returns NotImplemented will be flagged as an error unless we special-case that value in mypy (preferably only on binary operators). So this isn't a "please go fix it in typeshed" response -- we have to do two things in concert.

  2. JukkaL commented on Oct 2, 2018

    @JukkaL
    Collaborator

    One option would be to special case only raise NotImplemented and raise NotImplemented(...) to generate an error.

  3. gvanrossum commented on Oct 2, 2018

    @gvanrossum
    Member
  4. JukkaL commented on Oct 2, 2018

    @JukkaL
    Collaborator

    That's a very good point. Maybe return NotImplemented should be only valid in relavant dunder methods (or with a sufficiently permissive return type)?

  5. gvanrossum commented on Oct 2, 2018

    @gvanrossum
    Member

    Right, that's what I was trying to say earlier. :-)

  6. ilevkivskyi commented on Oct 2, 2018

    @ilevkivskyi
    Member

    A very easy but hacky way to fix this is to annotate NotImplemented as NoReturn. The latter is a subtype of al types, so it will be accepted as a return value, but it is not Any so it will be invalid in some other situations such as raising it.

  7. JukkaL commented on Oct 9, 2024

    @JukkaL
    Collaborator

    Fixed in #17890.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions