[flake8-pyi] Avoid flagging custom-typevar-for-self on metaclass methods (PYI019) (#16141)
Some checks are pending
CI / cargo build (release) (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

This commit is contained in:
Vlad Nedelcu 2025-02-13 20:44:11 +02:00 committed by GitHub
parent be49151a3d
commit cb8b23d609
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 2 deletions

View file

@ -174,3 +174,10 @@ class NamesShadowingTypeVarAreNotTouched:
type S = int type S = int
print(S) # not a reference to the type variable, so not touched by the autofix print(S) # not a reference to the type variable, so not touched by the autofix
return 42 return 42
MetaType = TypeVar("MetaType")
class MetaTestClass(type):
def m(cls: MetaType) -> MetaType:
return cls

View file

@ -165,3 +165,10 @@ class NoReturnAnnotations:
class MultipleBoundParameters: class MultipleBoundParameters:
def m[S: int, T: int](self: S, other: T) -> S: ... def m[S: int, T: int](self: S, other: T) -> S: ...
def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ... def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
MetaType = TypeVar("MetaType")
class MetaTestClass(type):
def m(cls: MetaType) -> MetaType:
return cls

View file

@ -4,6 +4,7 @@ use itertools::Itertools;
use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation}; use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata}; use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast as ast; use ruff_python_ast as ast;
use ruff_python_semantic::analyze::class::is_metaclass;
use ruff_python_semantic::analyze::function_type::{self, FunctionType}; use ruff_python_semantic::analyze::function_type::{self, FunctionType};
use ruff_python_semantic::analyze::visibility::{is_abstract, is_overload}; use ruff_python_semantic::analyze::visibility::{is_abstract, is_overload};
use ruff_python_semantic::{Binding, ResolvedReference, ScopeId, SemanticModel}; use ruff_python_semantic::{Binding, ResolvedReference, ScopeId, SemanticModel};
@ -128,9 +129,14 @@ pub(crate) fn custom_type_var_instead_of_self(
.next()?; .next()?;
let self_or_cls_annotation = self_or_cls_parameter.annotation()?; let self_or_cls_annotation = self_or_cls_parameter.annotation()?;
let parent_class = current_scope.kind.as_class()?;
// Skip any abstract, static, and overloaded methods. // Skip any abstract/static/overloaded methods,
if is_abstract(decorator_list, semantic) || is_overload(decorator_list, semantic) { // and any methods in metaclasses
if is_abstract(decorator_list, semantic)
|| is_overload(decorator_list, semantic)
|| is_metaclass(parent_class, semantic).is_yes()
{
return None; return None;
} }

View file

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