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

Prefer itertools.pairwise() over zip() for successive pairs (RUF007) #3501

Merged
merged 12 commits into from Mar 17, 2023

Conversation

evanrittenhouse
Copy link
Contributor

Fixes #3297.

We ensure that the two arguments to zip() match and that the lower bound of the slice is 1, if the second argument is a slice. Otherwise, we wouldn't be able to guarantee that the request being made is for successive pairs.

Copy link
Contributor

@andersk andersk left a comment

Choose a reason for hiding this comment

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

itertools.pairwise is new in Python 3.10, so this rule should be restricted to target_version >= PyVersion::Py310.

Should we also check for zip(a[:-1], a[1:])?

crates/ruff/src/rules/ruff/rules/pairwise_over_zipped.rs Outdated Show resolved Hide resolved
@evanrittenhouse evanrittenhouse marked this pull request as draft March 14, 2023 04:17
@github-actions
Copy link
Contributor

github-actions bot commented Mar 15, 2023

✅ ecosystem check detected no changes.

@evanrittenhouse evanrittenhouse marked this pull request as ready for review March 15, 2023 03:51
@evanrittenhouse evanrittenhouse marked this pull request as draft March 16, 2023 19:47
@github-actions
Copy link
Contributor

github-actions bot commented Mar 16, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                        main                                   pr
-----                        ----                                   --
linter/large/dataset.py      1.00      9.5±0.27ms     4.3 MB/sec    1.20     11.4±0.38ms     3.6 MB/sec
linter/numpy/ctypeslib.py    1.00      2.4±0.11ms   139.8 MB/sec    1.01      2.4±0.08ms   138.1 MB/sec
linter/numpy/globals.py      1.00  1233.2±44.12µs   144.5 MB/sec    1.00  1235.5±43.22µs   144.3 MB/sec
linter/pydantic/types.py     1.00      4.4±0.17ms     5.8 MB/sec    1.18      5.2±0.16ms     4.9 MB/sec

Windows

group                        main                                   pr
-----                        ----                                   --
linter/large/dataset.py      1.00      8.7±0.18ms     4.7 MB/sec    1.02      8.9±0.67ms     4.6 MB/sec
linter/numpy/ctypeslib.py    1.04      2.9±0.05ms   116.2 MB/sec    1.00      2.8±0.02ms   121.2 MB/sec
linter/numpy/globals.py      1.03  1510.6±27.55µs   118.0 MB/sec    1.00  1464.6±35.84µs   121.7 MB/sec
linter/pydantic/types.py     1.01      4.1±0.11ms     6.3 MB/sec    1.00      4.0±0.07ms     6.3 MB/sec

@charliermarsh
Copy link
Member

Should have time to merge this tonight if it's in good shape!

@evanrittenhouse
Copy link
Contributor Author

@charliermarsh just have to get the new changes re-reviewed - I changed the logic for determining successive pairs to simply be a difference of 1 between start of first/second slices. That'll allow us to pick up cases like zip(x[2:], x[3:]) which should still trigger the violation, e.g. pairwise(x[2:]).

@evanrittenhouse evanrittenhouse marked this pull request as ready for review March 17, 2023 00:14
@charliermarsh charliermarsh changed the title Prefer itertools.pairwise() over zip() for successive pairs Prefer itertools.pairwise() over zip() for successive pairs (RUF007) Mar 17, 2023
@charliermarsh charliermarsh merged commit 33d2457 into astral-sh:main Mar 17, 2023
12 checks passed
@charliermarsh
Copy link
Member

Thanks @evanrittenhouse!

