Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork10.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 ourterms of service andprivacy 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 kinrange(len(desired)): | ||
assert_equal(actual[k],desired[k], f'item={k!r}\n{err_msg}', | ||
for k, iinenumerate(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 thei
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 renamei
to something more descriptive.
DimitriPapadopoulosMay 15, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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, therange(len())
→enumerate()
change hasnot been suggested byruff. I added it for consistency with thedesired.keys()
→desired.items()
change a few lines before.
I am happy to revert therange(len))
→enumerate()
change and disable thedesired.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}')
DimitriPapadopoulosMay 15, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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()` methodPERF102 When using only the keys of a dict use the `keys()` method
PERF403 Use `dict.update` instead of a for-loopPERF403 Use a dictionary comprehension instead of a for-loop
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Uh oh!
There was an error while loading.Please reload this page.
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: