-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Confused traceback while floordiv
or mod
happens between Fraction
and complex
objects
#102840
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
Comments
In fact, this case is also suitable for |
Fraction
and complex
objectsfloordiv
or mod
happens between Fraction
and complex
objects
Maybe it could be fixed on this way... diff --git a/Lib/fractions.py b/Lib/fractions.py
index c95db0730e..fe3a5d137b 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -611,16 +611,18 @@ class doesn't subclass a concrete type, there's no
"""
def forward(a, b):
- if isinstance(b, Fraction):
- return monomorphic_operator(a, b)
- elif isinstance(b, int):
- return monomorphic_operator(a, Fraction(b))
- elif isinstance(b, float):
- return fallback_operator(float(a), b)
- elif isinstance(b, complex):
- return fallback_operator(complex(a), b)
- else:
- return NotImplemented
+ try:
+ if isinstance(b, Fraction):
+ return monomorphic_operator(a, b)
+ elif isinstance(b, int):
+ return monomorphic_operator(a, Fraction(b))
+ elif isinstance(b, float):
+ return fallback_operator(float(a), b)
+ elif isinstance(b, complex):
+ return fallback_operator(complex(a), b)
+ except TypeError:
+ pass
+ return NotImplemented
forward.__name__ = '__' + fallback_operator.__name__ + '__'
forward.__doc__ = monomorphic_operator.__doc__
(Ditto for the reverse().) Here is a traceback: >>> fractions.Fraction(1,2)//1j
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for //: 'Fraction' and 'complex' |
Thanks! |
As a rule we don't do type checking like this. Is the original traceback really that confusing? |
|
Yes, but the code in the traceback shows that it's converting one argument to |
Assessment of what can be understood by humans within traceback - really hard and subjective thing, so I can't say anything about this at all. |
@ericvsmith: "As a rule we don't do type checking like this. " We already are doing 'this' as the proposal just wraps an existing isinstance chain in try: except. I definitely like the revised message better. |
@terryjreedy: Thanks. I was trying to get across that in general we don't try to improve messages where duck typing fails. But you're correct, in this case we are already dispatching based on the type of a parameter, so my argument is weak. I think the better solution here might be to not try to convert the numerator to a complex value, since we know that |
I'm not convinced that this is worth the effort; it seems like an unlikely case to even hit in the first place. |
…pens between Fraction and complex objects (GH-102842)
I would like to backport this change (the final version), but other coredevs were skeptical. Confusing error message is not a large bug. |
…ns happens between Fraction and complex objects (pythonGH-102842)
This traceback really confused me.
However, it's easy to fix, just need change these lines:
cpython/Lib/fractions.py
Lines 620 to 621 in 5e6661b
To this:
So, after that, we have a pretty nice traceback:
But, here a one problem - would be nice to have in traceback originally
//
instead offloordiv
. I have no idea how to do it, without create a mapping with names. Theoretically - we can add a special attribute for it inoperator
, but it should be a another discussion.Linked PRs
floordiv
ormod
operations happens betweenFraction
andcomplex
objects #102842The text was updated successfully, but these errors were encountered: