Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
DimitriPapadopoulos wants to merge5 commits intonumpy:main
base:main
Choose a base branch
Loading
fromDimitriPapadopoulos:PERF
Open
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
STY: Apply ruff/Perflint rule PERF102
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
  • Loading branch information
@DimitriPapadopoulos
DimitriPapadopoulos committedMay 16, 2025
commit54be49e32e1cb15fb68bfd4deaf5c04fb2b0a127
4 changes: 2 additions & 2 deletionsbenchmarks/benchmarks/bench_ufunc.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,7 +53,7 @@ def setup(self, ufuncname):
except AttributeError:
raise NotImplementedError
self.args = []
for_,aarg in get_squares_().items():
for aarg in get_squares_().values():
arg = (aarg,) * 1 # no nin
try:
self.afdn(*arg)
Expand DownExpand Up@@ -100,7 +100,7 @@ def setup(self, ufuncname):
except AttributeError:
raise NotImplementedError
self.args = []
for_,aarg in get_squares_().items():
for aarg in get_squares_().values():
arg = (aarg,) * self.ufn.nin
try:
self.ufn(*arg)
Expand Down
2 changes: 1 addition & 1 deletionnumpy/f2py/func2subr.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -77,7 +77,7 @@ def var2fixfortran(vars, a, fa=None, f90mode=None):

def useiso_c_binding(rout):
useisoc = False
forkey,value in rout['vars'].items():
for value in rout['vars'].values():
kind_value = value.get('kindselector', {}).get('kind')
if kind_value in isoc_kindmap:
return True
Expand Down
8 changes: 4 additions & 4 deletionsnumpy/ma/testutils.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}')
Copy link
Contributor

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}')

Copy link
ContributorAuthor

@DimitriPapadopoulosDimitriPapadopoulosMay 15, 2025
edited
Loading

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.

return
# Case #2: lists .....
if isinstance(desired, (list, tuple)) and isinstance(actual, (list, tuple)):
Expand DownExpand Up@@ -162,12 +162,12 @@ def fail_if_equal(actual, desired, err_msg='',):
for k, i in desired.items():
if k not in actual:
raise AssertionError(repr(k))
fail_if_equal(actual[k],desired[k], f'key={k!r}\n{err_msg}')
fail_if_equal(actual[k],i, f'key={k!r}\n{err_msg}')
return
if isinstance(desired, (list, tuple)) and isinstance(actual, (list, tuple)):
fail_if_equal(len(actual), len(desired), err_msg)
for kinrange(len(desired)):
fail_if_equal(actual[k],desired[k], f'item={k!r}\n{err_msg}')
for k, iinenumerate(desired):
fail_if_equal(actual[k],i, f'item={k!r}\n{err_msg}')
return
if isinstance(actual, np.ndarray) or isinstance(desired, np.ndarray):
return fail_if_array_equal(actual, desired, err_msg)
Expand Down
6 changes: 3 additions & 3 deletionsnumpy/testing/_private/utils.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -356,13 +356,13 @@ def assert_equal(actual, desired, err_msg='', verbose=True, *, strict=False):
for k, i in desired.items():
if k not in actual:
raise AssertionError(repr(k))
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}',
verbose)
return
if isinstance(desired, (list, tuple)) and isinstance(actual, (list, tuple)):
assert_equal(len(actual), len(desired), err_msg, verbose)
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}',
Copy link
Contributor

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)

Copy link
Member

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.

Copy link
ContributorAuthor

@DimitriPapadopoulosDimitriPapadopoulosMay 15, 2025
edited
Loading

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.

verbose)
return
from numpy import imag, iscomplexobj, real
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp