Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable autofix for annotations within 'simple' string literals #3657

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP006.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ def f(x: "List[str]") -> None:
...


list = "abc"
def f(x: r"List[str]") -> None:
...


def f(x: List[str]) -> None:
def f(x: "List[str]") -> None:
...


def f(x: """List[str]""") -> None:
...


def f(x: "Li" "st[str]") -> None:
...
30 changes: 15 additions & 15 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use log::error;
use nohash_hasher::IntMap;
use rustc_hash::{FxHashMap, FxHashSet};
use rustpython_common::cformat::{CFormatError, CFormatErrorType};
use rustpython_parser as parser;
use rustpython_parser::ast::{
Arg, Arguments, Comprehension, Constant, Excepthandler, ExcepthandlerKind, Expr, ExprContext,
ExprKind, KeywordData, Located, Location, Operator, Pattern, PatternKind, Stmt, StmtKind,
Expand All @@ -19,14 +18,15 @@ use ruff_python_ast::helpers::{
binding_range, extract_handled_exceptions, to_module_path, Exceptions,
};
use ruff_python_ast::operations::{extract_all_names, AllNamesFlags};
use ruff_python_ast::relocate::relocate_expr;
use ruff_python_ast::scope::{
Binding, BindingId, BindingKind, ClassDef, ExecutionContext, FunctionDef, Lambda, Scope,
ScopeId, ScopeKind, ScopeStack,
};
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
use ruff_python_ast::types::{Node, Range, RefEquality};
use ruff_python_ast::typing::{match_annotated_subscript, Callable, SubscriptKind};
use ruff_python_ast::typing::{
match_annotated_subscript, parse_type_annotation, Callable, SubscriptKind,
};
use ruff_python_ast::visitor::{walk_excepthandler, walk_pattern, Visitor};
use ruff_python_ast::{
branch_detection, cast, helpers, operations, str, typing, visibility, visitor,
Expand Down Expand Up @@ -2142,7 +2142,8 @@ where
}

fn visit_expr(&mut self, expr: &'b Expr) {
if !(self.ctx.in_deferred_type_definition || self.ctx.in_deferred_string_type_definition)
if !self.ctx.in_deferred_type_definition
&& self.ctx.in_deferred_string_type_definition.is_none()
&& self.ctx.in_type_definition
&& self.ctx.annotations_future_enabled
{
Expand Down Expand Up @@ -4158,7 +4159,7 @@ impl<'a> Checker<'a> {
self.ctx.bindings[*index].mark_used(scope_id, Range::from(expr), context);

if self.ctx.bindings[*index].kind.is_annotation()
&& !self.ctx.in_deferred_string_type_definition
&& self.ctx.in_deferred_string_type_definition.is_none()
&& !self.ctx.in_deferred_type_definition
{
continue;
Expand Down Expand Up @@ -4542,20 +4543,19 @@ impl<'a> Checker<'a> {
where
'b: 'a,
{
let mut stacks = vec![];
let mut stacks = Vec::with_capacity(self.deferred.string_type_definitions.len());
self.deferred.string_type_definitions.reverse();
while let Some((range, expression, (in_annotation, in_type_checking_block), deferral)) =
while let Some((range, value, (in_annotation, in_type_checking_block), deferral)) =
self.deferred.string_type_definitions.pop()
Comment on lines 4547 to 4549
Copy link
Member

Choose a reason for hiding this comment

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

Nit: We can try to use drain to avoid the need for reversing and manually call pop

let mut string_type_definitions = self.deferred.string_type_definitions.drain(..).rev();
for ((range, value, (in_annotation, in_type_checking_block), deferral)) = string_type_definitions {
	...
}

If the borrow checker gets mad because of multiple mutable borrows of self, then try this:

let mut string_type_definitions = std::mem::take(&mut self.deferred.string_type_definitions);
for ((range, ..)) = string_type_definitions.drain(..).rev() {
  ..
}

// Restore vector, to re-use the heap allocations
self.deferred.string_type_definitions = string_type_definitions; 

Both these approaches assume that the loop's body does not read from or write to `self.deferred.string_type_definitions.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we actually do want to write to self.deferred.string_type_definitions, but we probably don't do it currently (#3655).

{
if let Ok(mut expr) = parser::parse_expression(expression, "<filename>") {
if let Ok((expr, kind)) = parse_type_annotation(value, range, self.locator) {
if in_annotation && self.ctx.annotations_future_enabled {
if self.settings.rules.enabled(Rule::QuotedAnnotation) {
pyupgrade::rules::quoted_annotation(self, expression, range);
pyupgrade::rules::quoted_annotation(self, value, range);
}
}
relocate_expr(&mut expr, range);
allocator.push(expr);
stacks.push(((in_annotation, in_type_checking_block), deferral));
stacks.push((kind, (in_annotation, in_type_checking_block), deferral));
} else {
if self
.settings
Expand All @@ -4564,24 +4564,24 @@ impl<'a> Checker<'a> {
{
self.diagnostics.push(Diagnostic::new(
pyflakes::rules::ForwardAnnotationSyntaxError {
body: expression.to_string(),
body: value.to_string(),
},
range,
));
}
}
}
for (expr, ((in_annotation, in_type_checking_block), (scopes, parents))) in
for (expr, (kind, (in_annotation, in_type_checking_block), (scopes, parents))) in
allocator.iter().zip(stacks)
{
self.ctx.scope_stack = scopes;
self.ctx.parents = parents;
self.ctx.in_annotation = in_annotation;
self.ctx.in_type_checking_block = in_type_checking_block;
self.ctx.in_type_definition = true;
self.ctx.in_deferred_string_type_definition = true;
self.ctx.in_deferred_string_type_definition = Some(kind);
self.visit_expr(expr);
self.ctx.in_deferred_string_type_definition = false;
self.ctx.in_deferred_string_type_definition = None;
self.ctx.in_type_definition = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ expression: diagnostics
fixable: false
location:
row: 58
column: 3
column: 4
end_location:
row: 58
column: 8
column: 7
fix: ~
parent: ~
- kind:
Expand Down Expand Up @@ -139,10 +139,10 @@ expression: diagnostics
fixable: false
location:
row: 115
column: 8
column: 9
end_location:
row: 115
column: 23
column: 22
fix: ~
parent: ~
- kind:
Expand All @@ -152,10 +152,10 @@ expression: diagnostics
fixable: false
location:
row: 123
column: 13
column: 14
end_location:
row: 123
column: 18
column: 17
fix: ~
parent: ~
- kind:
Expand All @@ -165,10 +165,10 @@ expression: diagnostics
fixable: false
location:
row: 123
column: 20
column: 21
end_location:
row: 123
column: 25
column: 24
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ expression: diagnostics
fixable: false
location:
row: 11
column: 9
column: 10
end_location:
row: 11
column: 16
column: 15
fix: ~
parent: ~
- kind:
Expand All @@ -22,10 +22,10 @@ expression: diagnostics
fixable: false
location:
row: 18
column: 16
column: 17
end_location:
row: 18
column: 23
column: 22
fix: ~
parent: ~
- kind:
Expand All @@ -35,10 +35,10 @@ expression: diagnostics
fixable: false
location:
row: 24
column: 12
column: 13
end_location:
row: 24
column: 19
column: 18
fix: ~
parent: ~
- kind:
Expand All @@ -48,10 +48,10 @@ expression: diagnostics
fixable: false
location:
row: 30
column: 10
column: 11
end_location:
row: 30
column: 17
column: 16
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ expression: diagnostics
fixable: false
location:
row: 18
column: 26
column: 27
end_location:
row: 18
column: 30
column: 29
fix: ~
parent: ~
- kind:
Expand All @@ -22,10 +22,10 @@ expression: diagnostics
fixable: false
location:
row: 23
column: 12
column: 13
end_location:
row: 23
column: 17
column: 16
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ expression: diagnostics
fixable: false
location:
row: 20
column: 26
column: 27
end_location:
row: 20
column: 30
column: 29
fix: ~
parent: ~
- kind:
Expand All @@ -22,10 +22,10 @@ expression: diagnostics
fixable: false
location:
row: 25
column: 12
column: 13
end_location:
row: 25
column: 17
column: 16
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ expression: diagnostics
fixable: false
location:
row: 5
column: 11
column: 12
end_location:
row: 5
column: 18
column: 17
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ expression: diagnostics
fixable: false
location:
row: 11
column: 8
column: 9
end_location:
row: 11
column: 13
column: 12
fix: ~
parent: ~
- kind:
Expand All @@ -22,10 +22,10 @@ expression: diagnostics
fixable: false
location:
row: 11
column: 15
column: 16
end_location:
row: 11
column: 22
column: 21
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ expression: diagnostics
fixable: false
location:
row: 4
column: 9
column: 10
end_location:
row: 4
column: 16
column: 15
fix: ~
parent: ~
- kind:
Expand All @@ -22,10 +22,10 @@ expression: diagnostics
fixable: false
location:
row: 9
column: 10
column: 11
end_location:
row: 9
column: 17
column: 16
fix: ~
parent: ~
- kind:
Expand All @@ -35,10 +35,10 @@ expression: diagnostics
fixable: false
location:
row: 14
column: 14
column: 15
end_location:
row: 14
column: 21
column: 20
fix: ~
parent: ~
- kind:
Expand All @@ -48,10 +48,10 @@ expression: diagnostics
fixable: false
location:
row: 19
column: 30
column: 31
end_location:
row: 19
column: 37
column: 36
fix: ~
parent: ~
- kind:
Expand All @@ -61,10 +61,10 @@ expression: diagnostics
fixable: false
location:
row: 24
column: 18
column: 19
end_location:
row: 24
column: 25
column: 24
fix: ~
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ expression: diagnostics
fixable: false
location:
row: 5
column: 29
column: 30
end_location:
row: 5
column: 41
column: 40
fix: ~
parent: ~