@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Mar 17, 2023
@evanrittenhouse evanrittenhouse deleted the 3297_itertools_pairwise branch March 17, 2023 13:42
renovate bot added a commit to ixm-one/pytest-cmake-presets that referenced this pull request Mar 18, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [ruff](https://togithub.com/charliermarsh/ruff) | `^0.0.256` ->
`^0.0.257` |
[![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.257/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.257/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.257/compatibility-slim/0.0.256)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.257/confidence-slim/0.0.256)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>charliermarsh/ruff</summary>

###
[`v0.0.257`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.257)

[Compare
Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.256...v0.0.257)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

##### Rules

- \[`ruff`] Prefer `itertools.pairwise()` over `zip()` for successive
pairs (`RUF007`) by
[@&#8203;evanrittenhouse](https://togithub.com/evanrittenhouse) in
[astral-sh/ruff#3501
- \[`flake8-bugbear`] Add `no-explicit-stacklevel` (`B028`) by
[@&#8203;johnor](https://togithub.com/johnor) in
[astral-sh/ruff#3550
- \[`pylint`] invalid-characters-\* by
[@&#8203;r3m0t](https://togithub.com/r3m0t) in
[astral-sh/ruff#3552
- \[`pylint`] Implement `useless-return` (`R1711`) by
[@&#8203;tomecki](https://togithub.com/tomecki) in
[astral-sh/ruff#3116
- \[`pylint`]: Implement `continue-in-finally` (`E0116`) by
[@&#8203;latonis](https://togithub.com/latonis) in
[astral-sh/ruff#3541

##### Bug Fixes

- Rewrite mock import with starred imports by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3566
- Respect `type` overrides in E721 by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3582
- Use `value > max` style in pylint and mccabe messages by
[@&#8203;edgarrmondragon](https://togithub.com/edgarrmondragon) in
[astral-sh/ruff#3553
- Fix autofix conflict between `D209` and `D400` by
[@&#8203;JonathanPlasse](https://togithub.com/JonathanPlasse) in
[astral-sh/ruff#3564
- Avoid C1901 violations within subscripts by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3517
- Avoid adding dashed line outside of docstring by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3581
- Enable ANSI colors on Windows 10 by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3583

#### New Contributors

- [@&#8203;johnor](https://togithub.com/johnor) made their first
contribution in
[astral-sh/ruff#3550

**Full Changelog**:
astral-sh/ruff@v0.0.256...v0.0.257

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/ixm-one/pytest-cmake-presets).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xMC4yIiwidXBkYXRlZEluVmVyIjoiMzUuMTAuMiJ9-->

Signed-off-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
renovate bot added a commit to allenporter/flux-local that referenced this pull request Mar 21, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [ruff](https://togithub.com/charliermarsh/ruff) | `==0.0.256` ->
`==0.0.257` |
[![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.257/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.257/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.257/compatibility-slim/0.0.256)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.257/confidence-slim/0.0.256)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>charliermarsh/ruff</summary>

###
[`v0.0.257`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.257)

[Compare
Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.256...v0.0.257)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

##### Rules

- \[`ruff`] Prefer `itertools.pairwise()` over `zip()` for successive
pairs (`RUF007`) by
[@&#8203;evanrittenhouse](https://togithub.com/evanrittenhouse) in
[astral-sh/ruff#3501
- \[`flake8-bugbear`] Add `no-explicit-stacklevel` (`B028`) by
[@&#8203;johnor](https://togithub.com/johnor) in
[astral-sh/ruff#3550
- \[`pylint`] invalid-characters-\* by
[@&#8203;r3m0t](https://togithub.com/r3m0t) in
[astral-sh/ruff#3552
- \[`pylint`] Implement `useless-return` (`R1711`) by
[@&#8203;tomecki](https://togithub.com/tomecki) in
[astral-sh/ruff#3116
- \[`pylint`]: Implement `continue-in-finally` (`E0116`) by
[@&#8203;latonis](https://togithub.com/latonis) in
[astral-sh/ruff#3541

##### Bug Fixes

- Rewrite mock import with starred imports by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3566
- Respect `type` overrides in E721 by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3582
- Use `value > max` style in pylint and mccabe messages by
[@&#8203;edgarrmondragon](https://togithub.com/edgarrmondragon) in
[astral-sh/ruff#3553
- Fix autofix conflict between `D209` and `D400` by
[@&#8203;JonathanPlasse](https://togithub.com/JonathanPlasse) in
[astral-sh/ruff#3564
- Avoid C1901 violations within subscripts by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3517
- Avoid adding dashed line outside of docstring by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3581
- Enable ANSI colors on Windows 10 by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#3583

#### New Contributors

- [@&#8203;johnor](https://togithub.com/johnor) made their first
contribution in
[astral-sh/ruff#3550

**Full Changelog**:
astral-sh/ruff@v0.0.256...v0.0.257

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/allenporter/flux-local).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNC4yIiwidXBkYXRlZEluVmVyIjoiMzUuMTQuMiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use itertools.pairwise instead of zipping a sequence against itself
4 participants