Skip to content

__ne__ doesn't allow returning NotImplemented #4767

Closed

Description

@kamahen

It seems that mypy doesn't allow __ne__ returning NotImplemented, (this is allowed with __eq__).
And when I try explicitly adding NotImplemented as a possible return type, I get a run-time error:

  File ".../pod.py", line 65, in PlainOldData
    def __ne__(self, other: Any) -> Union[bool, NotImplemented]:
  File "/usr/lib/python3.6/typing.py", line 682, in inner
    return func(*args, **kwds)
  File "/usr/lib/python3.6/typing.py", line 800, in __getitem__
    parameters = tuple(_type_check(p, msg) for p in parameters)
  File "/usr/lib/python3.6/typing.py", line 800, in <genexpr>
    parameters = tuple(_type_check(p, msg) for p in parameters)
  File "/usr/lib/python3.6/typing.py", line 374, in _type_check
    raise TypeError(msg + " Got %.100r." % (arg,))
TypeError: Union[arg, ...]: each arg must be a type. Got NotImplemented.

If I change this to def __ne__(self, other: Any) -> Union[bool, type(NotImplemented)]:, then mypy complains:

error: invalid type comment or annotation
note: Suggestion: use type[...] instead of type(...)

and when I try using this suggestion, I get a runtime error:

TypeError: 'type' object is not subscriptable

@gvanrossum suggested (in issue 1101) that this was due to __ne__ not being in sharedparse.__MAGICMETHODS; but it does appear to be there.

Activity

  1. gvanrossum commented on Mar 20, 2018

    @gvanrossum
    Member

    But it's not in BINARY_MAGIC_METHODS.

  2. kamahen commented on Mar 20, 2018

    @kamahen
    ContributorAuthor

    Oops. I'll put together a PR.

  3. added a commit that references this issue on Mar 28, 2018
    8b8d285
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions