Skip to content

Commit

Permalink
Avoid rewriting any PEP 604 runtime annotations (#3217)
Browse files Browse the repository at this point in the history
Following `pyupgrade`, we'll just _never_ touch these.

Closes #2981.

Closes #3215.
  • Loading branch information
charliermarsh committed Feb 24, 2023
1 parent 42f6153 commit a17b5c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP007.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ def f(x: Union["str", int]) -> None:

def f(x: Union[("str", "int"), float]) -> None:
...


def f() -> None:
x: Optional[str]
x = Optional[str]
5 changes: 3 additions & 2 deletions crates/ruff/src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2172,8 +2172,9 @@ where
// Pre-visit.
match &expr.node {
ExprKind::Subscript { value, slice, .. } => {
// Ex) Optional[...]
if !self.in_deferred_string_type_definition
// Ex) Optional[...], Union[...]
if self.in_type_definition
&& !self.in_deferred_string_type_definition
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.settings.rules.enabled(&Rule::TypingUnion)
&& (self.settings.target_version >= PythonVersion::Py310
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,21 @@ expression: diagnostics
row: 26
column: 40
parent: ~
- kind:
TypingUnion: ~
location:
row: 47
column: 7
end_location:
row: 47
column: 20
fix:
content: str | None
location:
row: 47
column: 7
end_location:
row: 47
column: 20
parent: ~

0 comments on commit a17b5c1

Please sign in to comment.