Skip to content

Commit ceca602

Browse files
[pyupgrade] Clarify fix safety docs (UP007, UP045) (#25288)
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 --------- Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
1 parent 2fd7ed0 commit ceca602

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ use crate::{Applicability, Edit, Fix, FixAvailability, Violation};
4343
/// while `UP045` checks for `typing.Optional`.
4444
///
4545
/// ## Fix safety
46-
/// This rule's fix is marked as unsafe, as it may lead to runtime errors when
47-
/// alongside libraries that rely on runtime type annotations, like Pydantic,
48-
/// on Python versions prior to Python 3.10, or as it may remove comments if they
49-
/// are present within the type annotation being rewritten. It may also lead to
50-
/// runtime errors in unusual and likely incorrect type annotations where the type
51-
/// does not support the `|` operator.
46+
/// This rule's fix is marked as unsafe on Python versions prior to 3.10 because
47+
/// using the PEP-604 syntax may lead to runtime errors in libraries that rely
48+
/// on runtime type annotations, like Pydantic, or in unusual and likely
49+
/// incorrect type annotations where the type does not support the `|`
50+
/// operator. The fix is also marked as unsafe when it would remove comments
51+
/// present within the type annotation being rewritten.
5252
///
5353
/// ## Options
5454
/// - `target-version`
@@ -101,12 +101,12 @@ impl Violation for NonPEP604AnnotationUnion {
101101
/// ```
102102
///
103103
/// ## Fix safety
104-
/// This rule's fix is marked as unsafe, as it may lead to runtime errors
105-
/// using libraries that rely on runtime type annotations, like Pydantic,
106-
/// on Python versions prior to Python 3.10, or as it may remove comments if they
107-
/// are present within the type annotation being rewritten. It may also lead to runtime
108-
/// errors in unusual and likely incorrect type annotations where the type does not
109-
/// support the `|` operator.
104+
/// This rule's fix is marked as unsafe on Python versions prior to 3.10 because
105+
/// using the PEP-604 syntax may lead to runtime errors in libraries that rely
106+
/// on runtime type annotations, like Pydantic, or in unusual and likely
107+
/// incorrect type annotations where the type does not support the `|`
108+
/// operator. The fix is also marked as unsafe when it would remove comments
109+
/// present within the type annotation being rewritten.
110110
///
111111
/// ## Options
112112
/// - `target-version`

0 commit comments

Comments
 (0)