-
Notifications
You must be signed in to change notification settings - Fork 784
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
Comparisons with __eq__ should not raise TypeError #1064
Comments
Hi @davidhewitt: Minor correction on the issue: "should return |
Just to clarify, It seems like the problem may be in the definition for |
Thanks for the clarifications! I think possibly the issue is in how we define Potentially we want to instead let the proc-macro code return |
Hi @davidhewitt I'm willing to give this a fair try. I would need some mentoring, though. First I'm trying to produce a test, maybe in I don't know yet how the macro uses the |
RichComparisonToSelf expects to compare itself with objects of the same type, but they we shouldn't raise TypeError but return NotImplemented.
I've created PR #1072 to address this issue. So far I've only create a simple test case; which, of course, fails. |
Many thanks @mvaled - I have some thoughts on the solution which I'll try to find time to write up later today! |
So here's my thoughts on this issue. It has some design questions which I think we should discuss to help us get the right design for the solution. Please challenge any or all of the below - it's only my ideas and could easily have issues I have not foreseen!
So far I wrote only about We may want to start by making a list of all the protocol methods we think this behaviour is applicable for.
The equivalent concept in C++'s I don't think we need to add any marker here, and instead can do it under-the-hood for all methods identified in (1). Because this'll add extra magic to We might also want to consider adding an attribute e.g.
Let's focus on The So you can follow the implementation from there. The function implementation that's instantiated is Line 251 in 51171f7
Inside that function, the line responsible for argument extraction is this one (currently 281): Line 281 in 51171f7
So, if we go with the design I'm sketching out here, we could change that line so that instead of short-circuiting the error with The rest of the protocol method implementations follow a very similar structure as above. The complication is that they are very uniform in structure so we often implement them using |
Hi @davidhewitt Thanks for your explanation. I have some study to do ;) I'm allotting the next week end for this issue. I'll report back when I know more. |
RichComparisonToSelf expects to compare itself with objects of the same type, but they we shouldn't raise TypeError but return NotImplemented.
RichComparisonToSelf expects to compare itself with objects of the same type, but they we shouldn't raise TypeError but return NotImplemented.
RichComparisonToSelf expects to compare itself with objects of the same type, but they we shouldn't raise TypeError but return NotImplemented.
RichComparisonToSelf expects to compare itself with objects of the same type, but they we shouldn't raise TypeError but return NotImplemented.
RichComparisonToSelf expects to compare itself with objects of the same type, but they we shouldn't raise TypeError but return NotImplemented.
* Add (failing) tests for issue #1064 * Return NotImplemented when richcmp doesn't match the expected type. * Fix tests that expect TypeError when richcmp returns NotImplemented. - The python code 'class Other: pass; c2 {} Other()' was raising a NameError: c2 not found - eq and ne never raise a TypeError, so I split the those cases. * Return NotImplemented for number-like binary operations. * Add dummy impl PyNumberProtocol for the test struct. * Rework tests of NotImplemented. * Make py_ternary_num_func return NotImplemented when type mismatches. * Return NotImplement for type mismatches in binary inplace operators. * Reduce boilerplate with `extract_or_return_not_implemented!` * Extract common definition 'Other' into a function. * Test explicitly for NotImplemented in the __ipow__ test. * Add entry in CHANGELOG for PR #1072. * Add the section 'Emulating numeric types' to the guide. * Ensure we're returning NotImplemented in tests. * Simplify the tests: only test we return NotImplemented. Our previous test were rather indirect: were relying that Python behaves correctly when we return NotImplemented. Now we only test that calling a pyclass dunder method returns NotImplemented when the argument doesn't match the type signature. This is the expected behavior. * Remove reverse operators in tests of NotImplemented The won't be used because of #844. * Apply suggestions from code review Co-authored-by: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com> * Add a note about #844 below the list of reflected operations. Co-authored-by: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com>
Consider this implementation of
__richcmp__
:With this comparison, comparing
MyClass() == 1
(for example) fails withTypeError: Can't convert 1 to MyClass
.This is fairly un-Pythonic; the usual situation for Python 3 is that
==
should returnFalse
if the types can't be compared.The text was updated successfully, but these errors were encountered: