Skip to content

[format check] Misleading scientific, engineering, or underscore grouping notations in float #10425

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

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/data/messages/b/bad-float-notation/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mindless_anarchy = 15_04e05 # [bad-float-notation]

this_checker_creation_time = 1751760000.0 # [bad-float-notation]

small_float = 9.0e2 # [bad-float-notation]
6 changes: 6 additions & 0 deletions doc/data/messages/b/bad-float-notation/details.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
There's 4 options associated with this message:
- ``strict-engineering-notation``
- ``strict-scientific-notation``
- ``strict-underscore-notation``
- ``float-notation-threshold``
By default we allow all three standard and the threshold is 10e6.
7 changes: 7 additions & 0 deletions doc/data/messages/b/bad-float-notation/good.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
engineering_notation = 150.4e6
scientific_notation = 1.504e8
pep515_notation = 150_400_000.0

this_checker_creation_time = (3600 * 24 * 365 * 55) + ((6 * 30 + 20) * 3600 * 24)

small_float = 90.0
1 change: 1 addition & 0 deletions doc/data/messages/b/bad-float-notation/related.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `PEP 515 <https://docs.python.org/3/library/string.html#formatstrings>`_
4 changes: 3 additions & 1 deletion doc/test_messages_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def _runTest(self) -> None:
assert len(actual_messages_raw) >= len(bad_files), self.assert_message_bad(
bad_files, actual_messages_raw
)
assert expected_messages == self._get_actual(actual_messages_raw)
actual = self._get_actual(actual_messages_raw)
assert_msg = f"Expected {expected_messages!r} and got {actual!r} in {self._test_file[1]}."
assert expected_messages == actual, assert_msg

def assert_message_good(self, messages: list[Message]) -> str:
good = self._test_file[1]
Expand Down
4 changes: 4 additions & 0 deletions doc/user_guide/checkers/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ Format checker Messages
:unnecessary-semicolon (W0301): *Unnecessary semicolon*
Used when a statement is ended by a semi-colon (";"), which isn't necessary
(that's python, not C ;).
:bad-float-notation (C0329): *'%s' %s, and it should be written as '%s' instead*
Emitted when a number is written in a non-standard notation. The three
allowed notation above the threshold are the scientific notation, the
engineering notation, and the underscore grouping notation defined in PEP515.
:missing-final-newline (C0304): *Final newline missing*
Used when the last line in a file is missing a newline.
:line-too-long (C0301): *Line too long (%s/%s)*
Expand Down
36 changes: 36 additions & 0 deletions doc/user_guide/configuration/all-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,13 @@ Standard Checkers
**Default:** ``""``


--float-notation-threshold
""""""""""""""""""""""""""
*Threshold for float literals to be expected to be written using the scientific, engineering or underscore notation. If the absolute value of a float literal is greater than this value (or smaller than the inverse of this value for scientific and engineering notation), it will be checked.*

**Default:** ``1e6``


--ignore-long-lines
"""""""""""""""""""
*Regexp for a line that is allowed to be longer than the limit.*
Expand Down Expand Up @@ -928,6 +935,27 @@ Standard Checkers
**Default:** ``False``


--strict-engineering-notation
"""""""""""""""""""""""""""""
*Only allow engineering notation for float literals with absolute value bigger than 'float-notation-threshold' or smallerthan the inverse of 'float-notation-threshold'.*

**Default:** ``False``


--strict-scientific-notation
""""""""""""""""""""""""""""
*Only allow scientific notation for float literals with absolute value bigger than 'float-notation-threshold' or smallerthan the inverse of 'float-notation-threshold'.*

**Default:** ``False``


--strict-underscore-notation
""""""""""""""""""""""""""""
*Only allow underscore notation for float literals bigger than 'float-notation-threshold'.*

**Default:** ``False``



.. raw:: html

Expand All @@ -942,6 +970,8 @@ Standard Checkers
# Possible choices: ['', 'LF', 'CRLF']
expected-line-ending-format = ""

float-notation-threshold = 1000000.0

ignore-long-lines = "^\\s*(# )?<?https?://\\S+>?$"

indent-after-paren = 4
Expand All @@ -956,6 +986,12 @@ Standard Checkers

single-line-if-stmt = false

strict-engineering-notation = false

strict-scientific-notation = false

strict-underscore-notation = false



.. raw:: html
Expand Down
1 change: 1 addition & 0 deletions doc/user_guide/messages/messages_overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ All messages in the convention category:
convention/bad-classmethod-argument
convention/bad-docstring-quotes
convention/bad-file-encoding
convention/bad-float-notation
convention/bad-mcs-classmethod-argument
convention/bad-mcs-method-argument
convention/consider-iterating-dictionary
Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10425.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added a check for misleading scientific notations and use of underscore grouping in `float` literals.

Refs #10425
Loading