Skip to content

[pyupgrade] Expand docs on reusable TypeVars and scoping (UP046) - #24153

Merged
ntBre merged 5 commits into
astral-sh:mainfrom
majiayu000:docs/issue-18894-up046-reusable-typevars
Apr 22, 2026
Merged

ntBre merged 5 commits into
astral-sh:mainfrom
majiayu000:docs/issue-18894-up046-reusable-typevars

Conversation

@majiayu000

Copy link
Copy Markdown
Contributor

Summary

  • Expand the "Why is this bad?" section to explain that old-style TypeVar variables are allocated at module scope but semantically meaningful only within their generic context, and that PEP 695 eliminates this confusion.
  • Add a second example showing how reusable TypeVars shared across multiple classes can be migrated to PEP 695 syntax using type aliases for the bound.

Based on the discussion in #18894 and the suggestions from @AlexWaygood.

Closes #18894

Test plan

Documentation-only change. Verified with cargo dev generate-all, cargo clippy, and cargo build.

Expand the "Why is this bad?" section to explain that old-style TypeVars
are confusingly allocated at module scope while only meaningful within
their generic context, which PEP 695 eliminates.

Add a second example showing how reusable TypeVars shared across multiple
classes can be migrated using PEP 695 type aliases for the bound.

Closes #18894

Signed-off-by: majiayu000 <1835304752@qq.com>
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre March 24, 2026 08:00
- Link "source of confusion" to PEP 695's Points of Confusion section
- Clarify that shared TypeVars get scoped parameters per class
- Add note on semantic equivalence of old vs new style

Signed-off-by: majiayu000 <1835304752@qq.com>
@AlexWaygood AlexWaygood added the documentation Improvements or additions to public-facing documentation label Mar 24, 2026
@astral-sh-bot

astral-sh-bot Bot commented Mar 24, 2026 •

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

βœ… ecosystem check detected no linter changes.

Linter (preview)

βœ… ecosystem check detected no linter changes.

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! This overall looks good to me, I just had a couple of minor suggestions about the text, and it looks like there's a formatting issue in CI.

majiayu000 and others added 3 commits March 25, 2026 09:50
- Pluralize "Example" to "Examples" heading
- Restructure reusable TypeVar text for clearer reading order

Signed-off-by: majiayu000 <1835304752@qq.com>

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@ntBre
ntBre enabled auto-merge (squash) April 22, 2026 21:49
@ntBre
ntBre disabled auto-merge April 22, 2026 21:49
@ntBre ntBre changed the title Expand UP046 docs on reusable TypeVars and scoping [pyupgrade] Expand docs on reusable TypeVars and scoping (UP046) Apr 22, 2026
@ntBre
ntBre enabled auto-merge (squash) April 22, 2026 21:51
@ntBre
ntBre merged commit a4d5a6d into astral-sh:main Apr 22, 2026
43 checks passed
nicopauss pushed a commit to Intersec/lib-common that referenced this pull request Jun 4, 2026
##### [\`v0.15.12\`](https://github.com/astral-sh/ruff/blob/HEAD/CHANGELOG.md#01512)

Released on 2026-04-24.

##### Preview features

- Implement `#ruff:file-ignore` file-level suppressions ([#23599](astral-sh/ruff#23599))
- Implement `#ruff:ignore` logical-line suppressions ([#23404](astral-sh/ruff#23404))
- Revert preview changes to displayed diagnostic severity in LSP ([#24789](astral-sh/ruff#24789))
- \[`airflow`] Implement `task-branch-as-short-circuit` (`AIR004`) ([#23579](astral-sh/ruff#23579))
- \[`flake8-bugbear`] Fix `break`/`continue` handling in `loop-iterator-mutation` (`B909`) ([#24440](astral-sh/ruff#24440))
- \[`pylint`] Fix `PLC2701` for type parameter scopes ([#24576](astral-sh/ruff#24576))

##### Rule changes

- \[`pandas-vet`] Suggest `.array` as well in `PD011` ([#24805](astral-sh/ruff#24805))

##### CLI

- Respect default Unix permissions for cache files ([#24794](astral-sh/ruff#24794))

##### Documentation

- \[`pylint`] Fix `PLR0124` description not to claim self-comparison always returns the same value ([#24749](astral-sh/ruff#24749))
- \[`pyupgrade`] Expand docs on reusable `TypeVar`s and scoping (`UP046`) ([#24153](astral-sh/ruff#24153))
- Improve rules table accessibility ([#24711](astral-sh/ruff#24711))

##### Contributors

- [@dylwil3](https://github.com/dylwil3)
- [@AlexWaygood](https://github.com/AlexWaygood)
- [@woodruffw](https://github.com/woodruffw)
- [@avasis-ai](https://github.com/avasis-ai)
- [@Dev-iL](https://github.com/Dev-iL)
- [@denyszhak](https://github.com/denyszhak)
- [@ShipItAndPray](https://github.com/ShipItAndPray)
- [@anishgirianish](https://github.com/anishgirianish)
- [@augustelalande](https://github.com/augustelalande)
- [@amyreese](https://github.com/amyreese)
- [@majiayu000](https://github.com/majiayu000)
##### [\`v0.15.11\`](https://github.com/astral-sh/ruff/blob/HEAD/CHANGELOG.md#01511)

Released on 2026-04-16.

##### Preview features

- \[`ruff`] Ignore `RUF029` when function is decorated with `asynccontextmanager` ([#24642](astral-sh/ruff#24642))
- \[`airflow`] Implement `airflow-xcom-pull-in-template-string` (`AIR201`) ([#23583](astral-sh/ruff#23583))
- \[`flake8-bandit`] Fix `S103` false positives and negatives in mask analysis ([#24424](astral-sh/ruff#24424))

##### Bug fixes

- \[`flake8-async`] Omit overridden methods for `ASYNC109` ([#24648](astral-sh/ruff#24648))

##### Documentation

- \[`flake8-async`] Add override mention to `ASYNC109` docs ([#24666](astral-sh/ruff#24666))
- Update Neovim config examples to use `vim.lsp.config` ([#24577](astral-sh/ruff#24577))

##### Contributors

- [@augustelalande](https://github.com/augustelalande)
- [@anishgirianish](https://github.com/anishgirianish)
- [@benberryallwood](https://github.com/benberryallwood)
- [@charliermarsh](https://github.com/charliermarsh)
- [@Dev-iL](https://github.com/Dev-iL)

Renovate-Branch: renovate/2024.6-ruff-0.15.x
Change-Id: I2c5de44f14ce3133db71161eae18c7b43f7ba09b
Priv-Id: 9c1a7f10043a2db2338a90de9a62a4d7989df14d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to public-facing documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve docs for UP046 regarding reusable TypeVars

3 participants