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
Handle NotImplemented from __ne__ #4770
Changes from 1 commit
4ac02d5
dafce6c
4dfae58
c084266
2a1bd83
bc76eed
7ce5488
6176031
e969ca2
a5c1c7f
67b979f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,8 +145,29 @@ main:4: warning: Returning Any from function declared to return "int" | |
|
||
[case testReturnAnyForNotImplementedInBinaryMagicMethods] | ||
# flags: --warn-return-any | ||
from typing import Any | ||
class A: | ||
def __eq__(self, other: object) -> bool: return NotImplemented | ||
def __init__(self, x: Any) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually the tests in this file are specifically for the Tests that all binary methods support returning I trust that the branching logic is very well tested already, so I don't think we need more tests for that. As to where the basic |
||
self.x = x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indent with four spaces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
def __eq__(self, other: Any) -> bool: | ||
if self.__class__ is other.__class__: | ||
return bool(self.x == other.x) | ||
return NotImplemented | ||
def __lt__(self, other: Any) -> bool: return NotImplemented | ||
def __gt__(self, other: Any) -> bool: return NotImplemented | ||
def __le__(self, other: Any) -> bool: return NotImplemented | ||
def __ge__(self, other: Any) -> bool: return NotImplemented | ||
def __ne__(self, other: Any) -> bool: | ||
if self.__class__ is other.__class__: | ||
return bool(self.x != other.x) | ||
else: | ||
# pylint will complain about this return not needing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment seems irrelevant; we don't run pylint on test cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I put this comment in is that someone might think that the "else" could be removed, leaving the "return" outside then if-then-else (pylint encourages this kind of thing). Do you want me to just remove the comment about pylint complaining? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the comment is helpful, but I won't be the person deciding whether to merge this. Why does it matter whether the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type inferencing might produce different results (I'm essentially doing black-box test thinking). So, I have two ways of generating a Union result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've removed the comment about pylint complaining, and added an explanation of why I have the test done two different ways |
||
# an "else", but want to check that Union[bool, NotImplemented] | ||
# works as the inferred return typ | ||
return NotImplemented | ||
# TODO: If the programmer specifies "-> Union[bool, type(NotImplemented)]", | ||
# mypy suggests changing this to "-> Union[bool, type[NotImplemented]]", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a runtime error, and there's nothing you can do about it. The return type should just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps I should open another bug report instead of discussing here ... anyway, there seem to be two issues:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened issue #4791. |
||
# which gives a run-time error (issue #4767) | ||
[builtins fixtures/notimplemented.pyi] | ||
[out] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
__index__
be added here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I should add test cases for all the operators, to ensure that nothing is missing.
(I don't know enough about index to know whether it should be in this list or not.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a bunch more methods; not entirely sure if they should all be there (e.g.,
__call__
,__del__
).The results are inconsistent. If you look in the test case, I've commented out the ones that fail. I'm guessing that there's "magic" code for handling some of them and that magic code doesn't cover all the cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvanrossum - this change is a bit complicated to review because I also refactored it to avoid the problem of a method being specified in MAGIC_BINARY_METHODS but not in MAGIC_METHODS. Would it make things easier if I reverted that refactoring and submitted it as a separate change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvanrossum - I've refactored my changes a bit to make the changes more obvious.