Skip to content

[pyupgrade] Clarify fix safety docs (UP007, UP045)#25288

Merged
ntBre merged 2 commits into
astral-sh:mainfrom
adityasingh2400:docs-up007-fix-safety-clarify
May 21, 2026
Merged

[pyupgrade] Clarify fix safety docs (UP007, UP045)#25288
ntBre merged 2 commits into
astral-sh:mainfrom
adityasingh2400:docs-up007-fix-safety-clarify

Conversation

@adityasingh2400

@adityasingh2400 adityasingh2400 commented May 21, 2026

Copy link
Copy Markdown
Contributor

The current UP007 "Fix safety" paragraph reads "This rule's fix is marked as unsafe, as it may lead to runtime errors when alongside libraries that rely on runtime type annotations, like Pydantic, on Python versions prior to Python 3.10..." The filer of #17062 hit the fix on Python 3.10+ (where the source explicitly marks the fix safe) and was confused about what the docs were warning about.

In the thread, ntBre clarified the actual semantics ("the fix is marked unsafe on Python versions prior to Python 3.10 but safe on and after 3.10") and pjonsson suggested flipping the sentence around. I rewrote the paragraph to mirror the code: the fix is safe when target is 3.10+ and there are no comments, otherwise it's unsafe for one of two reasons (pre-3.10 runtime introspection like Pydantic can TypeError on X | Y, or comments inside the original would be dropped during rewrite). The unusual-inner-expression caveat is kept. I applied the same rewrite to UP045's docstring because it had the same wording.

Docstring-only. cargo dev generate-all and uvx prek run --from-ref main both pass.

fixes #17062

The current "Fix safety" sentence ("may lead to runtime errors when
alongside libraries...") obscured which combination of [`target-version`]
and comments actually makes the fix unsafe, and the issue's filer hit
the rule fix on Python 3.10+ (where the fix is safe) and was confused
into thinking the docs implied a stronger warning.

Reword as a single rule + two reasons, in line with @pjonsson's
suggestion of flipping the sentence around: the fix is safe when target
is 3.10+ and there are no comments, otherwise unsafe because (a) pre-3.10
runtime introspection (Pydantic, etc.) can `TypeError` on `X | Y`, or
(b) inline comments would be dropped during rewrite. Applied symmetrically
to UP045's docstring since it had the same phrasing.

Closes astral-sh#17062.
@astral-sh-bot astral-sh-bot Bot requested a review from ntBre May 21, 2026 11:23
@astral-sh-bot

astral-sh-bot Bot commented May 21, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

for future reference, I think the "unusal and likely incorrect" part of this should probably be in a
separate Known Issues section, as I don't think this is isolated to Python versions before 3.10,
and the code doesn't actually check anything about the types to determine fix safety.

astral-sh#9306 added this phrase, which I think is worth having
somewhere, just not necessarily in this section
@ntBre ntBre added the documentation Improvements or additions to documentation label May 21, 2026
@ntBre ntBre changed the title [pyupgrade] Clarify UP007 / UP045 fix safety docs [pyupgrade] Clarify fix safety docs (UP007, UP045) May 21, 2026
@ntBre ntBre enabled auto-merge (squash) May 21, 2026 20:04
@ntBre ntBre merged commit ceca602 into astral-sh:main May 21, 2026
44 checks passed
thejchap pushed a commit to thejchap/ruff that referenced this pull request May 23, 2026
…5288)

The current UP007 "Fix safety" paragraph reads "This rule's fix is
marked as unsafe, as it may lead to runtime errors when alongside
libraries that rely on runtime type annotations, like Pydantic, on
Python versions prior to Python 3.10..." The filer of astral-sh#17062 hit the fix
on Python 3.10+ (where the source explicitly marks the fix safe) and was
confused about what the docs were warning about.

In the thread, ntBre clarified the actual semantics ("the fix is marked
unsafe on Python versions prior to Python 3.10 but safe on and after
3.10") and pjonsson suggested flipping the sentence around. I rewrote
the paragraph to mirror the code: the fix is safe when target is 3.10+
and there are no comments, otherwise it's unsafe for one of two reasons
(pre-3.10 runtime introspection like Pydantic can `TypeError` on `X |
Y`, or comments inside the original would be dropped during rewrite).
The unusual-inner-expression caveat is kept. I applied the same rewrite
to UP045's docstring because it had the same wording.

Docstring-only. `cargo dev generate-all` and `uvx prek run --from-ref
main` both pass.

fixes astral-sh#17062

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
anishgirianish pushed a commit to anishgirianish/ruff that referenced this pull request May 28, 2026
…5288)

The current UP007 "Fix safety" paragraph reads "This rule's fix is
marked as unsafe, as it may lead to runtime errors when alongside
libraries that rely on runtime type annotations, like Pydantic, on
Python versions prior to Python 3.10..." The filer of astral-sh#17062 hit the fix
on Python 3.10+ (where the source explicitly marks the fix safe) and was
confused about what the docs were warning about.

In the thread, ntBre clarified the actual semantics ("the fix is marked
unsafe on Python versions prior to Python 3.10 but safe on and after
3.10") and pjonsson suggested flipping the sentence around. I rewrote
the paragraph to mirror the code: the fix is safe when target is 3.10+
and there are no comments, otherwise it's unsafe for one of two reasons
(pre-3.10 runtime introspection like Pydantic can `TypeError` on `X |
Y`, or comments inside the original would be dropped during rewrite).
The unusual-inner-expression caveat is kept. I applied the same rewrite
to UP045's docstring because it had the same wording.

Docstring-only. `cargo dev generate-all` and `uvx prek run --from-ref
main` both pass.

fixes astral-sh#17062

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: fix safety of UP007 should be clarified

2 participants