You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make ZeroDivisionError message more precise when floor-dividing by zero #119057
try:
10//0exceptZeroDivisionErrorase:
print(e) #returns integer division or modulo by zerotry:
10/0exceptZeroDivisionErrorase:
print(e) #returns integer division by zerotry:
10%0exceptZeroDivisionErrorase:
print(e) #returns integer modulo by zero
When floor dividing by zero the error says "division or modulo" but while (non-floor) dividing by zero or modulo-ing (?) it is more specific saying "division" or "modulo". I would recommend changing this to "floor division" or "division" because it would help with knowing what exactly the error is talking about.
Thank You!
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
changed the title [-]Make error message more precise when floor-dividing by zero[/-][+]Make ZeroDivisionError message more precise when floor-dividing by zero[/+]on May 15, 2024
I was thinking about it yesterday. Perhaps a better solution would be dropping " or modulo" and leaving only "integer division by zero" for x / 0, x // 0 and divmod(x, 0). It does not make difference whether it is a true division, a floor division, or if the modulo is involved, -- it is all division by zero.
But if make the error message more specific (add "floor"), should not it also replace "division or modulo" with "division and modulo" for divmod(x, 0)?
Whatever be done for integers, should be done for other numerical types.
I see the value in harmonising the error messages, but I suspect we're all going to have different opinions on what the message should be. I'd personally drop the mention of the type, so that both 10 // 0 and 10.0 // 0 would give message "floor division by zero". (And as a bonus, fractions.Fraction(10) // 0 would also give message floor division by zero, simply by propagating the error message from the internal integer division.)
So if we're changing this, I'd go for:
10 // 0, 10.0 // 0 - "floor division by zero"
10 / 0, 10.0 // 0 - "division by zero"
10 % 0, 10.0 % 0 is tricky: "modulo by zero" doesn't really work grammatically. But there's an implicit division, so "division by zero" doesn't seem wrong.
Would it be terrible to simply say "division by zero" for all these cases? No mention of type; no distinguishing between the /, //, % operators and divmod.
Perhaps a better solution would be dropping " or modulo" and leaving only "integer division by zero" for x / 0, x // 0 and divmod(x, 0). It does not make difference whether it is a true division, a floor division, or if the modulo is involved, -- it is all division by zero.
If it helps to harmonize the error messages, I support dropping also the type. The only difference may be if mixed types are involved, and the non-zero divisor is converted with losses to the zero of other type. But AFAIK there are no such cases in the core and the stdlib.
In the end, all ZeroDivisionError exceptions may have the same error message.
Activity
[-]Remove redundant ZeroDivisionError details[/-][+]Make error message more precise when floor-dividing by zero[/+][-]Make error message more precise when floor-dividing by zero[/-][+]Make ZeroDivisionError message more precise when floor-dividing by zero[/+]pythongh-119057: Use better error message for `x // 0`
pythongh-119057: Use better error message for `x // 0`
serhiy-storchaka commentedon May 15, 2024
I was thinking about it yesterday. Perhaps a better solution would be dropping " or modulo" and leaving only "integer division by zero" for
x / 0,x // 0anddivmod(x, 0). It does not make difference whether it is a true division, a floor division, or if the modulo is involved, -- it is all division by zero.But if make the error message more specific (add "floor"), should not it also replace "division or modulo" with "division and modulo" for
divmod(x, 0)?Whatever be done for integers, should be done for other numerical types.
cc @mdickinson
mdickinson commentedon May 15, 2024
I see the value in harmonising the error messages, but I suspect we're all going to have different opinions on what the message should be. I'd personally drop the mention of the type, so that both
10 // 0and10.0 // 0would give message"floor division by zero". (And as a bonus,fractions.Fraction(10) // 0would also give messagefloor division by zero, simply by propagating the error message from the internal integer division.)So if we're changing this, I'd go for:
10 // 0,10.0 // 0- "floor division by zero"10 / 0,10.0 // 0- "division by zero"10 % 0,10.0 % 0is tricky: "modulo by zero" doesn't really work grammatically. But there's an implicit division, so "division by zero" doesn't seem wrong.Would it be terrible to simply say
"division by zero"for all these cases? No mention of type; no distinguishing between the/,//,%operators anddivmod.mdickinson commentedon May 15, 2024
@serhiy-storchaka
+1
serhiy-storchaka commentedon May 15, 2024
If it helps to harmonize the error messages, I support dropping also the type. The only difference may be if mixed types are involved, and the non-zero divisor is converted with losses to the zero of other type. But AFAIK there are no such cases in the core and the stdlib.
In the end, all ZeroDivisionError exceptions may have the same error message.
sobolevn commentedon May 16, 2024
Ok, I agree. I will change my PR 👍
sobolevn commentedon May 16, 2024
I think that I found one case that should not be changed:
I would not want to see "division by zero" here. In fact, it will be just
1 / 0, but still it won't be very clear.I also found that
0.0is used when0 ** -1mdickinson commentedon May 16, 2024
Agreed that the power case shouldn't be changed
gh-119057: Use better error messages for zero division (#119066)
pythongh-119057: Use better error messages for zero division (python#…
pythongh-119057: Use better error messages for zero division (python#…
pythongh-119057: Use better error messages for zero division (python#…