-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
Ignore Ruff RUF015 #115481
Ignore Ruff RUF015 #115481
Conversation
If |
Yes of course, it is short-circuting, but I didn't see anything as well which should be impacted by it. |
self._auth_module_id = list(self.available_mfa_modules)[0] | ||
self._auth_module_id = next(iter(self.available_mfa_modules)) |
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'm not sure we should really do that. Of course next
might be slightly faster but in most of these cases that doesn't really matter all that much. The more important thing IMO is that, especially for beginners, wrapping something with list
and accessing the first element is easier to reason about than next(...)
.
If it makes sense in a specific case, e.g. because it's part of the core and often executed, I'm not against using next
but I would suggest we don't enable this rule.
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.
Okay, then I revert the changes and put it on ignore instead.
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.
Thanks @autinerd 👍🏻
Proposed change
EDIT: This ignores the Ruff rule RUF015.
ORIGINAL:
This enables the Ruff rule RUF015: Prefer
next(...)
over single element slice.This performs two transformations:
list(x)[0]
-->next(iter(x))
[x for x in y][0]
-->next(x for x in y)
Using
next
for 1. is quite a bit faster (of course depends on the size of the object):For 2.,
next
is actually slower if the source list contains only a few elements:But from about 14 elements onwards (on my machine),
next
is faster:(When the generator expression / list comprehension contains an if clause for filtering,
next
gets already faster with fewer elements)Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: