Skip to content

Commit

Permalink
Only avoid PEP604 rewrites for pre-Python 3.10 code
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 1, 2023
1 parent 9d8c6ba commit e3ce35a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2018,12 +2018,12 @@ where
ExprKind::Subscript { value, slice, .. } => {
// Ex) Optional[...]
if !self.in_deferred_string_type_definition
&& self.in_annotation
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.settings.rules.enabled(&Rule::UsePEP604Annotation)
&& (self.settings.target_version >= PythonVersion::Py310
|| (self.settings.target_version >= PythonVersion::Py37
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.annotations_future_enabled))
&& self.annotations_future_enabled
&& self.in_annotation))
{
pyupgrade::rules::use_pep604_annotation(self, expr, value, slice);
}
Expand Down Expand Up @@ -2073,10 +2073,10 @@ where

// Ex) List[...]
if !self.in_deferred_string_type_definition
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.settings.rules.enabled(&Rule::UsePEP585Annotation)
&& (self.settings.target_version >= PythonVersion::Py39
|| (self.settings.target_version >= PythonVersion::Py37
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.annotations_future_enabled
&& self.in_annotation))
&& typing::is_pep585_builtin(self, expr)
Expand Down Expand Up @@ -2118,6 +2118,7 @@ where
ExprKind::Attribute { attr, value, .. } => {
// Ex) typing.List[...]
if !self.in_deferred_string_type_definition
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.settings.rules.enabled(&Rule::UsePEP585Annotation)
&& (self.settings.target_version >= PythonVersion::Py39
|| (self.settings.target_version >= PythonVersion::Py37
Expand Down
4 changes: 3 additions & 1 deletion src/rules/pyupgrade/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pub struct Options {
/// (`Optional[str]` -> `str | None`) rewrites even if a file imports `from
/// __future__ import annotations`. Note that this setting is only
/// applicable when the target Python version is below 3.9 and 3.10
/// respectively.
/// respectively, and enabling it is equivalent to disabling
/// `use-pep585-annotation` (`UP006`) and `use-pep604-annotation`
/// (`UP007`) entirely.
pub keep_runtime_typing: Option<bool>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,22 @@ expression: diagnostics
row: 40
column: 16
parent: ~
- kind:
UsePEP604Annotation: ~
location:
row: 42
column: 20
end_location:
row: 42
column: 47
fix:
content:
- "List[int] | List[str]"
location:
row: 42
column: 20
end_location:
row: 42
column: 47
parent: ~

0 comments on commit e3ce35a

Please sign in to comment.