Skip to content

Rule for PEP 515 - Underscores in Numeric Literals #12787

@guillp

Description

@guillp

I would like to implement a rule for PEP 515. This allows using underscores as visual separators in numerics. Large numbers are hard to read and using underscore as thousands, millions, etc. separators increases readability a lot.

E.g this would turn this:

my_large_int = 313391313513489341

into:

my_large_int = 313_391_313_513_489_341

A few things I have in mind:

  • underscore would be added as thousands, millions, billions, etc. separator, so every 3 digits
  • for float, it probably makes sense to add it as thousandths, millionths, etc.
  • for integers <= 99999 (so 5 or less digits), no need to add a thousands separator, those are readable enough without it
  • for hexadecimal notation, add an underscore every 4 digits, if the length is > 4 (eg. 0x15_DEAD_BEEF). Would it make sense to left-pad with 0 to have a length that is a multiple of 4 (so 0x0015_DEAD_BEEF) ?
  • for binary notation, add an underscore every 8 digits (eg. 0b1100110_01101111_01101111). Would it make sense to left pad with 0 up to a multiple of 8 (so 0b01100110_01101111_01101111) ?

I did not find any prior art so if such a rule exists somewhere else, feel free to point me to it as well.

This would be my first Ruff rule and one of my first experiments with Rust, so I'm looking for feedback about the rule itself before I start coding it. If you feel this is not implementable for any reason, just let me know as well :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-decisionAwaiting a decision from a maintainerneeds-designNeeds further design before implementationruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions