Skip to content

Make ZeroDivisionError message more precise when floor-dividing by zero #119057

Closed

Description

@E8zEbo8Luna

Feature or enhancement

Proposal:

try:
  10 // 0
except ZeroDivisionError as e:
  print(e) #returns integer division or modulo by zero

try:
  10 / 0
except ZeroDivisionError as e:
  print(e) #returns integer division by zero

try:
  10 % 0
except ZeroDivisionError as e:
  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

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. changed the title [-]Remove redundant ZeroDivisionError details[/-] [+]Make error message more precise when floor-dividing by zero[/+] on May 15, 2024
  2. 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
  3. added 2 commits that reference this issue on May 15, 2024
  4. serhiy-storchaka commented on May 15, 2024

    @serhiy-storchaka
    Member

    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.

    cc @mdickinson

  5. mdickinson commented on May 15, 2024

    @mdickinson
    Member

    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.

  6. mdickinson commented on May 15, 2024

    @mdickinson
    Member

    @serhiy-storchaka

    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.

    +1

  7. serhiy-storchaka commented on May 15, 2024

    @serhiy-storchaka
    Member

    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.

  8. sobolevn commented on May 16, 2024

    @sobolevn
    Member

    Ok, I agree. I will change my PR 👍

  9. sobolevn commented on May 16, 2024

    @sobolevn
    Member

    I think that I found one case that should not be changed:

    >>> 0 ** -1
    Traceback (most recent call last):
      File "<python-input-0>", line 1, in <module>
        0 ** -1
        ~~^^~~~
    ZeroDivisionError: 0.0 cannot be raised to a negative power

    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.0 is used when 0 ** -1

  10. mdickinson commented on May 16, 2024

    @mdickinson
    Member

    Agreed that the power case shouldn't be changed

  11. added a commit that references this issue on Jun 3, 2024
  12. added a commit that references this issue on Jun 3, 2024
  13. added a commit that references this issue on Jun 5, 2024
  14. added a commit that references this issue on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions