Skip to content
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

Merged
merged 4 commits into from
Apr 13, 2024
Merged

Ignore Ruff RUF015 #115481

merged 4 commits into from
Apr 13, 2024

Conversation

autinerd
Copy link
Contributor

@autinerd autinerd commented Apr 12, 2024

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:

  1. list(x)[0] --> next(iter(x))
  2. [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):

❯ python3 -m timeit 'list({"1": "a", "e": 23})[0]'
1000000 loops, best of 5: 297 nsec per loop
❯ python3 -m timeit 'next(iter({"1": "a", "e": 23}))'
2000000 loops, best of 5: 194 nsec per loop

For 2., next is actually slower if the source list contains only a few elements:

❯ python3 -m timeit '[i for i in [1,2,3,4,5]][0]'
1000000 loops, best of 5: 354 nsec per loop
❯ python3 -m timeit 'next(i for i in [1,2,3,4,5])'
500000 loops, best of 5: 496 nsec per loop

But from about 14 elements onwards (on my machine), next is faster:

❯ python3 -m timeit '[i for i in [1,2,3,4,5,6,7,8,9,0,1,2,3,4]][0]'
500000 loops, best of 5: 496 nsec per loop
❯ python3 -m timeit 'next(i for i in [1,2,3,4,5,6,7,8,9,0,1,2,3,4])'
500000 loops, best of 5: 486 nsec per loop

(When the generator expression / list comprehension contains an if clause for filtering, next gets already faster with fewer elements)

❯ python3 -m timeit '[i for i in [1,2,3,4,5,6,7,8,9,0,1,2,3,4] if i % 2 == 0][0]'
500000 loops, best of 5: 844 nsec per loop
❯ python3 -m timeit 'next(i for i in [1,2,3,4,5,6,7,8,9,0,1,2,3,4] if i % 2 == 0)'
500000 loops, best of 5: 570 nsec per loop

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@puddly
Copy link
Contributor

puddly commented Apr 12, 2024

If x is a generator, this will change behavior by not exhaustively iterating over the whole thing (which only matters if the generator has side effects). Not sure if it matters here though, none of the code I'm able to spot does this.

@autinerd
Copy link
Contributor Author

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))
Copy link
Member

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.

Copy link
Contributor Author

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.

@autinerd autinerd changed the title Enable Ruff RUF015 Ignore Ruff RUF015 Apr 13, 2024
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @autinerd 👍🏻

@cdce8p cdce8p merged commit 1a9ff8c into home-assistant:dev Apr 13, 2024
38 checks passed
@autinerd autinerd deleted the ruff-ruf015 branch April 13, 2024 07:46
@github-actions github-actions bot locked and limited conversation to collaborators Apr 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants