Repository navigation
[format check] Misleading scientific, engineering, or underscore grouping notations in numbers - #10425
Open
Pierre-Sassoulas wants to merge 12 commits into
Open
[format check] Misleading scientific, engineering, or underscore grouping notations in numbers#10425Pierre-Sassoulas wants to merge 12 commits into
Pierre-Sassoulas wants to merge 12 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10425 +/- ##
==========================================
+ Coverage 96.58% 96.64% +0.05%
==========================================
Files 187 187
Lines 20874 21203 +329
==========================================
+ Hits 20162 20491 +329
Misses 712 712
🚀 New features to boost your workflow:
|
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
June 13, 2025 19:11
fdf5bc1 to
18b00a7
Compare
float
Pierre-Sassoulas
marked this pull request as draft
June 14, 2025 18:53
DanielNoord
reviewed
Jun 16, 2025
DanielNoord
left a comment
Collaborator
There was a problem hiding this comment.
Seems useful, but would probably make it an extension? Or perhaps, first have it as an extension to get feedback?
Member
|
+1 for the idea |
floatfloat
floatfloat
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
June 17, 2025 21:35
f3ff9d4 to
1db9508
Compare
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
June 18, 2025 12:29
1db9508 to
77a3d6b
Compare
This comment has been minimized.
This comment has been minimized.
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
June 21, 2025 09:12
cbe8b6a to
2886dc7
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
June 21, 2025 19:41
8ba51e0 to
35898de
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
June 22, 2025 20:57
afc04e3 to
dc7592d
Compare
This comment has been minimized.
This comment has been minimized.
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
September 13, 2025 13:32
dc7592d to
bad3e82
Compare
This comment has been minimized.
This comment has been minimized.
Contributor
Pierre-Sassoulas
added a commit
to Pierre-Sassoulas/pylint
that referenced
this pull request
Sep 6, 2026
When the source literal already uses underscore grouping in the mantissa (e.g. ``1.002_737_811_911_354_48``, from astropy/astropy#19597), the suggested replacement now keeps the grouping rather than flattening it into a wall of digits. Mantissas of mixed exponent + underscore literals are excluded — pylint flags the mix itself, so suggesting back a still-mixed mantissa would be circular. Adds the ``suggest-mantissa-underscore`` option (default off): when enabled, scientific / engineering / ``repr`` suggestions always group the mantissa, even when the source literal didn't. Refs pylint-dev#10425
Pierre-Sassoulas
added a commit
to Pierre-Sassoulas/pylint
that referenced
this pull request
Sep 6, 2026
Trialling the check on astropy (astropy/astropy#19597) showed the reviewers agreed along one axis, and it was not the notation style: rewriting an integer with PEP 515 underscores was endorsed by everyone who commented, while rewriting a float in scientific notation was rejected. Both lived behind one message, so a project could not adopt the uncontested half on its own. C0331 bad-integer-notation ints, all bases, PEP 515 grouping default on C0329 bad-float-notation floats, the three notation styles default off C0332 bad-float-precision float64 fidelity, unchanged default on C0330 is left alone: it belonged to the deleted bad-continuation, and re-registering it silently shadowed that deletion record, since MessageIdStore.get_active_msgids only consults is_deleted_msgid for ids that are not registered. A legacy 'disable=C0330' disabled bad-float-precision instead of raising DeletedMessageError. Two options answer the concrete objections raised in the review, and both are options rather than behaviour because consistency and brevity are each a legitimate goal: - float-notation-min-gain (default 0) is how many characters a suggestion must save before a plain float is flagged. 0 keeps every out-of-band float on one notation. 1 leaves 299792458.0 and 5724535.74068625 alone, since spelling them out again buys nothing. - allow-aligned-exponents (default yes) leaves a float alone when another float in the same statement shares its exponent, so a measurement and its margin of error stay on one scale (1.3806488e-23 beside 0.0000013e-23). suggest-int-underscore is gone: enabling bad-integer-notation is the opt-in, which also settles the naming discussion on pylint-dev#10425. number-notation-style and number-notation-threshold become float-notation-style and float-notation-threshold, and integers get their own integer-notation-threshold, since notation style never applied to them. FormatChecker.open now validates each check's options under its own guard. The float guard used to compute a threshold string that the integer path read, which would have been an AttributeError once the float check went default-off. Both paths compare with >=, so 1000000 and 1000000.0 are treated alike, and the message says "is at least" rather than "is greater than". Refs pylint-dev#10425
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
September 6, 2026 09:24
bea79a8 to
91fdd45
Compare
Contributor
Pierre-Sassoulas
added a commit
to Pierre-Sassoulas/pylint
that referenced
this pull request
Sep 6, 2026
Trialling the check on astropy (astropy/astropy#19597) showed the reviewers agreed along one axis, and it was not the notation style: rewriting an integer with PEP 515 underscores was endorsed by everyone who commented, while rewriting a float in scientific notation was rejected. Both lived behind one message, so a project could not adopt the uncontested half on its own. C0331 bad-integer-notation ints, all bases, PEP 515 grouping default on C0329 bad-float-notation floats, the three notation styles default off C0332 bad-float-precision float64 fidelity, unchanged default on C0330 is left alone: it belonged to the deleted bad-continuation, and re-registering it silently shadowed that deletion record, since MessageIdStore.get_active_msgids only consults is_deleted_msgid for ids that are not registered. A legacy 'disable=C0330' disabled bad-float-precision instead of raising DeletedMessageError. Two options answer the concrete objections raised in the review, and both are options rather than behaviour because consistency and brevity are each a legitimate goal: - float-notation-min-gain (default 0) is how many characters a suggestion must save before a plain float is flagged. 0 keeps every out-of-band float on one notation. 1 leaves 299792458.0 and 5724535.74068625 alone, since spelling them out again buys nothing. - allow-aligned-exponents (default yes) leaves a float alone when another float in the same statement shares its exponent, so a measurement and its margin of error stay on one scale (1.3806488e-23 beside 0.0000013e-23). suggest-int-underscore is gone: enabling bad-integer-notation is the opt-in, which also settles the naming discussion on pylint-dev#10425. number-notation-style and number-notation-threshold become float-notation-style and float-notation-threshold, and integers get their own integer-notation-threshold, since notation style never applied to them. FormatChecker.open now validates each check's options under its own guard. The float guard used to compute a threshold string that the integer path read, which would have been an AttributeError once the float check went default-off. Both paths compare with >=, so 1000000 and 1000000.0 are treated alike, and the message says "is at least" rather than "is greater than". Refs pylint-dev#10425
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
September 6, 2026 09:58
91fdd45 to
f813b09
Compare
Contributor
Pierre-Sassoulas
added a commit
to Pierre-Sassoulas/pylint
that referenced
this pull request
Sep 6, 2026
When the source literal already uses underscore grouping in the mantissa (e.g. ``1.002_737_811_911_354_48``, from astropy/astropy#19597), the suggested replacement now keeps the grouping rather than flattening it into a wall of digits. Mantissas of mixed exponent + underscore literals are excluded — pylint flags the mix itself, so suggesting back a still-mixed mantissa would be circular. Adds the ``suggest-mantissa-underscore`` option (default off): when enabled, scientific / engineering / ``repr`` suggestions always group the mantissa, even when the source literal didn't. Refs pylint-dev#10425
Pierre-Sassoulas
added a commit
to Pierre-Sassoulas/pylint
that referenced
this pull request
Sep 6, 2026
Trialling the check on astropy (astropy/astropy#19597) showed the reviewers agreed along one axis, and it was not the notation style: rewriting an integer with PEP 515 underscores was endorsed by everyone who commented, while rewriting a float in scientific notation was rejected. Both lived behind one message, so a project could not adopt the uncontested half on its own. C0331 bad-integer-notation ints, all bases, PEP 515 grouping default on C0329 bad-float-notation floats, the three notation styles default off C0332 bad-float-precision float64 fidelity, unchanged default on C0330 is left alone: it belonged to the deleted bad-continuation, and re-registering it silently shadowed that deletion record, since MessageIdStore.get_active_msgids only consults is_deleted_msgid for ids that are not registered. A legacy 'disable=C0330' disabled bad-float-precision instead of raising DeletedMessageError. Two options answer the concrete objections raised in the review, and both are options rather than behaviour because consistency and brevity are each a legitimate goal: - float-notation-min-gain (default 0) is how many characters a suggestion must save before a plain float is flagged. 0 keeps every out-of-band float on one notation. 1 leaves 299792458.0 and 5724535.74068625 alone, since spelling them out again buys nothing. - allow-aligned-exponents (default yes) leaves a float alone when another float in the same statement shares its exponent, so a measurement and its margin of error stay on one scale (1.3806488e-23 beside 0.0000013e-23). suggest-int-underscore is gone: enabling bad-integer-notation is the opt-in, which also settles the naming discussion on pylint-dev#10425. number-notation-style and number-notation-threshold become float-notation-style and float-notation-threshold, and integers get their own integer-notation-threshold, since notation style never applied to them. FormatChecker.open now validates each check's options under its own guard. The float guard used to compute a threshold string that the integer path read, which would have been an AttributeError once the float check went default-off. Both paths compare with >=, so 1000000 and 1000000.0 are treated alike, and the message says "is at least" rather than "is greater than". Refs pylint-dev#10425
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
September 6, 2026 18:16
f813b09 to
ce19119
Compare
Contributor
Flag number literals written in a notation that misleads the reader, and suggest the scientific, engineering or PEP 515 underscore form instead. Includes the esoteric underscore grouping cases, the design notes, the zero and mixed underscore/exponent handling, and a threshold high enough not to fire on everyday values. Co-authored-by: shauss <[email protected]>
Move the notation rebuilding into a dedicated helper class, and extend the PEP 515 grouping checks to octal, binary and hexadecimal literals. Also drops the time-suggestion experiment, which guessed at intent rather than reporting a defect.
The check was never float-only, so the name was wrong. Replaces three mutually exclusive booleans with a single style choice, and drops the opinionated guard that exempted values below 1000.
Adds the suggest-int-underscore option (default off) and reports zero literals written as 0.00, 0e0 and friends. Rewrites the underscore grouping on a Decimal expansion so the digits group correctly either side of the dot.
Roughly 25% faster standardize(), fewer dead branches, and the near-identical notation tests folded into parametrized cases.
Cap underscore suggestions at the 15 significant digits float can actually store, and offer decimal.Decimal for literals that exceed it. Precomputes the threshold strings once in open() instead of per token.
Reports literals that overflow to infinity or underflow to zero, and breaks _check_bad_number_notation into one handler per literal shape so each branch reads on its own.
Notation form and value fidelity are orthogonal: a project may want one without the other. bad-float-precision now owns overflow, underflow and precision loss, and documents the overflow examples.
Replace three mutually-exclusive boolean flags with an allowlist of accepted notation styles. Default `()` keeps the original behavior (any of scientific/engineering/underscore is fine); subsets like `scientific,engineering` let a project allow either form while still rejecting underscore. Reason wording for mismatches is picked from the most permissive remaining allowed style.
When the source literal already uses underscore grouping in the mantissa (e.g. ``1.002_737_811_911_354_48``, from astropy/astropy#19597), the suggested replacement now keeps the grouping rather than flattening it into a wall of digits. Mantissas of mixed exponent + underscore literals are excluded — pylint flags the mix itself, so suggesting back a still-mixed mantissa would be circular. Adds the ``suggest-mantissa-underscore`` option (default off): when enabled, scientific / engineering / ``repr`` suggestions always group the mantissa, even when the source literal didn't. Refs pylint-dev#10425
Trialling the check on astropy (astropy/astropy#19597) showed the reviewers agreed along one axis, and it was not the notation style: rewriting an integer with PEP 515 underscores was endorsed by everyone who commented, while rewriting a float in scientific notation was rejected. Both lived behind one message, so a project could not adopt the uncontested half on its own. C0331 bad-integer-notation ints, all bases, PEP 515 grouping default on C0329 bad-float-notation floats, the three notation styles default off C0332 bad-float-precision float64 fidelity, unchanged default on C0330 is left alone: it belonged to the deleted bad-continuation, and re-registering it silently shadowed that deletion record, since MessageIdStore.get_active_msgids only consults is_deleted_msgid for ids that are not registered. A legacy 'disable=C0330' disabled bad-float-precision instead of raising DeletedMessageError. Two options answer the concrete objections raised in the review, and both are options rather than behaviour because consistency and brevity are each a legitimate goal: - float-notation-min-gain (default 0) is how many characters a suggestion must save before a plain float is flagged. 0 keeps every out-of-band float on one notation. 1 leaves 299792458.0 and 5724535.74068625 alone, since spelling them out again buys nothing. - allow-aligned-exponents (default yes) leaves a float alone when another float in the same statement shares its exponent, so a measurement and its margin of error stay on one scale (1.3806488e-23 beside 0.0000013e-23). suggest-int-underscore is gone: enabling bad-integer-notation is the opt-in, which also settles the naming discussion on pylint-dev#10425. number-notation-style and number-notation-threshold become float-notation-style and float-notation-threshold, and integers get their own integer-notation-threshold, since notation style never applied to them. FormatChecker.open now validates each check's options under its own guard. The float guard used to compute a threshold string that the integer path read, which would have been an AttributeError once the float check went default-off. Both paths compare with >=, so 1000000 and 1000000.0 are treated alike, and the message says "is at least" rather than "is greater than". Refs pylint-dev#10425
Runs of consecutive digits (1234567890, 987654321) are read by their shape, usually as seeds or placeholders, and grouping them hides it. They are no longer flagged when written without underscores. The new 'ignored-integers' option lists values a project knows by heart, such as int32/int64 bounds. Values are compared whatever the base, sign or grouping used to write them.
Pierre-Sassoulas
force-pushed
the
scientific-notation-formatting
branch
from
October 1, 2026 20:55
ce19119 to
5775fea
Compare
Contributor
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of Changes
Description
A little experiment to gather feedback with primers and reviews (still a draft).
The idea is to enforce normalized ways to write number above a threshold or close to 0 in absolute value:
Another idea could be to warn to use python's Decimal for number with more than 15 digits of precision, not sure if it should be the same checker though. We could also set/enforce a default precision for a project.