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

[flake8-pyi] Make PYI019 autofixable for .py files in preview mode as well as stubs #15889

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
also fix most stringized references
  • Loading branch information
AlexWaygood committed Feb 4, 2025
commit bf1db9a1753a668e015a4c8c02985dec2a1865aa
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ def n[S](cls: type[S], other: S) -> S:
x: type[S] = type(other)
return x()

class StringizedReferencesAreTooComplicated:
class StringizedReferencesCanBeFixed:
def m[S](self: S) -> S:
x = cast("S", self)
x = cast("list[tuple[S, S]]", self)
return x

class ButStrangeStringizedReferencesCannotBeFixed:
def m[_T](self: _T) -> _T:
x = cast('list[_\x54]', self)
return x

class DeletionsAreNotTouched:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub(crate) fn custom_type_var_instead_of_self(

let mut diagnostic = Diagnostic::new(
CustomTypeVarForSelf {
typevar_name: custom_typevar.name(checker).to_string(),
typevar_name: custom_typevar.name(checker.source()).to_string(),
},
diagnostic_range,
);
Expand Down Expand Up @@ -507,6 +507,7 @@ fn replace_custom_typevar_with_self(

replace_typevar_usages_with_self(
custom_typevar,
checker.source(),
self_or_cls_annotation.range(),
&self_symbol_binding,
replace_references_range,
Expand Down Expand Up @@ -557,19 +558,23 @@ fn import_self(checker: &Checker, position: TextSize) -> Result<(Edit, String),
/// This ensures that no edit in this series will overlap with other edits.
fn replace_typevar_usages_with_self<'a>(
typevar: TypeVar<'a>,
source: &'a str,
self_or_cls_annotation_range: TextRange,
self_symbol_binding: &'a str,
editable_range: TextRange,
semantic: &'a SemanticModel<'a>,
edits: &mut Vec<Edit>,
) -> anyhow::Result<()> {
let tvar_name = typevar.name(source);
for reference in typevar.references(semantic) {
if reference.in_string_type_definition() {
let reference_range = reference.range();
if &source[reference_range] != tvar_name {
bail!(
"Cannot apply autofix where some references to the TypeVar are in string type definitions"
"Cannot autofix: feference in the source code (`{}`) is not equal to the typevar name (`{}`)",
&source[reference_range],
tvar_name
);
}
let reference_range = reference.range();
if !editable_range.contains_range(reference_range) {
continue;
}
Expand Down Expand Up @@ -629,8 +634,8 @@ impl<'a> TypeVar<'a> {
self.0.kind.is_type_param()
}

fn name(self, checker: &'a Checker) -> &'a str {
self.0.name(checker.source())
fn name(self, source: &'a str) -> &'a str {
self.0.name(source)
}

fn references(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,30 +277,40 @@ PYI019_0.py:149:41: PYI019 Use `Self` instead of custom TypeVar `S`

PYI019_0.py:154:26: PYI019 Use `Self` instead of custom TypeVar `S`
|
153 | class StringizedReferencesAreTooComplicated:
153 | class StringizedReferencesCanBeFixed:
154 | def m[S](self: S) -> S:
| ^ PYI019
155 | x = cast("S", self)
155 | x = cast("list[tuple[S, S]]", self)
156 | return x
|
= help: Replace TypeVar `S` with `Self`

PYI019_0.py:159:26: PYI019 Use `Self` instead of custom TypeVar `S`
PYI019_0.py:159:28: PYI019 Use `Self` instead of custom TypeVar `_T`
|
158 | class DeletionsAreNotTouched:
159 | def m[S](self: S) -> S:
158 | class ButStrangeStringizedReferencesCannotBeFixed:
159 | def m[_T](self: _T) -> _T:
| ^^ PYI019
160 | x = cast('list[_\x54]', self)
161 | return x
|
= help: Replace TypeVar `_T` with `Self`

PYI019_0.py:164:26: PYI019 Use `Self` instead of custom TypeVar `S`
|
163 | class DeletionsAreNotTouched:
164 | def m[S](self: S) -> S:
| ^ PYI019
160 | # `S` is not a local variable here, and `del` can only be used with local variables,
161 | # so `del S` here is not actually a reference to the type variable `S`.
165 | # `S` is not a local variable here, and `del` can only be used with local variables,
166 | # so `del S` here is not actually a reference to the type variable `S`.
|
= help: Replace TypeVar `S` with `Self`

PYI019_0.py:168:26: PYI019 Use `Self` instead of custom TypeVar `S`
PYI019_0.py:173:26: PYI019 Use `Self` instead of custom TypeVar `S`
|
167 | class NamesShadowingTypeVarAreNotTouched:
168 | def m[S](self: S) -> S:
172 | class NamesShadowingTypeVarAreNotTouched:
173 | def m[S](self: S) -> S:
| ^ PYI019
169 | type S = int
170 | print(S) # not a reference to the type variable, so not touched by the autofix
174 | type S = int
175 | print(S) # not a reference to the type variable, so not touched by the autofix
|
= help: Replace TypeVar `S` with `Self`
Original file line number Diff line number Diff line change
Expand Up @@ -617,54 +617,76 @@ PYI019_0.py:149:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
150 |+ x: type[Self] = type(other)
151 151 | return x()
152 152 |
153 153 | class StringizedReferencesAreTooComplicated:
153 153 | class StringizedReferencesCanBeFixed:

PYI019_0.py:154:10: PYI019 Use `Self` instead of custom TypeVar `S`
PYI019_0.py:154:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
153 | class StringizedReferencesAreTooComplicated:
153 | class StringizedReferencesCanBeFixed:
154 | def m[S](self: S) -> S:
| ^^^^^^^^^^^^^^^^^ PYI019
155 | x = cast("S", self)
155 | x = cast("list[tuple[S, S]]", self)
156 | return x
|
= help: Replace TypeVar `S` with `Self`

PYI019_0.py:159:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
ℹ Safe fix
151 151 | return x()
152 152 |
153 153 | class StringizedReferencesCanBeFixed:
154 |- def m[S](self: S) -> S:
155 |- x = cast("list[tuple[S, S]]", self)
154 |+ def m(self) -> Self:
155 |+ x = cast("list[tuple[Self, Self]]", self)
156 156 | return x
157 157 |
158 158 | class ButStrangeStringizedReferencesCannotBeFixed:

PYI019_0.py:159:10: PYI019 Use `Self` instead of custom TypeVar `_T`
|
158 | class DeletionsAreNotTouched:
159 | def m[S](self: S) -> S:
158 | class ButStrangeStringizedReferencesCannotBeFixed:
159 | def m[_T](self: _T) -> _T:
| ^^^^^^^^^^^^^^^^^^^^ PYI019
160 | x = cast('list[_\x54]', self)
161 | return x
|
= help: Replace TypeVar `_T` with `Self`

PYI019_0.py:164:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
163 | class DeletionsAreNotTouched:
164 | def m[S](self: S) -> S:
| ^^^^^^^^^^^^^^^^^ PYI019
160 | # `S` is not a local variable here, and `del` can only be used with local variables,
161 | # so `del S` here is not actually a reference to the type variable `S`.
165 | # `S` is not a local variable here, and `del` can only be used with local variables,
166 | # so `del S` here is not actually a reference to the type variable `S`.
|
= help: Replace TypeVar `S` with `Self`

ℹ Safe fix
156 156 | return x
157 157 |
158 158 | class DeletionsAreNotTouched:
159 |- def m[S](self: S) -> S:
159 |+ def m(self) -> Self:
160 160 | # `S` is not a local variable here, and `del` can only be used with local variables,
161 161 | # so `del S` here is not actually a reference to the type variable `S`.
162 162 | # This `del` statement is therefore not touched by the autofix (it raises `UnboundLocalError`

PYI019_0.py:168:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
161 161 | return x
162 162 |
163 163 | class DeletionsAreNotTouched:
164 |- def m[S](self: S) -> S:
164 |+ def m(self) -> Self:
165 165 | # `S` is not a local variable here, and `del` can only be used with local variables,
166 166 | # so `del S` here is not actually a reference to the type variable `S`.
167 167 | # This `del` statement is therefore not touched by the autofix (it raises `UnboundLocalError`

PYI019_0.py:173:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
167 | class NamesShadowingTypeVarAreNotTouched:
168 | def m[S](self: S) -> S:
172 | class NamesShadowingTypeVarAreNotTouched:
173 | def m[S](self: S) -> S:
| ^^^^^^^^^^^^^^^^^ PYI019
169 | type S = int
170 | print(S) # not a reference to the type variable, so not touched by the autofix
174 | type S = int
175 | print(S) # not a reference to the type variable, so not touched by the autofix
|
= help: Replace TypeVar `S` with `Self`

ℹ Safe fix
165 165 | return self
166 166 |
167 167 | class NamesShadowingTypeVarAreNotTouched:
168 |- def m[S](self: S) -> S:
168 |+ def m(self) -> Self:
169 169 | type S = int
170 170 | print(S) # not a reference to the type variable, so not touched by the autofix
171 171 | return 42
170 170 | return self
171 171 |
172 172 | class NamesShadowingTypeVarAreNotTouched:
173 |- def m[S](self: S) -> S:
173 |+ def m(self) -> Self:
174 174 | type S = int
175 175 | print(S) # not a reference to the type variable, so not touched by the autofix
176 176 | return 42
Loading