Skip to content

Commit cb8b23d

Browse files
authored
[flake8-pyi] Avoid flagging custom-typevar-for-self on metaclass methods (PYI019) (#16141)
1 parent be49151 commit cb8b23d

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI019_0.py

+7
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,10 @@ def m[S](self: S) -> S:
174174
type S = int
175175
print(S) # not a reference to the type variable, so not touched by the autofix
176176
return 42
177+
178+
179+
MetaType = TypeVar("MetaType")
180+
181+
class MetaTestClass(type):
182+
def m(cls: MetaType) -> MetaType:
183+
return cls

crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI019_0.pyi

+7
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,10 @@ class NoReturnAnnotations:
165165
class MultipleBoundParameters:
166166
def m[S: int, T: int](self: S, other: T) -> S: ...
167167
def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
168+
169+
170+
MetaType = TypeVar("MetaType")
171+
172+
class MetaTestClass(type):
173+
def m(cls: MetaType) -> MetaType:
174+
return cls

crates/ruff_linter/src/rules/flake8_pyi/rules/custom_type_var_for_self.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use itertools::Itertools;
44
use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation};
55
use ruff_macros::{derive_message_formats, ViolationMetadata};
66
use ruff_python_ast as ast;
7+
use ruff_python_semantic::analyze::class::is_metaclass;
78
use ruff_python_semantic::analyze::function_type::{self, FunctionType};
89
use ruff_python_semantic::analyze::visibility::{is_abstract, is_overload};
910
use ruff_python_semantic::{Binding, ResolvedReference, ScopeId, SemanticModel};
@@ -128,9 +129,14 @@ pub(crate) fn custom_type_var_instead_of_self(
128129
.next()?;
129130

130131
let self_or_cls_annotation = self_or_cls_parameter.annotation()?;
132+
let parent_class = current_scope.kind.as_class()?;
131133

132-
// Skip any abstract, static, and overloaded methods.
133-
if is_abstract(decorator_list, semantic) || is_overload(decorator_list, semantic) {
134+
// Skip any abstract/static/overloaded methods,
135+
// and any methods in metaclasses
136+
if is_abstract(decorator_list, semantic)
137+
|| is_overload(decorator_list, semantic)
138+
|| is_metaclass(parent_class, semantic).is_yes()
139+
{
134140
return None;
135141
}
136142

crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview_PYI019_PYI019_0.pyi.snap

+5
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ PYI019_0.pyi:166:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
661661
166 |- def m[S: int, T: int](self: S, other: T) -> S: ...
662662
166 |+ def m[T: int](self, other: T) -> Self: ...
663663
167 167 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
664+
168 168 |
665+
169 169 |
664666

665667
PYI019_0.pyi:167:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
666668
|
@@ -677,3 +679,6 @@ PYI019_0.pyi:167:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
677679
166 166 | def m[S: int, T: int](self: S, other: T) -> S: ...
678680
167 |- def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
679681
167 |+ def n[T: (int, str)](self, other: T) -> Self: ...
682+
168 168 |
683+
169 169 |
684+
170 170 | MetaType = TypeVar("MetaType")

0 commit comments

Comments
 (0)