Avoid PEP 585 rewrites when builtins are shadowed (#3286)

This commit is contained in:
Charlie Marsh 2023-02-28 18:25:42 -05:00 committed by GitHub
parent af5f7dbd83
commit a032b66c2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View file

@ -28,3 +28,10 @@ def f(x: IList[str]) -> None:
def f(x: "List[str]") -> None: def f(x: "List[str]") -> None:
... ...
list = "abc"
def f(x: List[str]) -> None:
...

View file

@ -26,7 +26,7 @@ impl AlwaysAutofixableViolation for DeprecatedCollectionType {
fn autofix_title(&self) -> String { fn autofix_title(&self) -> String {
let DeprecatedCollectionType { name } = self; let DeprecatedCollectionType { name } = self;
format!("Replace `{name}` with `{}`", name.to_lowercase(),) format!("Replace `{name}` with `{}`", name.to_lowercase())
} }
} }
@ -43,11 +43,14 @@ pub fn use_pep585_annotation(checker: &mut Checker, expr: &Expr) {
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(diagnostic.kind.rule()) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( let binding = binding.to_lowercase();
binding.to_lowercase(), if checker.is_builtin(&binding) {
expr.location, diagnostic.amend(Fix::replacement(
expr.end_location.unwrap(), binding,
)); expr.location,
expr.end_location.unwrap(),
));
}
} }
checker.diagnostics.push(diagnostic); checker.diagnostics.push(diagnostic);
} }

View file

@ -74,4 +74,15 @@ expression: diagnostics
row: 25 row: 25
column: 14 column: 14
parent: ~ parent: ~
- kind:
DeprecatedCollectionType:
name: List
location:
row: 36
column: 9
end_location:
row: 36
column: 13
fix: ~
parent: ~