-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
MNT: Enforce ruff/Perflint rules (PERF) #28970
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
base: main
Are you sure you want to change the base?
Conversation
Needs rebase. |
Rebased. |
numpy/testing/_private/utils.py
Outdated
for k in range(len(desired)): | ||
assert_equal(actual[k], desired[k], f'item={k!r}\n{err_msg}', | ||
for k, i in enumerate(desired): | ||
assert_equal(actual[k], i, f'item={k!r}\n{err_msg}', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is less readable than before, but maybe we have to accept these changes to have the benefits of ruff. Renaming the i
would help here though. Another option would be to turn of the performance rules for the tests (as test performance is typically not critical)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very much out-of-scope here, but generally speaking it's preferable to use parametric tests instead of putting assertions in a for-loop 🤷🏻
But even so, I agree that it wouldn't hurt to rename i
to something more descriptive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To tell the truth, the range(len())
→ enumerate()
change has not been suggested by ruff. I added it for consistency with the desired.keys()
→ desired.items()
change a few lines before.
I am happy to revert the range(len))
→ enumerate()
change and disable the desired.keys()
→ desired.items()
, globally or locally.
Note that PERF rules are not necessarily about "performance" as such. In most cases, these micro-optimisations will have only (very) marginal effect on actual global performance. Rather, the idea of all these rules is to use consistent Python style.
numpy/ma/testutils.py
Outdated
@@ -124,7 +124,7 @@ def assert_equal(actual, desired, err_msg=''): | ||
for k, i in desired.items(): | ||
if k not in actual: | ||
raise AssertionError(f"{k} not in {actual}") | ||
assert_equal(actual[k], desired[k], f'key={k!r}\n{err_msg}') | ||
assert_equal(actual[k], i, f'key={k!r}\n{err_msg}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is not the one suggested by the latest version of ruff (on my system at least). Instead of the change here we could also write this as
for k in desired.keys():
if k not in actual:
raise AssertionError(f"{k} not in {actual}")
assert_equal(actual[k], desired[k], f'key={k!r}\n{err_msg}')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I seem to recall no fixes were available here, or perhaps only "unsafe fixes". But yes, I went for a different fix to address the "performance" issue, perhaps at the expense of readability. I will follow your advice and use the above change instead.
PERF102 When using only the values of a dict use the `values()` method PERF102 When using only the keys of a dict use the `keys()` method
PERF403 Use `dict.update` instead of a for-loop PERF403 Use a dictionary comprehension instead of a for-loop
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
The changes itself look fine now. On enabling the |
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Left out because I seem to recall maintainers don't like it: