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

__ne__ doesn't allow returning NotImplemented #4767

Closed
kamahen opened this Issue Mar 20, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@kamahen
Contributor

kamahen commented Mar 20, 2018

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.

@gvanrossum

This comment has been minimized.

Member

gvanrossum commented Mar 20, 2018

But it's not in BINARY_MAGIC_METHODS.

@kamahen

This comment has been minimized.

Contributor

kamahen commented Mar 20, 2018

Oops. I'll put together a PR.

kamahen added a commit to kamahen/mypy that referenced this issue Mar 20, 2018

Handle NotImplemented from __ne__; improve test cases
Also change sharedparse.py to avoid this kind of bug in future.
Fixes python#4767.

kamahen added a commit to kamahen/mypy that referenced this issue Mar 20, 2018

Handle NotImplemented from __ne__; improve test cases
Also change sharedparse.py to avoid this kind of bug in future.
Fixes python#4767.

kamahen added a commit to kamahen/mypy that referenced this issue Mar 20, 2018

Handle NotImplemented from __ne__; improve test cases
Also change sharedparse.py to avoid this kind of bug in future.
Fixes python#4767.

gvanrossum added a commit that referenced this issue Mar 28, 2018

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