[ruff-0.10] [flake8-pyi] Stabilize preview-mode behaviours for custom-type-var-for-self(PYI019) (#16607)

## Summary

This PR stabilizes several preview-only behaviours for
`custom-typevar-for-self` (`PYI019`). Namely:
- A new, more accurate technique is now employed for detecting custom
TypeVars that are replaceable with `Self`. See
https://github.com/astral-sh/ruff/pull/15888 for details.
- The range of the diagnostic is now the full function header rather
than just the return annotation. (Previously, the rule only applied to
methods with return annotations, but this is no longer true due to the
changes in the first bullet point.)
- The fix is now available even when preview mode is not enabled.

## Test Plan

- Existing snapshots that do not have preview mode enabled are updated
- Preview-specific snapshots are removed
- I'll check the ecosystem report on this PR to verify everything's as
expected
This commit is contained in:
Alex Waygood 2025-03-11 16:05:25 +00:00 committed by Micha Reiser
parent bbcddf7e79
commit 66cae0a3ec
8 changed files with 958 additions and 1787 deletions

View file

@ -156,30 +156,6 @@ mod tests {
Ok(())
}
#[test_case(Rule::CustomTypeVarForSelf, Path::new("PYI019_0.py"))]
#[test_case(Rule::CustomTypeVarForSelf, Path::new("PYI019_0.pyi"))]
#[test_case(Rule::CustomTypeVarForSelf, Path::new("PYI019_1.pyi"))]
fn custom_classmethod_rules_preview(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview_{}_{}",
rule_code.noqa_code(),
path.to_string_lossy()
);
let diagnostics = test_path(
Path::new("flake8_pyi").join(path).as_path(),
&settings::LinterSettings {
pep8_naming: pep8_naming::settings::Settings {
classmethod_decorators: vec!["foo_classmethod".to_string()],
..pep8_naming::settings::Settings::default()
},
preview: PreviewMode::Enabled,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
#[test_case(Rule::TypeAliasWithoutAnnotation, Path::new("PYI026.py"))]
#[test_case(Rule::TypeAliasWithoutAnnotation, Path::new("PYI026.pyi"))]
#[test_case(Rule::RedundantNoneLiteral, Path::new("PYI061.py"))]

View file

@ -71,20 +71,8 @@ use ruff_python_ast::PythonVersion;
/// The fix is only marked as unsafe if there is the possibility that it might delete a comment
/// from your code.
///
/// ## Preview-mode behaviour
/// This rule's behaviour has several differences when [`preview`] mode is enabled:
/// 1. The fix for this rule is currently only available if `preview` mode is enabled.
/// 2. By default, this rule is only applied to methods that have return-type annotations,
/// and the range of the diagnostic is the range of the return-type annotation.
/// In preview mode, this rule is also applied to some methods that do not have
/// return-type annotations. The range of the diagnostic is the range of the function
/// header (from the end of the function name to the end of the parameters).
/// 3. In `preview` mode, the rule uses different logic to determine whether an annotation
/// refers to a type variable. The `preview`-mode logic is more accurate, but may lead
/// to more methods being flagged than if `preview` mode is disabled.
///
/// [PEP 673]: https://peps.python.org/pep-0673/#motivation
/// [PEP 695]: https://peps.python.org/pep-0695/
/// [PEP-695]: https://peps.python.org/pep-0695/
/// [PYI018]: https://docs.astral.sh/ruff/rules/unused-private-type-var/
/// [type parameter list]: https://docs.python.org/3/reference/compound_stmts.html#type-params
/// [Self]: https://docs.python.org/3/library/typing.html#typing.Self
@ -162,73 +150,33 @@ pub(crate) fn custom_type_var_instead_of_self(
&checker.settings.pep8_naming.staticmethod_decorators,
);
let function_header_end = returns
.as_deref()
.map(Ranged::end)
.unwrap_or_else(|| parameters.end());
// In stable mode, we only emit the diagnostic on methods that have a return type annotation.
// In preview mode, we have a more principled approach to determine if an annotation refers
// to a type variable, and we emit the diagnostic on some methods that do not have return
// annotations.
let (method, diagnostic_range) = match function_kind {
FunctionType::ClassMethod | FunctionType::NewMethod => {
if checker.settings.preview.is_enabled() {
(
Method::PreviewClass(PreviewClassMethod {
cls_annotation: self_or_cls_annotation,
type_params,
}),
TextRange::new(function_name.end(), function_header_end),
)
} else {
returns.as_deref().map(|returns| {
(
Method::Class(ClassMethod {
cls_annotation: self_or_cls_annotation,
returns,
type_params,
}),
returns.range(),
)
})?
}
}
FunctionType::Method => {
if checker.settings.preview.is_enabled() {
(
Method::PreviewInstance(PreviewInstanceMethod {
self_annotation: self_or_cls_annotation,
type_params,
}),
TextRange::new(function_name.end(), function_header_end),
)
} else {
returns.as_deref().map(|returns| {
(
Method::Instance(InstanceMethod {
self_annotation: self_or_cls_annotation,
returns,
type_params,
}),
returns.range(),
)
})?
}
}
let method = match function_kind {
FunctionType::ClassMethod | FunctionType::NewMethod => Method::Class(ClassMethod {
cls_annotation: self_or_cls_annotation,
type_params,
}),
FunctionType::Method => Method::Instance(InstanceMethod {
self_annotation: self_or_cls_annotation,
type_params,
}),
FunctionType::Function | FunctionType::StaticMethod => return None,
};
let custom_typevar = method.custom_typevar(semantic, binding.scope)?;
let function_header_end = returns
.as_deref()
.map(Ranged::end)
.unwrap_or_else(|| parameters.end());
let mut diagnostic = Diagnostic::new(
CustomTypeVarForSelf {
typevar_name: custom_typevar.name(checker.source()).to_string(),
},
diagnostic_range,
TextRange::new(function_name.end(), function_header_end),
);
diagnostic.try_set_optional_fix(|| {
diagnostic.try_set_fix(|| {
replace_custom_typevar_with_self(
checker,
function_def,
@ -244,9 +192,7 @@ pub(crate) fn custom_type_var_instead_of_self(
#[derive(Debug)]
enum Method<'a> {
Class(ClassMethod<'a>),
PreviewClass(PreviewClassMethod<'a>),
Instance(InstanceMethod<'a>),
PreviewInstance(PreviewInstanceMethod<'a>),
}
impl Method<'_> {
@ -257,9 +203,7 @@ impl Method<'_> {
) -> Option<TypeVar<'a>> {
match self {
Self::Class(class_method) => class_method.custom_typevar(semantic, scope),
Self::PreviewClass(class_method) => class_method.custom_typevar(semantic, scope),
Self::Instance(instance_method) => instance_method.custom_typevar(semantic),
Self::PreviewInstance(instance_method) => instance_method.custom_typevar(semantic),
}
}
}
@ -267,76 +211,10 @@ impl Method<'_> {
#[derive(Debug)]
struct ClassMethod<'a> {
cls_annotation: &'a ast::Expr,
returns: &'a ast::Expr,
type_params: Option<&'a ast::TypeParams>,
}
impl ClassMethod<'_> {
/// Returns `Some(typevar)` if the class method is annotated with
/// a custom `TypeVar` that is likely private.
fn custom_typevar<'a>(
&'a self,
semantic: &'a SemanticModel<'a>,
scope: ScopeId,
) -> Option<TypeVar<'a>> {
let ast::ExprSubscript {
value: cls_annotation_value,
slice: cls_annotation_typevar,
..
} = self.cls_annotation.as_subscript_expr()?;
let cls_annotation_typevar = cls_annotation_typevar.as_name_expr()?;
let cls_annotation_typevar_name = &cls_annotation_typevar.id;
let ast::ExprName { id, .. } = cls_annotation_value.as_name_expr()?;
if id != "type" {
return None;
}
if !semantic.has_builtin_binding_in_scope("type", scope) {
return None;
}
let return_annotation_typevar = match self.returns {
ast::Expr::Name(ast::ExprName { id, .. }) => id,
ast::Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
let return_annotation_typevar = slice.as_name_expr()?;
let ast::ExprName { id, .. } = value.as_name_expr()?;
if id != "type" {
return None;
}
&return_annotation_typevar.id
}
_ => return None,
};
if cls_annotation_typevar_name != return_annotation_typevar {
return None;
}
if !is_likely_private_typevar(cls_annotation_typevar_name, self.type_params) {
return None;
}
semantic
.resolve_name(cls_annotation_typevar)
.map(|binding_id| TypeVar(semantic.binding(binding_id)))
}
}
/// Struct for implementing this rule as applied to classmethods in preview mode.
///
/// In stable mode, we only emit this diagnostic on methods that have return annotations,
/// so the stable-mode version of this struct has a `returns: &ast::Expr` field. In preview
/// mode, we also emit this diagnostic on methods that do not have return annotations, so
/// the preview-mode version of this struct does not have a `returns` field.
#[derive(Debug)]
struct PreviewClassMethod<'a> {
cls_annotation: &'a ast::Expr,
type_params: Option<&'a ast::TypeParams>,
}
impl PreviewClassMethod<'_> {
/// Returns `Some(typevar)` if the class method is annotated with
/// a custom `TypeVar` for the `cls` parameter
fn custom_typevar<'a>(
@ -360,59 +238,21 @@ impl PreviewClassMethod<'_> {
return None;
}
custom_typevar_preview(cls_annotation_typevar, self.type_params, semantic)
custom_typevar(cls_annotation_typevar, self.type_params, semantic)
}
}
#[derive(Debug)]
struct InstanceMethod<'a> {
self_annotation: &'a ast::Expr,
returns: &'a ast::Expr,
type_params: Option<&'a ast::TypeParams>,
}
impl InstanceMethod<'_> {
/// Returns `Some(typevar)` if the instance method is annotated with
/// a custom `TypeVar` that is likely private.
fn custom_typevar<'a>(&'a self, semantic: &'a SemanticModel<'a>) -> Option<TypeVar<'a>> {
let self_annotation = self.self_annotation.as_name_expr()?;
let first_arg_type = &self_annotation.id;
let ast::ExprName {
id: return_type, ..
} = self.returns.as_name_expr()?;
if first_arg_type != return_type {
return None;
}
if !is_likely_private_typevar(first_arg_type, self.type_params) {
return None;
}
semantic
.resolve_name(self_annotation)
.map(|binding_id| TypeVar(semantic.binding(binding_id)))
}
}
/// Struct for implementing this rule as applied to instance methods in preview mode.
///
/// In stable mode, we only emit this diagnostic on methods that have return annotations,
/// so the stable-mode version of this struct has a `returns: &ast::Expr` field. In preview
/// mode, we also emit this diagnostic on methods that do not have return annotations, so
/// the preview-mode version of this struct does not have a `returns` field.
#[derive(Debug)]
struct PreviewInstanceMethod<'a> {
self_annotation: &'a ast::Expr,
type_params: Option<&'a ast::TypeParams>,
}
impl PreviewInstanceMethod<'_> {
/// Returns `Some(typevar)` if the instance method is annotated with
/// a custom `TypeVar` for the `self` parameter
fn custom_typevar<'a>(&'a self, semantic: &'a SemanticModel<'a>) -> Option<TypeVar<'a>> {
custom_typevar_preview(
custom_typevar(
self.self_annotation.as_name_expr()?,
self.type_params,
semantic,
@ -420,30 +260,8 @@ impl PreviewInstanceMethod<'_> {
}
}
/// Returns `true` if the type variable is likely private.
///
/// This routine is only used if `--preview` is not enabled,
/// as it uses heuristics to determine if an annotation uses a type variable.
/// In preview mode, we apply a more principled approach.
fn is_likely_private_typevar(type_var_name: &str, type_params: Option<&ast::TypeParams>) -> bool {
// Ex) `_T`
if type_var_name.starts_with('_') {
return true;
}
// Ex) `class Foo[T]: ...`
type_params.is_some_and(|type_params| {
type_params.iter().any(|type_param| {
if let ast::TypeParam::TypeVar(ast::TypeParamTypeVar { name, .. }) = type_param {
name == type_var_name
} else {
false
}
})
})
}
/// Returns `Some(TypeVar)` if `typevar_expr` refers to a `TypeVar` binding
fn custom_typevar_preview<'a>(
fn custom_typevar<'a>(
typevar_expr: &'a ast::ExprName,
type_params: Option<&ast::TypeParams>,
semantic: &'a SemanticModel<'a>,
@ -497,11 +315,7 @@ fn replace_custom_typevar_with_self(
custom_typevar: TypeVar,
self_or_cls_parameter: &ast::ParameterWithDefault,
self_or_cls_annotation: &ast::Expr,
) -> anyhow::Result<Option<Fix>> {
if checker.settings.preview.is_disabled() {
return Ok(None);
}
) -> anyhow::Result<Fix> {
// (1) Import `Self` (if necessary)
let (import_edit, self_symbol_binding) = import_self(checker, function_def.start())?;
@ -513,9 +327,9 @@ fn replace_custom_typevar_with_self(
// (3) If it was a PEP-695 type variable, remove that `TypeVar` from the PEP-695 type-parameter list
if custom_typevar.is_pep695_typevar() {
let Some(type_params) = function_def.type_params.as_deref() else {
bail!("Should not be possible to have a type parameter without a type parameter list");
};
let type_params = function_def.type_params.as_deref().context(
"Should not be possible to have a type parameter without a type parameter list",
)?;
let deletion_edit = remove_pep695_typevar_declaration(type_params, custom_typevar)
.context("Failed to find a `TypeVar` in the type params that matches the binding")?;
other_edits.push(deletion_edit);
@ -546,11 +360,11 @@ fn replace_custom_typevar_with_self(
Applicability::Safe
};
Ok(Some(Fix::applicable_edits(
Ok(Fix::applicable_edits(
import_edit,
other_edits,
applicability,
)))
))
}
/// Attempt to create an [`Edit`] that imports `Self`.

View file

@ -1,316 +1,692 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI019_0.py:7:62: PYI019 Use `Self` instead of custom TypeVar `_S`
PYI019_0.py:7:16: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
6 | class BadClass:
7 | def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
PYI019_0.py:10:54: PYI019 Use `Self` instead of custom TypeVar `_S`
Safe fix
4 4 | _S2 = TypeVar("_S2", BadClass, GoodClass)
5 5 |
6 6 | class BadClass:
7 |- def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
7 |+ def __new__(cls, *args: str, **kwargs: int) -> Self: ... # PYI019
8 8 |
9 9 |
10 10 | def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
PYI019_0.py:10:28: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
10 | def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
PYI019_0.py:14:54: PYI019 Use `Self` instead of custom TypeVar `_S`
Safe fix
7 7 | def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
8 8 |
9 9 |
10 |- def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
10 |+ def bad_instance_method(self, arg: bytes) -> Self: ... # PYI019
11 11 |
12 12 |
13 13 | @classmethod
PYI019_0.py:14:25: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
13 | @classmethod
14 | def bad_class_method(cls: type[_S], arg: int) -> _S: ... # PYI019
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
PYI019_0.py:18:55: PYI019 Use `Self` instead of custom TypeVar `_S`
Safe fix
11 11 |
12 12 |
13 13 | @classmethod
14 |- def bad_class_method(cls: type[_S], arg: int) -> _S: ... # PYI019
14 |+ def bad_class_method(cls, arg: int) -> Self: ... # PYI019
15 15 |
16 16 |
17 17 | @classmethod
PYI019_0.py:18:33: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
17 | @classmethod
18 | def bad_posonly_class_method(cls: type[_S], /) -> _S: ... # PYI019
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
PYI019_0.py:39:63: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
15 15 |
16 16 |
17 17 | @classmethod
18 |- def bad_posonly_class_method(cls: type[_S], /) -> _S: ... # PYI019
18 |+ def bad_posonly_class_method(cls, /) -> Self: ... # PYI019
19 19 |
20 20 |
21 21 | @classmethod
PYI019_0.py:39:14: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
37 | # Python > 3.12
38 | class PEP695BadDunderNew[T]:
39 | def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:42:46: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
36 36 |
37 37 | # Python > 3.12
38 38 | class PEP695BadDunderNew[T]:
39 |- def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
39 |+ def __new__(cls, *args: Any, ** kwargs: Any) -> Self: ... # PYI019
40 40 |
41 41 |
42 42 | def generic_instance_method[S](self: S) -> S: ... # PYI019
PYI019_0.py:42:30: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
42 | def generic_instance_method[S](self: S) -> S: ... # PYI019
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:54:32: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
39 39 | def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
40 40 |
41 41 |
42 |- def generic_instance_method[S](self: S) -> S: ... # PYI019
42 |+ def generic_instance_method(self) -> Self: ... # PYI019
43 43 |
44 44 |
45 45 | class PEP695GoodDunderNew[T]:
PYI019_0.py:54:11: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
52 | # in the settings for this test:
53 | @foo_classmethod
54 | def foo[S](cls: type[S]) -> S: ... # PYI019
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:61:48: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
51 51 | # due to `foo_classmethod being listed in `pep8_naming.classmethod-decorators`
52 52 | # in the settings for this test:
53 53 | @foo_classmethod
54 |- def foo[S](cls: type[S]) -> S: ... # PYI019
54 |+ def foo(cls) -> Self: ... # PYI019
55 55 |
56 56 |
57 57 | _S695 = TypeVar("_S695", bound="PEP695Fix")
PYI019_0.py:61:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
60 | class PEP695Fix:
61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
62 |
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:63:47: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
58 58 |
59 59 |
60 60 | class PEP695Fix:
61 |- def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
61 |+ def __new__(cls) -> Self: ...
62 62 |
63 63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 64 |
PYI019_0.py:63:26: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
62 |
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
64 |
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:65:43: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
60 60 | class PEP695Fix:
61 61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
62 62 |
63 |- def __init_subclass__[S](cls: type[S]) -> S: ...
63 |+ def __init_subclass__(cls) -> Self: ...
64 64 |
65 65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 66 |
PYI019_0.py:65:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 |
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
66 |
67 | def __pos__[S](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:67:32: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
62 62 |
63 63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 64 |
65 |- def __neg__[S: PEP695Fix](self: S) -> S: ...
65 |+ def __neg__(self) -> Self: ...
66 66 |
67 67 | def __pos__[S](self: S) -> S: ...
68 68 |
PYI019_0.py:67:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 |
67 | def __pos__[S](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
68 |
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:69:53: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
64 64 |
65 65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 66 |
67 |- def __pos__[S](self: S) -> S: ...
67 |+ def __pos__(self) -> Self: ...
68 68 |
69 69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 70 |
PYI019_0.py:69:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
67 | def __pos__[S](self: S) -> S: ...
68 |
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
70 |
71 | def __sub__[S](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:71:42: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
66 66 |
67 67 | def __pos__[S](self: S) -> S: ...
68 68 |
69 |- def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
69 |+ def __add__(self, other: Self) -> Self: ...
70 70 |
71 71 | def __sub__[S](self: S, other: S) -> S: ...
72 72 |
PYI019_0.py:71:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 |
71 | def __sub__[S](self: S, other: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
72 |
73 | @classmethod
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:74:59: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
68 68 |
69 69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 70 |
71 |- def __sub__[S](self: S, other: S) -> S: ...
71 |+ def __sub__(self, other: Self) -> Self: ...
72 72 |
73 73 | @classmethod
74 74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
PYI019_0.py:74:27: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
73 | @classmethod
74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
75 |
76 | @classmethod
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:77:50: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
71 71 | def __sub__[S](self: S, other: S) -> S: ...
72 72 |
73 73 | @classmethod
74 |- def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
74 |+ def class_method_bound(cls) -> Self: ...
75 75 |
76 76 | @classmethod
77 77 | def class_method_unbound[S](cls: type[S]) -> S: ...
PYI019_0.py:77:29: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
76 | @classmethod
77 | def class_method_unbound[S](cls: type[S]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
78 |
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:79:57: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
74 74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
75 75 |
76 76 | @classmethod
77 |- def class_method_unbound[S](cls: type[S]) -> S: ...
77 |+ def class_method_unbound(cls) -> Self: ...
78 78 |
79 79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 80 |
PYI019_0.py:79:30: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
77 | def class_method_unbound[S](cls: type[S]) -> S: ...
78 |
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
80 |
81 | def instance_method_unbound[S](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:81:48: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
76 76 | @classmethod
77 77 | def class_method_unbound[S](cls: type[S]) -> S: ...
78 78 |
79 |- def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
79 |+ def instance_method_bound(self) -> Self: ...
80 80 |
81 81 | def instance_method_unbound[S](self: S) -> S: ...
82 82 |
PYI019_0.py:81:32: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 |
81 | def instance_method_unbound[S](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
82 |
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:83:90: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
78 78 |
79 79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 80 |
81 |- def instance_method_unbound[S](self: S) -> S: ...
81 |+ def instance_method_unbound(self) -> Self: ...
82 82 |
83 83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 84 |
PYI019_0.py:83:53: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
81 | def instance_method_unbound[S](self: S) -> S: ...
82 |
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
84 |
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:85:81: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
80 80 |
81 81 | def instance_method_unbound[S](self: S) -> S: ...
82 82 |
83 |- def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
83 |+ def instance_method_bound_with_another_parameter(self, other: Self) -> Self: ...
84 84 |
85 85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 86 |
PYI019_0.py:85:55: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 |
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
86 |
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:87:94: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
82 82 |
83 83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 84 |
85 |- def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
85 |+ def instance_method_unbound_with_another_parameter(self, other: Self) -> Self: ...
86 86 |
87 87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 88 |
PYI019_0.py:87:27: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 |
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
88 |
89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:89:75: PYI019 Use `Self` instead of custom TypeVar `_S695`
Safe fix
84 84 |
85 85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 86 |
87 |- def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
87 |+ def multiple_type_vars[*Ts, T](self, other: Self, /, *args: *Ts, a: T, b: list[T]) -> Self: ...
88 88 |
89 89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
90 90 |
PYI019_0.py:89:43: PYI019 [*] Use `Self` instead of custom TypeVar `_S695`
|
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 |
89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
| ^^^^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S695` with `Self`
PYI019_0.py:114:31: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
86 86 |
87 87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 88 |
89 |- def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
89 |+ def mixing_old_and_new_style_type_vars[T](self, a: T, b: T) -> Self: ...
90 90 |
91 91 |
92 92 | class InvalidButWeDoNotPanic:
PYI019_0.py:94:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
92 | class InvalidButWeDoNotPanic:
93 | @classmethod
94 | def m[S](cls: type[S], /) -> S[int]: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
95 | def n(self: S) -> S[int]: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
91 91 |
92 92 | class InvalidButWeDoNotPanic:
93 93 | @classmethod
94 |- def m[S](cls: type[S], /) -> S[int]: ...
94 |+ def m(cls, /) -> Self[int]: ...
95 95 | def n(self: S) -> S[int]: ...
96 96 |
97 97 |
PYI019_0.py:114:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
112 | class SubscriptReturnType:
113 | @classmethod
114 | def m[S](cls: type[S]) -> type[S]: ... # PYI019
| ^^^^^^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:129:34: PYI019 Use `Self` instead of custom TypeVar `_NotATypeVar`
|
127 | # but our preview-mode logic is smarter about this.
128 | class Foo:
129 | def x(self: _NotATypeVar) -> _NotATypeVar: ...
| ^^^^^^^^^^^^ PYI019
130 | @classmethod
131 | def y(self: type[_NotATypeVar]) -> _NotATypeVar: ...
|
= help: Replace TypeVar `_NotATypeVar` with `Self`
Safe fix
111 111 |
112 112 | class SubscriptReturnType:
113 113 | @classmethod
114 |- def m[S](cls: type[S]) -> type[S]: ... # PYI019
114 |+ def m(cls) -> type[Self]: ... # PYI019
115 115 |
116 116 |
117 117 | class SelfNotUsedInReturnAnnotation:
PYI019_0.py:131:40: PYI019 Use `Self` instead of custom TypeVar `_NotATypeVar`
PYI019_0.py:118:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
129 | def x(self: _NotATypeVar) -> _NotATypeVar: ...
130 | @classmethod
131 | def y(self: type[_NotATypeVar]) -> _NotATypeVar: ...
| ^^^^^^^^^^^^ PYI019
117 | class SelfNotUsedInReturnAnnotation:
118 | def m[S](self: S, other: S) -> int: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
119 | @classmethod
120 | def n[S](cls: type[S], other: S) -> int: ...
|
= help: Replace TypeVar `_NotATypeVar` with `Self`
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:140:49: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
115 115 |
116 116 |
117 117 | class SelfNotUsedInReturnAnnotation:
118 |- def m[S](self: S, other: S) -> int: ...
118 |+ def m(self, other: Self) -> int: ...
119 119 | @classmethod
120 120 | def n[S](cls: type[S], other: S) -> int: ...
121 121 |
PYI019_0.py:120:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
118 | def m[S](self: S, other: S) -> int: ...
119 | @classmethod
120 | def n[S](cls: type[S], other: S) -> int: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
117 117 | class SelfNotUsedInReturnAnnotation:
118 118 | def m[S](self: S, other: S) -> int: ...
119 119 | @classmethod
120 |- def n[S](cls: type[S], other: S) -> int: ...
120 |+ def n(cls, other: Self) -> int: ...
121 121 |
122 122 |
123 123 | class _NotATypeVar: ...
PYI019_0.py:135:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
134 | class NoReturnAnnotations:
135 | def m[S](self: S, other: S): ...
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
136 | @classmethod
137 | def n[S](cls: type[S], other: S): ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
132 132 |
133 133 |
134 134 | class NoReturnAnnotations:
135 |- def m[S](self: S, other: S): ...
135 |+ def m(self, other: Self): ...
136 136 | @classmethod
137 137 | def n[S](cls: type[S], other: S): ...
138 138 |
PYI019_0.py:137:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
135 | def m[S](self: S, other: S): ...
136 | @classmethod
137 | def n[S](cls: type[S], other: S): ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
138 |
139 | class MultipleBoundParameters:
|
= help: Replace TypeVar `S` with `Self`
Safe fix
134 134 | class NoReturnAnnotations:
135 135 | def m[S](self: S, other: S): ...
136 136 | @classmethod
137 |- def n[S](cls: type[S], other: S): ...
137 |+ def n(cls, other: Self): ...
138 138 |
139 139 | class MultipleBoundParameters:
140 140 | def m[S: int, T: int](self: S, other: T) -> S: ...
PYI019_0.py:140:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
139 | class MultipleBoundParameters:
140 | def m[S: int, T: int](self: S, other: T) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
141 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:141:63: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
137 137 | def n[S](cls: type[S], other: S): ...
138 138 |
139 139 | class MultipleBoundParameters:
140 |- def m[S: int, T: int](self: S, other: T) -> S: ...
140 |+ def m[T: int](self, other: T) -> Self: ...
141 141 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
142 142 |
143 143 | class MethodsWithBody:
PYI019_0.py:141:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
139 | class MultipleBoundParameters:
140 | def m[S: int, T: int](self: S, other: T) -> S: ...
141 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
142 |
143 | class MethodsWithBody:
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:144:36: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
138 138 |
139 139 | class MultipleBoundParameters:
140 140 | def m[S: int, T: int](self: S, other: T) -> S: ...
141 |- def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
141 |+ def n[T: (int, str)](self, other: T) -> Self: ...
142 142 |
143 143 | class MethodsWithBody:
144 144 | def m[S](self: S, other: S) -> S:
PYI019_0.py:144:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
143 | class MethodsWithBody:
144 | def m[S](self: S, other: S) -> S:
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
145 | x: S = other
146 | return x
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:149:41: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
141 141 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
142 142 |
143 143 | class MethodsWithBody:
144 |- def m[S](self: S, other: S) -> S:
145 |- x: S = other
144 |+ def m(self, other: Self) -> Self:
145 |+ x: Self = other
146 146 | return x
147 147 |
148 148 | @classmethod
PYI019_0.py:149:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
148 | @classmethod
149 | def n[S](cls: type[S], other: S) -> S:
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
150 | x: type[S] = type(other)
151 | return x()
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:154:26: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
146 146 | return x
147 147 |
148 148 | @classmethod
149 |- def n[S](cls: type[S], other: S) -> S:
150 |- x: type[S] = type(other)
149 |+ def n(cls, other: Self) -> Self:
150 |+ x: type[Self] = type(other)
151 151 | return x()
152 152 |
153 153 | class StringizedReferencesCanBeFixed:
PYI019_0.py:154:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
153 | class StringizedReferencesCanBeFixed:
154 | def m[S](self: S) -> S:
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
155 | x = cast("list[tuple[S, S]]", self)
156 | return x
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.py:159:28: PYI019 Use `Self` instead of custom TypeVar `_T`
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 ButStrangeStringizedReferencesCannotBeFixed:
159 | def m[_T](self: _T) -> _T:
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^ 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`
PYI019_0.py:164:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
163 | class DeletionsAreNotTouched:
164 | def m[S](self: S) -> S:
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
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:173:26: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
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`
|
172 | class NamesShadowingTypeVarAreNotTouched:
173 | def m[S](self: S) -> S:
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
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
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

View file

@ -1,293 +1,684 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI019_0.pyi:7:62: PYI019 Use `Self` instead of custom TypeVar `_S`
PYI019_0.pyi:7:16: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
6 | class BadClass:
7 | def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
PYI019_0.pyi:10:54: PYI019 Use `Self` instead of custom TypeVar `_S`
Safe fix
4 4 | _S2 = TypeVar("_S2", BadClass, GoodClass)
5 5 |
6 6 | class BadClass:
7 |- def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
7 |+ def __new__(cls, *args: str, **kwargs: int) -> Self: ... # PYI019
8 8 |
9 9 |
10 10 | def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
PYI019_0.pyi:10:28: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
10 | def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
PYI019_0.pyi:14:54: PYI019 Use `Self` instead of custom TypeVar `_S`
Safe fix
7 7 | def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
8 8 |
9 9 |
10 |- def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
10 |+ def bad_instance_method(self, arg: bytes) -> Self: ... # PYI019
11 11 |
12 12 |
13 13 | @classmethod
PYI019_0.pyi:14:25: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
13 | @classmethod
14 | def bad_class_method(cls: type[_S], arg: int) -> _S: ... # PYI019
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
PYI019_0.pyi:18:55: PYI019 Use `Self` instead of custom TypeVar `_S`
Safe fix
11 11 |
12 12 |
13 13 | @classmethod
14 |- def bad_class_method(cls: type[_S], arg: int) -> _S: ... # PYI019
14 |+ def bad_class_method(cls, arg: int) -> Self: ... # PYI019
15 15 |
16 16 |
17 17 | @classmethod
PYI019_0.pyi:18:33: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
17 | @classmethod
18 | def bad_posonly_class_method(cls: type[_S], /) -> _S: ... # PYI019
| ^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
PYI019_0.pyi:39:63: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
15 15 |
16 16 |
17 17 | @classmethod
18 |- def bad_posonly_class_method(cls: type[_S], /) -> _S: ... # PYI019
18 |+ def bad_posonly_class_method(cls, /) -> Self: ... # PYI019
19 19 |
20 20 |
21 21 | @classmethod
PYI019_0.pyi:39:14: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
37 | # Python > 3.12
38 | class PEP695BadDunderNew[T]:
39 | def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:42:46: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
36 36 |
37 37 | # Python > 3.12
38 38 | class PEP695BadDunderNew[T]:
39 |- def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
39 |+ def __new__(cls, *args: Any, ** kwargs: Any) -> Self: ... # PYI019
40 40 |
41 41 |
42 42 | def generic_instance_method[S](self: S) -> S: ... # PYI019
PYI019_0.pyi:42:30: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
42 | def generic_instance_method[S](self: S) -> S: ... # PYI019
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:54:32: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
39 39 | def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
40 40 |
41 41 |
42 |- def generic_instance_method[S](self: S) -> S: ... # PYI019
42 |+ def generic_instance_method(self) -> Self: ... # PYI019
43 43 |
44 44 |
45 45 | class PEP695GoodDunderNew[T]:
PYI019_0.pyi:54:11: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
52 | # in the settings for this test:
53 | @foo_classmethod
54 | def foo[S](cls: type[S]) -> S: ... # PYI019
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:61:48: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
51 51 | # due to `foo_classmethod being listed in `pep8_naming.classmethod-decorators`
52 52 | # in the settings for this test:
53 53 | @foo_classmethod
54 |- def foo[S](cls: type[S]) -> S: ... # PYI019
54 |+ def foo(cls) -> Self: ... # PYI019
55 55 |
56 56 |
57 57 | _S695 = TypeVar("_S695", bound="PEP695Fix")
PYI019_0.pyi:61:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
60 | class PEP695Fix:
61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
62 |
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:63:47: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
58 58 |
59 59 |
60 60 | class PEP695Fix:
61 |- def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
61 |+ def __new__(cls) -> Self: ...
62 62 |
63 63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 64 |
PYI019_0.pyi:63:26: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
62 |
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
64 |
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:65:43: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
60 60 | class PEP695Fix:
61 61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
62 62 |
63 |- def __init_subclass__[S](cls: type[S]) -> S: ...
63 |+ def __init_subclass__(cls) -> Self: ...
64 64 |
65 65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 66 |
PYI019_0.pyi:65:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 |
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
66 |
67 | def __pos__[S](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:67:32: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
62 62 |
63 63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 64 |
65 |- def __neg__[S: PEP695Fix](self: S) -> S: ...
65 |+ def __neg__(self) -> Self: ...
66 66 |
67 67 | def __pos__[S](self: S) -> S: ...
68 68 |
PYI019_0.pyi:67:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 |
67 | def __pos__[S](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
68 |
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:69:53: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
64 64 |
65 65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 66 |
67 |- def __pos__[S](self: S) -> S: ...
67 |+ def __pos__(self) -> Self: ...
68 68 |
69 69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 70 |
PYI019_0.pyi:69:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
67 | def __pos__[S](self: S) -> S: ...
68 |
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
70 |
71 | def __sub__[S](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:71:42: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
66 66 |
67 67 | def __pos__[S](self: S) -> S: ...
68 68 |
69 |- def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
69 |+ def __add__(self, other: Self) -> Self: ...
70 70 |
71 71 | def __sub__[S](self: S, other: S) -> S: ...
72 72 |
PYI019_0.pyi:71:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 |
71 | def __sub__[S](self: S, other: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
72 |
73 | @classmethod
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:74:59: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
68 68 |
69 69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 70 |
71 |- def __sub__[S](self: S, other: S) -> S: ...
71 |+ def __sub__(self, other: Self) -> Self: ...
72 72 |
73 73 | @classmethod
74 74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
PYI019_0.pyi:74:27: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
73 | @classmethod
74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
75 |
76 | @classmethod
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:77:50: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
71 71 | def __sub__[S](self: S, other: S) -> S: ...
72 72 |
73 73 | @classmethod
74 |- def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
74 |+ def class_method_bound(cls) -> Self: ...
75 75 |
76 76 | @classmethod
77 77 | def class_method_unbound[S](cls: type[S]) -> S: ...
PYI019_0.pyi:77:29: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
76 | @classmethod
77 | def class_method_unbound[S](cls: type[S]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
78 |
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:79:57: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
74 74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
75 75 |
76 76 | @classmethod
77 |- def class_method_unbound[S](cls: type[S]) -> S: ...
77 |+ def class_method_unbound(cls) -> Self: ...
78 78 |
79 79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 80 |
PYI019_0.pyi:79:30: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
77 | def class_method_unbound[S](cls: type[S]) -> S: ...
78 |
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
80 |
81 | def instance_method_unbound[S](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:81:48: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
76 76 | @classmethod
77 77 | def class_method_unbound[S](cls: type[S]) -> S: ...
78 78 |
79 |- def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
79 |+ def instance_method_bound(self) -> Self: ...
80 80 |
81 81 | def instance_method_unbound[S](self: S) -> S: ...
82 82 |
PYI019_0.pyi:81:32: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 |
81 | def instance_method_unbound[S](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
82 |
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:83:90: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
78 78 |
79 79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 80 |
81 |- def instance_method_unbound[S](self: S) -> S: ...
81 |+ def instance_method_unbound(self) -> Self: ...
82 82 |
83 83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 84 |
PYI019_0.pyi:83:53: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
81 | def instance_method_unbound[S](self: S) -> S: ...
82 |
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
84 |
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:85:81: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
80 80 |
81 81 | def instance_method_unbound[S](self: S) -> S: ...
82 82 |
83 |- def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
83 |+ def instance_method_bound_with_another_parameter(self, other: Self) -> Self: ...
84 84 |
85 85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 86 |
PYI019_0.pyi:85:55: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 |
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
86 |
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:87:94: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
82 82 |
83 83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 84 |
85 |- def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
85 |+ def instance_method_unbound_with_another_parameter(self, other: Self) -> Self: ...
86 86 |
87 87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 88 |
PYI019_0.pyi:87:27: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 |
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
88 |
89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:89:75: PYI019 Use `Self` instead of custom TypeVar `_S695`
Safe fix
84 84 |
85 85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 86 |
87 |- def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
87 |+ def multiple_type_vars[*Ts, T](self, other: Self, /, *args: *Ts, a: T, b: list[T]) -> Self: ...
88 88 |
89 89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
90 90 |
PYI019_0.pyi:89:43: PYI019 [*] Use `Self` instead of custom TypeVar `_S695`
|
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 |
89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
| ^^^^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S695` with `Self`
PYI019_0.pyi:114:31: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
86 86 |
87 87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 88 |
89 |- def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
89 |+ def mixing_old_and_new_style_type_vars[T](self, a: T, b: T) -> Self: ...
90 90 |
91 91 |
92 92 | class InvalidButWeDoNotPanic:
PYI019_0.pyi:94:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
92 | class InvalidButWeDoNotPanic:
93 | @classmethod
94 | def m[S](cls: type[S], /) -> S[int]: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
95 | def n(self: S) -> S[int]: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
91 91 |
92 92 | class InvalidButWeDoNotPanic:
93 93 | @classmethod
94 |- def m[S](cls: type[S], /) -> S[int]: ...
94 |+ def m(cls, /) -> Self[int]: ...
95 95 | def n(self: S) -> S[int]: ...
96 96 |
97 97 |
PYI019_0.pyi:114:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
112 | class SubscriptReturnType:
113 | @classmethod
114 | def m[S](cls: type[S]) -> type[S]: ... # PYI019
| ^^^^^^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:118:29: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
111 111 |
112 112 | class SubscriptReturnType:
113 113 | @classmethod
114 |- def m[S](cls: type[S]) -> type[S]: ... # PYI019
114 |+ def m(cls) -> type[Self]: ... # PYI019
115 115 |
116 116 |
117 117 | class PEP695TypeParameterAtTheVeryEndOfTheList:
PYI019_0.pyi:118:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
117 | class PEP695TypeParameterAtTheVeryEndOfTheList:
118 | def f[T, S](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:122:100: PYI019 Use `Self` instead of custom TypeVar `_S695`
Safe fix
115 115 |
116 116 |
117 117 | class PEP695TypeParameterAtTheVeryEndOfTheList:
118 |- def f[T, S](self: S) -> S: ...
118 |+ def f[T](self) -> Self: ...
119 119 |
120 120 |
121 121 | class PEP695Again:
PYI019_0.pyi:122:26: PYI019 [*] Use `Self` instead of custom TypeVar `_S695`
|
121 | class PEP695Again:
122 | def mixing_and_nested[T](self: _S695, a: list[_S695], b: dict[_S695, str | T | set[_S695]]) -> _S695: ...
| ^^^^^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
123 | def also_uses_s695_but_should_not_be_edited(self, v: set[tuple[_S695]]) -> _S695: ...
|
= help: Replace TypeVar `_S695` with `Self`
PYI019_0.pyi:132:10: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
119 119 |
120 120 |
121 121 | class PEP695Again:
122 |- def mixing_and_nested[T](self: _S695, a: list[_S695], b: dict[_S695, str | T | set[_S695]]) -> _S695: ...
122 |+ def mixing_and_nested[T](self, a: list[Self], b: dict[Self, str | T | set[Self]]) -> Self: ...
123 123 | def also_uses_s695_but_should_not_be_edited(self, v: set[tuple[_S695]]) -> _S695: ...
124 124 |
125 125 | @classmethod
PYI019_0.pyi:126:29: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
130 | a: T,
131 | b: tuple[S, T]
132 | ) -> S: ...
| ^ PYI019
125 | @classmethod
126 | def comment_in_fix_range[T, S](
| _____________________________^
127 | | cls: type[ # Lorem ipsum
128 | | S
129 | | ],
130 | | a: T,
131 | | b: tuple[S, T]
132 | | ) -> S: ...
| |__________^ PYI019
133 |
134 | def comment_outside_fix_range[T, S](
134 | def comment_outside_fix_range[T, S](
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:141:10: PYI019 Use `Self` instead of custom TypeVar `S`
Unsafe fix
123 123 | def also_uses_s695_but_should_not_be_edited(self, v: set[tuple[_S695]]) -> _S695: ...
124 124 |
125 125 | @classmethod
126 |- def comment_in_fix_range[T, S](
127 |- cls: type[ # Lorem ipsum
128 |- S
129 |- ],
126 |+ def comment_in_fix_range[T](
127 |+ cls,
130 128 | a: T,
131 |- b: tuple[S, T]
132 |- ) -> S: ...
129 |+ b: tuple[Self, T]
130 |+ ) -> Self: ...
133 131 |
134 132 | def comment_outside_fix_range[T, S](
135 133 | self: S,
PYI019_0.pyi:134:34: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
139 | S, T
140 | ]
141 | ) -> S: ...
| ^ PYI019
132 | ) -> S: ...
133 |
134 | def comment_outside_fix_range[T, S](
| __________________________________^
135 | | self: S,
136 | | a: T,
137 | | b: tuple[
138 | | # Lorem ipsum
139 | | S, T
140 | | ]
141 | | ) -> S: ...
| |__________^ PYI019
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:156:34: PYI019 Use `Self` instead of custom TypeVar `_NotATypeVar`
|
154 | # but our preview-mode logic is smarter about this.
155 | class Foo:
156 | def x(self: _NotATypeVar) -> _NotATypeVar: ...
| ^^^^^^^^^^^^ PYI019
157 | @classmethod
158 | def y(self: type[_NotATypeVar]) -> _NotATypeVar: ...
|
= help: Replace TypeVar `_NotATypeVar` with `Self`
Safe fix
131 131 | b: tuple[S, T]
132 132 | ) -> S: ...
133 133 |
134 |- def comment_outside_fix_range[T, S](
135 |- self: S,
134 |+ def comment_outside_fix_range[T](
135 |+ self,
136 136 | a: T,
137 137 | b: tuple[
138 138 | # Lorem ipsum
139 |- S, T
139 |+ Self, T
140 140 | ]
141 |- ) -> S: ...
141 |+ ) -> Self: ...
142 142 |
143 143 |
144 144 | class SelfNotUsedInReturnAnnotation:
PYI019_0.pyi:158:40: PYI019 Use `Self` instead of custom TypeVar `_NotATypeVar`
PYI019_0.pyi:145:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
144 | class SelfNotUsedInReturnAnnotation:
145 | def m[S](self: S, other: S) -> int: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
146 | @classmethod
147 | def n[S](cls: type[S], other: S) -> int: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
142 142 |
143 143 |
144 144 | class SelfNotUsedInReturnAnnotation:
145 |- def m[S](self: S, other: S) -> int: ...
145 |+ def m(self, other: Self) -> int: ...
146 146 | @classmethod
147 147 | def n[S](cls: type[S], other: S) -> int: ...
148 148 |
PYI019_0.pyi:147:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
145 | def m[S](self: S, other: S) -> int: ...
146 | @classmethod
147 | def n[S](cls: type[S], other: S) -> int: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
144 144 | class SelfNotUsedInReturnAnnotation:
145 145 | def m[S](self: S, other: S) -> int: ...
146 146 | @classmethod
147 |- def n[S](cls: type[S], other: S) -> int: ...
147 |+ def n(cls, other: Self) -> int: ...
148 148 |
149 149 |
150 150 | class _NotATypeVar: ...
PYI019_0.pyi:161:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
156 | def x(self: _NotATypeVar) -> _NotATypeVar: ...
157 | @classmethod
158 | def y(self: type[_NotATypeVar]) -> _NotATypeVar: ...
| ^^^^^^^^^^^^ PYI019
159 |
160 | class NoReturnAnnotations:
161 | def m[S](self: S, other: S): ...
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
162 | @classmethod
163 | def n[S](cls: type[S], other: S): ...
|
= help: Replace TypeVar `_NotATypeVar` with `Self`
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:166:49: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
158 158 | def y(self: type[_NotATypeVar]) -> _NotATypeVar: ...
159 159 |
160 160 | class NoReturnAnnotations:
161 |- def m[S](self: S, other: S): ...
161 |+ def m(self, other: Self): ...
162 162 | @classmethod
163 163 | def n[S](cls: type[S], other: S): ...
164 164 |
PYI019_0.pyi:163:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
161 | def m[S](self: S, other: S): ...
162 | @classmethod
163 | def n[S](cls: type[S], other: S): ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
164 |
165 | class MultipleBoundParameters:
|
= help: Replace TypeVar `S` with `Self`
Safe fix
160 160 | class NoReturnAnnotations:
161 161 | def m[S](self: S, other: S): ...
162 162 | @classmethod
163 |- def n[S](cls: type[S], other: S): ...
163 |+ def n(cls, other: Self): ...
164 164 |
165 165 | class MultipleBoundParameters:
166 166 | def m[S: int, T: int](self: S, other: T) -> S: ...
PYI019_0.pyi:166:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
165 | class MultipleBoundParameters:
166 | def m[S: int, T: int](self: S, other: T) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
167 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
PYI019_0.pyi:167:63: PYI019 Use `Self` instead of custom TypeVar `S`
Safe fix
163 163 | def n[S](cls: type[S], other: S): ...
164 164 |
165 165 | class MultipleBoundParameters:
166 |- def m[S: int, T: int](self: S, other: T) -> S: ...
166 |+ def m[T: int](self, other: T) -> Self: ...
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`
|
165 | class MultipleBoundParameters:
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: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
164 164 |
165 165 | class MultipleBoundParameters:
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)](self, other: T) -> Self: ...
168 168 |
169 169 |
170 170 | MetaType = TypeVar("MetaType")

View file

@ -1,10 +1,17 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI019_1.pyi:4:26: PYI019 Use `Self` instead of custom TypeVar `S`
PYI019_1.pyi:4:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
3 | class F:
4 | def m[S](self: S) -> S: ...
| ^ PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
1 1 | import typing
2 2 |
3 3 | class F:
4 |- def m[S](self: S) -> S: ...
4 |+ def m(self) -> typing.Self: ...

View file

@ -1,692 +0,0 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI019_0.py:7:16: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
6 | class BadClass:
7 | def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
Safe fix
4 4 | _S2 = TypeVar("_S2", BadClass, GoodClass)
5 5 |
6 6 | class BadClass:
7 |- def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
7 |+ def __new__(cls, *args: str, **kwargs: int) -> Self: ... # PYI019
8 8 |
9 9 |
10 10 | def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
PYI019_0.py:10:28: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
10 | def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
Safe fix
7 7 | def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
8 8 |
9 9 |
10 |- def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
10 |+ def bad_instance_method(self, arg: bytes) -> Self: ... # PYI019
11 11 |
12 12 |
13 13 | @classmethod
PYI019_0.py:14:25: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
13 | @classmethod
14 | def bad_class_method(cls: type[_S], arg: int) -> _S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
Safe fix
11 11 |
12 12 |
13 13 | @classmethod
14 |- def bad_class_method(cls: type[_S], arg: int) -> _S: ... # PYI019
14 |+ def bad_class_method(cls, arg: int) -> Self: ... # PYI019
15 15 |
16 16 |
17 17 | @classmethod
PYI019_0.py:18:33: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
17 | @classmethod
18 | def bad_posonly_class_method(cls: type[_S], /) -> _S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
Safe fix
15 15 |
16 16 |
17 17 | @classmethod
18 |- def bad_posonly_class_method(cls: type[_S], /) -> _S: ... # PYI019
18 |+ def bad_posonly_class_method(cls, /) -> Self: ... # PYI019
19 19 |
20 20 |
21 21 | @classmethod
PYI019_0.py:39:14: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
37 | # Python > 3.12
38 | class PEP695BadDunderNew[T]:
39 | def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
36 36 |
37 37 | # Python > 3.12
38 38 | class PEP695BadDunderNew[T]:
39 |- def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
39 |+ def __new__(cls, *args: Any, ** kwargs: Any) -> Self: ... # PYI019
40 40 |
41 41 |
42 42 | def generic_instance_method[S](self: S) -> S: ... # PYI019
PYI019_0.py:42:30: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
42 | def generic_instance_method[S](self: S) -> S: ... # PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
39 39 | def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
40 40 |
41 41 |
42 |- def generic_instance_method[S](self: S) -> S: ... # PYI019
42 |+ def generic_instance_method(self) -> Self: ... # PYI019
43 43 |
44 44 |
45 45 | class PEP695GoodDunderNew[T]:
PYI019_0.py:54:11: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
52 | # in the settings for this test:
53 | @foo_classmethod
54 | def foo[S](cls: type[S]) -> S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
51 51 | # due to `foo_classmethod being listed in `pep8_naming.classmethod-decorators`
52 52 | # in the settings for this test:
53 53 | @foo_classmethod
54 |- def foo[S](cls: type[S]) -> S: ... # PYI019
54 |+ def foo(cls) -> Self: ... # PYI019
55 55 |
56 56 |
57 57 | _S695 = TypeVar("_S695", bound="PEP695Fix")
PYI019_0.py:61:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
60 | class PEP695Fix:
61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
62 |
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
58 58 |
59 59 |
60 60 | class PEP695Fix:
61 |- def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
61 |+ def __new__(cls) -> Self: ...
62 62 |
63 63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 64 |
PYI019_0.py:63:26: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
62 |
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
64 |
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
60 60 | class PEP695Fix:
61 61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
62 62 |
63 |- def __init_subclass__[S](cls: type[S]) -> S: ...
63 |+ def __init_subclass__(cls) -> Self: ...
64 64 |
65 65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 66 |
PYI019_0.py:65:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 |
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
66 |
67 | def __pos__[S](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
62 62 |
63 63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 64 |
65 |- def __neg__[S: PEP695Fix](self: S) -> S: ...
65 |+ def __neg__(self) -> Self: ...
66 66 |
67 67 | def __pos__[S](self: S) -> S: ...
68 68 |
PYI019_0.py:67:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 |
67 | def __pos__[S](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^ PYI019
68 |
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
64 64 |
65 65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 66 |
67 |- def __pos__[S](self: S) -> S: ...
67 |+ def __pos__(self) -> Self: ...
68 68 |
69 69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 70 |
PYI019_0.py:69:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
67 | def __pos__[S](self: S) -> S: ...
68 |
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
70 |
71 | def __sub__[S](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
66 66 |
67 67 | def __pos__[S](self: S) -> S: ...
68 68 |
69 |- def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
69 |+ def __add__(self, other: Self) -> Self: ...
70 70 |
71 71 | def __sub__[S](self: S, other: S) -> S: ...
72 72 |
PYI019_0.py:71:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 |
71 | def __sub__[S](self: S, other: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
72 |
73 | @classmethod
|
= help: Replace TypeVar `S` with `Self`
Safe fix
68 68 |
69 69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 70 |
71 |- def __sub__[S](self: S, other: S) -> S: ...
71 |+ def __sub__(self, other: Self) -> Self: ...
72 72 |
73 73 | @classmethod
74 74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
PYI019_0.py:74:27: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
73 | @classmethod
74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
75 |
76 | @classmethod
|
= help: Replace TypeVar `S` with `Self`
Safe fix
71 71 | def __sub__[S](self: S, other: S) -> S: ...
72 72 |
73 73 | @classmethod
74 |- def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
74 |+ def class_method_bound(cls) -> Self: ...
75 75 |
76 76 | @classmethod
77 77 | def class_method_unbound[S](cls: type[S]) -> S: ...
PYI019_0.py:77:29: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
76 | @classmethod
77 | def class_method_unbound[S](cls: type[S]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
78 |
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
74 74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
75 75 |
76 76 | @classmethod
77 |- def class_method_unbound[S](cls: type[S]) -> S: ...
77 |+ def class_method_unbound(cls) -> Self: ...
78 78 |
79 79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 80 |
PYI019_0.py:79:30: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
77 | def class_method_unbound[S](cls: type[S]) -> S: ...
78 |
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
80 |
81 | def instance_method_unbound[S](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
76 76 | @classmethod
77 77 | def class_method_unbound[S](cls: type[S]) -> S: ...
78 78 |
79 |- def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
79 |+ def instance_method_bound(self) -> Self: ...
80 80 |
81 81 | def instance_method_unbound[S](self: S) -> S: ...
82 82 |
PYI019_0.py:81:32: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 |
81 | def instance_method_unbound[S](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^ PYI019
82 |
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
78 78 |
79 79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 80 |
81 |- def instance_method_unbound[S](self: S) -> S: ...
81 |+ def instance_method_unbound(self) -> Self: ...
82 82 |
83 83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 84 |
PYI019_0.py:83:53: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
81 | def instance_method_unbound[S](self: S) -> S: ...
82 |
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
84 |
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
80 80 |
81 81 | def instance_method_unbound[S](self: S) -> S: ...
82 82 |
83 |- def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
83 |+ def instance_method_bound_with_another_parameter(self, other: Self) -> Self: ...
84 84 |
85 85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 86 |
PYI019_0.py:85:55: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 |
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
86 |
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
82 82 |
83 83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 84 |
85 |- def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
85 |+ def instance_method_unbound_with_another_parameter(self, other: Self) -> Self: ...
86 86 |
87 87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 88 |
PYI019_0.py:87:27: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 |
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
88 |
89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
84 84 |
85 85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 86 |
87 |- def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
87 |+ def multiple_type_vars[*Ts, T](self, other: Self, /, *args: *Ts, a: T, b: list[T]) -> Self: ...
88 88 |
89 89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
90 90 |
PYI019_0.py:89:43: PYI019 [*] Use `Self` instead of custom TypeVar `_S695`
|
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 |
89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S695` with `Self`
Safe fix
86 86 |
87 87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 88 |
89 |- def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
89 |+ def mixing_old_and_new_style_type_vars[T](self, a: T, b: T) -> Self: ...
90 90 |
91 91 |
92 92 | class InvalidButWeDoNotPanic:
PYI019_0.py:94:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
92 | class InvalidButWeDoNotPanic:
93 | @classmethod
94 | def m[S](cls: type[S], /) -> S[int]: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
95 | def n(self: S) -> S[int]: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
91 91 |
92 92 | class InvalidButWeDoNotPanic:
93 93 | @classmethod
94 |- def m[S](cls: type[S], /) -> S[int]: ...
94 |+ def m(cls, /) -> Self[int]: ...
95 95 | def n(self: S) -> S[int]: ...
96 96 |
97 97 |
PYI019_0.py:114:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
112 | class SubscriptReturnType:
113 | @classmethod
114 | def m[S](cls: type[S]) -> type[S]: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
111 111 |
112 112 | class SubscriptReturnType:
113 113 | @classmethod
114 |- def m[S](cls: type[S]) -> type[S]: ... # PYI019
114 |+ def m(cls) -> type[Self]: ... # PYI019
115 115 |
116 116 |
117 117 | class SelfNotUsedInReturnAnnotation:
PYI019_0.py:118:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
117 | class SelfNotUsedInReturnAnnotation:
118 | def m[S](self: S, other: S) -> int: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
119 | @classmethod
120 | def n[S](cls: type[S], other: S) -> int: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
115 115 |
116 116 |
117 117 | class SelfNotUsedInReturnAnnotation:
118 |- def m[S](self: S, other: S) -> int: ...
118 |+ def m(self, other: Self) -> int: ...
119 119 | @classmethod
120 120 | def n[S](cls: type[S], other: S) -> int: ...
121 121 |
PYI019_0.py:120:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
118 | def m[S](self: S, other: S) -> int: ...
119 | @classmethod
120 | def n[S](cls: type[S], other: S) -> int: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
117 117 | class SelfNotUsedInReturnAnnotation:
118 118 | def m[S](self: S, other: S) -> int: ...
119 119 | @classmethod
120 |- def n[S](cls: type[S], other: S) -> int: ...
120 |+ def n(cls, other: Self) -> int: ...
121 121 |
122 122 |
123 123 | class _NotATypeVar: ...
PYI019_0.py:135:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
134 | class NoReturnAnnotations:
135 | def m[S](self: S, other: S): ...
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
136 | @classmethod
137 | def n[S](cls: type[S], other: S): ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
132 132 |
133 133 |
134 134 | class NoReturnAnnotations:
135 |- def m[S](self: S, other: S): ...
135 |+ def m(self, other: Self): ...
136 136 | @classmethod
137 137 | def n[S](cls: type[S], other: S): ...
138 138 |
PYI019_0.py:137:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
135 | def m[S](self: S, other: S): ...
136 | @classmethod
137 | def n[S](cls: type[S], other: S): ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
138 |
139 | class MultipleBoundParameters:
|
= help: Replace TypeVar `S` with `Self`
Safe fix
134 134 | class NoReturnAnnotations:
135 135 | def m[S](self: S, other: S): ...
136 136 | @classmethod
137 |- def n[S](cls: type[S], other: S): ...
137 |+ def n(cls, other: Self): ...
138 138 |
139 139 | class MultipleBoundParameters:
140 140 | def m[S: int, T: int](self: S, other: T) -> S: ...
PYI019_0.py:140:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
139 | class MultipleBoundParameters:
140 | def m[S: int, T: int](self: S, other: T) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
141 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
137 137 | def n[S](cls: type[S], other: S): ...
138 138 |
139 139 | class MultipleBoundParameters:
140 |- def m[S: int, T: int](self: S, other: T) -> S: ...
140 |+ def m[T: int](self, other: T) -> Self: ...
141 141 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
142 142 |
143 143 | class MethodsWithBody:
PYI019_0.py:141:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
139 | class MultipleBoundParameters:
140 | def m[S: int, T: int](self: S, other: T) -> S: ...
141 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
142 |
143 | class MethodsWithBody:
|
= help: Replace TypeVar `S` with `Self`
Safe fix
138 138 |
139 139 | class MultipleBoundParameters:
140 140 | def m[S: int, T: int](self: S, other: T) -> S: ...
141 |- def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
141 |+ def n[T: (int, str)](self, other: T) -> Self: ...
142 142 |
143 143 | class MethodsWithBody:
144 144 | def m[S](self: S, other: S) -> S:
PYI019_0.py:144:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
143 | class MethodsWithBody:
144 | def m[S](self: S, other: S) -> S:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
145 | x: S = other
146 | return x
|
= help: Replace TypeVar `S` with `Self`
Safe fix
141 141 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
142 142 |
143 143 | class MethodsWithBody:
144 |- def m[S](self: S, other: S) -> S:
145 |- x: S = other
144 |+ def m(self, other: Self) -> Self:
145 |+ x: Self = other
146 146 | return x
147 147 |
148 148 | @classmethod
PYI019_0.py:149:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
148 | @classmethod
149 | def n[S](cls: type[S], other: S) -> S:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
150 | x: type[S] = type(other)
151 | return x()
|
= help: Replace TypeVar `S` with `Self`
Safe fix
146 146 | return x
147 147 |
148 148 | @classmethod
149 |- def n[S](cls: type[S], other: S) -> S:
150 |- x: type[S] = type(other)
149 |+ def n(cls, other: Self) -> Self:
150 |+ x: type[Self] = type(other)
151 151 | return x()
152 152 |
153 153 | class StringizedReferencesCanBeFixed:
PYI019_0.py:154:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
153 | class StringizedReferencesCanBeFixed:
154 | def m[S](self: S) -> S:
| ^^^^^^^^^^^^^^^^^ PYI019
155 | x = cast("list[tuple[S, S]]", self)
156 | return x
|
= help: Replace TypeVar `S` with `Self`
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 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
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
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`
|
172 | class NamesShadowingTypeVarAreNotTouched:
173 | def m[S](self: S) -> S:
| ^^^^^^^^^^^^^^^^^ PYI019
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
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

View file

@ -1,684 +0,0 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI019_0.pyi:7:16: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
6 | class BadClass:
7 | def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
Safe fix
4 4 | _S2 = TypeVar("_S2", BadClass, GoodClass)
5 5 |
6 6 | class BadClass:
7 |- def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
7 |+ def __new__(cls, *args: str, **kwargs: int) -> Self: ... # PYI019
8 8 |
9 9 |
10 10 | def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
PYI019_0.pyi:10:28: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
10 | def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
Safe fix
7 7 | def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ... # PYI019
8 8 |
9 9 |
10 |- def bad_instance_method(self: _S, arg: bytes) -> _S: ... # PYI019
10 |+ def bad_instance_method(self, arg: bytes) -> Self: ... # PYI019
11 11 |
12 12 |
13 13 | @classmethod
PYI019_0.pyi:14:25: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
13 | @classmethod
14 | def bad_class_method(cls: type[_S], arg: int) -> _S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
Safe fix
11 11 |
12 12 |
13 13 | @classmethod
14 |- def bad_class_method(cls: type[_S], arg: int) -> _S: ... # PYI019
14 |+ def bad_class_method(cls, arg: int) -> Self: ... # PYI019
15 15 |
16 16 |
17 17 | @classmethod
PYI019_0.pyi:18:33: PYI019 [*] Use `Self` instead of custom TypeVar `_S`
|
17 | @classmethod
18 | def bad_posonly_class_method(cls: type[_S], /) -> _S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S` with `Self`
Safe fix
15 15 |
16 16 |
17 17 | @classmethod
18 |- def bad_posonly_class_method(cls: type[_S], /) -> _S: ... # PYI019
18 |+ def bad_posonly_class_method(cls, /) -> Self: ... # PYI019
19 19 |
20 20 |
21 21 | @classmethod
PYI019_0.pyi:39:14: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
37 | # Python > 3.12
38 | class PEP695BadDunderNew[T]:
39 | def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
36 36 |
37 37 | # Python > 3.12
38 38 | class PEP695BadDunderNew[T]:
39 |- def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
39 |+ def __new__(cls, *args: Any, ** kwargs: Any) -> Self: ... # PYI019
40 40 |
41 41 |
42 42 | def generic_instance_method[S](self: S) -> S: ... # PYI019
PYI019_0.pyi:42:30: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
42 | def generic_instance_method[S](self: S) -> S: ... # PYI019
| ^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
39 39 | def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ... # PYI019
40 40 |
41 41 |
42 |- def generic_instance_method[S](self: S) -> S: ... # PYI019
42 |+ def generic_instance_method(self) -> Self: ... # PYI019
43 43 |
44 44 |
45 45 | class PEP695GoodDunderNew[T]:
PYI019_0.pyi:54:11: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
52 | # in the settings for this test:
53 | @foo_classmethod
54 | def foo[S](cls: type[S]) -> S: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
51 51 | # due to `foo_classmethod being listed in `pep8_naming.classmethod-decorators`
52 52 | # in the settings for this test:
53 53 | @foo_classmethod
54 |- def foo[S](cls: type[S]) -> S: ... # PYI019
54 |+ def foo(cls) -> Self: ... # PYI019
55 55 |
56 56 |
57 57 | _S695 = TypeVar("_S695", bound="PEP695Fix")
PYI019_0.pyi:61:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
60 | class PEP695Fix:
61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
62 |
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
58 58 |
59 59 |
60 60 | class PEP695Fix:
61 |- def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
61 |+ def __new__(cls) -> Self: ...
62 62 |
63 63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 64 |
PYI019_0.pyi:63:26: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
62 |
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
64 |
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
60 60 | class PEP695Fix:
61 61 | def __new__[S: PEP695Fix](cls: type[S]) -> S: ...
62 62 |
63 |- def __init_subclass__[S](cls: type[S]) -> S: ...
63 |+ def __init_subclass__(cls) -> Self: ...
64 64 |
65 65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 66 |
PYI019_0.pyi:65:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 |
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
66 |
67 | def __pos__[S](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
62 62 |
63 63 | def __init_subclass__[S](cls: type[S]) -> S: ...
64 64 |
65 |- def __neg__[S: PEP695Fix](self: S) -> S: ...
65 |+ def __neg__(self) -> Self: ...
66 66 |
67 67 | def __pos__[S](self: S) -> S: ...
68 68 |
PYI019_0.pyi:67:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 |
67 | def __pos__[S](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^ PYI019
68 |
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
64 64 |
65 65 | def __neg__[S: PEP695Fix](self: S) -> S: ...
66 66 |
67 |- def __pos__[S](self: S) -> S: ...
67 |+ def __pos__(self) -> Self: ...
68 68 |
69 69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 70 |
PYI019_0.pyi:69:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
67 | def __pos__[S](self: S) -> S: ...
68 |
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
70 |
71 | def __sub__[S](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
66 66 |
67 67 | def __pos__[S](self: S) -> S: ...
68 68 |
69 |- def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
69 |+ def __add__(self, other: Self) -> Self: ...
70 70 |
71 71 | def __sub__[S](self: S, other: S) -> S: ...
72 72 |
PYI019_0.pyi:71:16: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 |
71 | def __sub__[S](self: S, other: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
72 |
73 | @classmethod
|
= help: Replace TypeVar `S` with `Self`
Safe fix
68 68 |
69 69 | def __add__[S: PEP695Fix](self: S, other: S) -> S: ...
70 70 |
71 |- def __sub__[S](self: S, other: S) -> S: ...
71 |+ def __sub__(self, other: Self) -> Self: ...
72 72 |
73 73 | @classmethod
74 74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
PYI019_0.pyi:74:27: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
73 | @classmethod
74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
75 |
76 | @classmethod
|
= help: Replace TypeVar `S` with `Self`
Safe fix
71 71 | def __sub__[S](self: S, other: S) -> S: ...
72 72 |
73 73 | @classmethod
74 |- def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
74 |+ def class_method_bound(cls) -> Self: ...
75 75 |
76 76 | @classmethod
77 77 | def class_method_unbound[S](cls: type[S]) -> S: ...
PYI019_0.pyi:77:29: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
76 | @classmethod
77 | def class_method_unbound[S](cls: type[S]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
78 |
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
74 74 | def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
75 75 |
76 76 | @classmethod
77 |- def class_method_unbound[S](cls: type[S]) -> S: ...
77 |+ def class_method_unbound(cls) -> Self: ...
78 78 |
79 79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 80 |
PYI019_0.pyi:79:30: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
77 | def class_method_unbound[S](cls: type[S]) -> S: ...
78 |
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
80 |
81 | def instance_method_unbound[S](self: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
76 76 | @classmethod
77 77 | def class_method_unbound[S](cls: type[S]) -> S: ...
78 78 |
79 |- def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
79 |+ def instance_method_bound(self) -> Self: ...
80 80 |
81 81 | def instance_method_unbound[S](self: S) -> S: ...
82 82 |
PYI019_0.pyi:81:32: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 |
81 | def instance_method_unbound[S](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^ PYI019
82 |
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
78 78 |
79 79 | def instance_method_bound[S: PEP695Fix](self: S) -> S: ...
80 80 |
81 |- def instance_method_unbound[S](self: S) -> S: ...
81 |+ def instance_method_unbound(self) -> Self: ...
82 82 |
83 83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 84 |
PYI019_0.pyi:83:53: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
81 | def instance_method_unbound[S](self: S) -> S: ...
82 |
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
84 |
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
80 80 |
81 81 | def instance_method_unbound[S](self: S) -> S: ...
82 82 |
83 |- def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
83 |+ def instance_method_bound_with_another_parameter(self, other: Self) -> Self: ...
84 84 |
85 85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 86 |
PYI019_0.pyi:85:55: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 |
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
86 |
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
82 82 |
83 83 | def instance_method_bound_with_another_parameter[S: PEP695Fix](self: S, other: S) -> S: ...
84 84 |
85 |- def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
85 |+ def instance_method_unbound_with_another_parameter(self, other: Self) -> Self: ...
86 86 |
87 87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 88 |
PYI019_0.pyi:87:27: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 |
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
88 |
89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
84 84 |
85 85 | def instance_method_unbound_with_another_parameter[S](self: S, other: S) -> S: ...
86 86 |
87 |- def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
87 |+ def multiple_type_vars[*Ts, T](self, other: Self, /, *args: *Ts, a: T, b: list[T]) -> Self: ...
88 88 |
89 89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
90 90 |
PYI019_0.pyi:89:43: PYI019 [*] Use `Self` instead of custom TypeVar `_S695`
|
87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 |
89 | def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `_S695` with `Self`
Safe fix
86 86 |
87 87 | def multiple_type_vars[S, *Ts, T](self: S, other: S, /, *args: *Ts, a: T, b: list[T]) -> S: ...
88 88 |
89 |- def mixing_old_and_new_style_type_vars[T](self: _S695, a: T, b: T) -> _S695: ...
89 |+ def mixing_old_and_new_style_type_vars[T](self, a: T, b: T) -> Self: ...
90 90 |
91 91 |
92 92 | class InvalidButWeDoNotPanic:
PYI019_0.pyi:94:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
92 | class InvalidButWeDoNotPanic:
93 | @classmethod
94 | def m[S](cls: type[S], /) -> S[int]: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
95 | def n(self: S) -> S[int]: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
91 91 |
92 92 | class InvalidButWeDoNotPanic:
93 93 | @classmethod
94 |- def m[S](cls: type[S], /) -> S[int]: ...
94 |+ def m(cls, /) -> Self[int]: ...
95 95 | def n(self: S) -> S[int]: ...
96 96 |
97 97 |
PYI019_0.pyi:114:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
112 | class SubscriptReturnType:
113 | @classmethod
114 | def m[S](cls: type[S]) -> type[S]: ... # PYI019
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
111 111 |
112 112 | class SubscriptReturnType:
113 113 | @classmethod
114 |- def m[S](cls: type[S]) -> type[S]: ... # PYI019
114 |+ def m(cls) -> type[Self]: ... # PYI019
115 115 |
116 116 |
117 117 | class PEP695TypeParameterAtTheVeryEndOfTheList:
PYI019_0.pyi:118:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
117 | class PEP695TypeParameterAtTheVeryEndOfTheList:
118 | def f[T, S](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
115 115 |
116 116 |
117 117 | class PEP695TypeParameterAtTheVeryEndOfTheList:
118 |- def f[T, S](self: S) -> S: ...
118 |+ def f[T](self) -> Self: ...
119 119 |
120 120 |
121 121 | class PEP695Again:
PYI019_0.pyi:122:26: PYI019 [*] Use `Self` instead of custom TypeVar `_S695`
|
121 | class PEP695Again:
122 | def mixing_and_nested[T](self: _S695, a: list[_S695], b: dict[_S695, str | T | set[_S695]]) -> _S695: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
123 | def also_uses_s695_but_should_not_be_edited(self, v: set[tuple[_S695]]) -> _S695: ...
|
= help: Replace TypeVar `_S695` with `Self`
Safe fix
119 119 |
120 120 |
121 121 | class PEP695Again:
122 |- def mixing_and_nested[T](self: _S695, a: list[_S695], b: dict[_S695, str | T | set[_S695]]) -> _S695: ...
122 |+ def mixing_and_nested[T](self, a: list[Self], b: dict[Self, str | T | set[Self]]) -> Self: ...
123 123 | def also_uses_s695_but_should_not_be_edited(self, v: set[tuple[_S695]]) -> _S695: ...
124 124 |
125 125 | @classmethod
PYI019_0.pyi:126:29: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
125 | @classmethod
126 | def comment_in_fix_range[T, S](
| _____________________________^
127 | | cls: type[ # Lorem ipsum
128 | | S
129 | | ],
130 | | a: T,
131 | | b: tuple[S, T]
132 | | ) -> S: ...
| |__________^ PYI019
133 |
134 | def comment_outside_fix_range[T, S](
|
= help: Replace TypeVar `S` with `Self`
Unsafe fix
123 123 | def also_uses_s695_but_should_not_be_edited(self, v: set[tuple[_S695]]) -> _S695: ...
124 124 |
125 125 | @classmethod
126 |- def comment_in_fix_range[T, S](
127 |- cls: type[ # Lorem ipsum
128 |- S
129 |- ],
126 |+ def comment_in_fix_range[T](
127 |+ cls,
130 128 | a: T,
131 |- b: tuple[S, T]
132 |- ) -> S: ...
129 |+ b: tuple[Self, T]
130 |+ ) -> Self: ...
133 131 |
134 132 | def comment_outside_fix_range[T, S](
135 133 | self: S,
PYI019_0.pyi:134:34: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
132 | ) -> S: ...
133 |
134 | def comment_outside_fix_range[T, S](
| __________________________________^
135 | | self: S,
136 | | a: T,
137 | | b: tuple[
138 | | # Lorem ipsum
139 | | S, T
140 | | ]
141 | | ) -> S: ...
| |__________^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
131 131 | b: tuple[S, T]
132 132 | ) -> S: ...
133 133 |
134 |- def comment_outside_fix_range[T, S](
135 |- self: S,
134 |+ def comment_outside_fix_range[T](
135 |+ self,
136 136 | a: T,
137 137 | b: tuple[
138 138 | # Lorem ipsum
139 |- S, T
139 |+ Self, T
140 140 | ]
141 |- ) -> S: ...
141 |+ ) -> Self: ...
142 142 |
143 143 |
144 144 | class SelfNotUsedInReturnAnnotation:
PYI019_0.pyi:145:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
144 | class SelfNotUsedInReturnAnnotation:
145 | def m[S](self: S, other: S) -> int: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
146 | @classmethod
147 | def n[S](cls: type[S], other: S) -> int: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
142 142 |
143 143 |
144 144 | class SelfNotUsedInReturnAnnotation:
145 |- def m[S](self: S, other: S) -> int: ...
145 |+ def m(self, other: Self) -> int: ...
146 146 | @classmethod
147 147 | def n[S](cls: type[S], other: S) -> int: ...
148 148 |
PYI019_0.pyi:147:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
145 | def m[S](self: S, other: S) -> int: ...
146 | @classmethod
147 | def n[S](cls: type[S], other: S) -> int: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
144 144 | class SelfNotUsedInReturnAnnotation:
145 145 | def m[S](self: S, other: S) -> int: ...
146 146 | @classmethod
147 |- def n[S](cls: type[S], other: S) -> int: ...
147 |+ def n(cls, other: Self) -> int: ...
148 148 |
149 149 |
150 150 | class _NotATypeVar: ...
PYI019_0.pyi:161:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
160 | class NoReturnAnnotations:
161 | def m[S](self: S, other: S): ...
| ^^^^^^^^^^^^^^^^^^^^^^ PYI019
162 | @classmethod
163 | def n[S](cls: type[S], other: S): ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
158 158 | def y(self: type[_NotATypeVar]) -> _NotATypeVar: ...
159 159 |
160 160 | class NoReturnAnnotations:
161 |- def m[S](self: S, other: S): ...
161 |+ def m(self, other: Self): ...
162 162 | @classmethod
163 163 | def n[S](cls: type[S], other: S): ...
164 164 |
PYI019_0.pyi:163:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
161 | def m[S](self: S, other: S): ...
162 | @classmethod
163 | def n[S](cls: type[S], other: S): ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
164 |
165 | class MultipleBoundParameters:
|
= help: Replace TypeVar `S` with `Self`
Safe fix
160 160 | class NoReturnAnnotations:
161 161 | def m[S](self: S, other: S): ...
162 162 | @classmethod
163 |- def n[S](cls: type[S], other: S): ...
163 |+ def n(cls, other: Self): ...
164 164 |
165 165 | class MultipleBoundParameters:
166 166 | def m[S: int, T: int](self: S, other: T) -> S: ...
PYI019_0.pyi:166:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
165 | class MultipleBoundParameters:
166 | def m[S: int, T: int](self: S, other: T) -> S: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
167 | def n[T: (int, str), S: (int, str)](self: S, other: T) -> S: ...
|
= help: Replace TypeVar `S` with `Self`
Safe fix
163 163 | def n[S](cls: type[S], other: S): ...
164 164 |
165 165 | class MultipleBoundParameters:
166 |- def m[S: int, T: int](self: S, other: T) -> S: ...
166 |+ def m[T: int](self, other: T) -> Self: ...
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`
|
165 | class MultipleBoundParameters:
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: ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
164 164 |
165 165 | class MultipleBoundParameters:
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)](self, other: T) -> Self: ...
168 168 |
169 169 |
170 170 | MetaType = TypeVar("MetaType")

View file

@ -1,17 +0,0 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI019_1.pyi:4:10: PYI019 [*] Use `Self` instead of custom TypeVar `S`
|
3 | class F:
4 | def m[S](self: S) -> S: ...
| ^^^^^^^^^^^^^^^^^ PYI019
|
= help: Replace TypeVar `S` with `Self`
Safe fix
1 1 | import typing
2 2 |
3 3 | class F:
4 |- def m[S](self: S) -> S: ...
4 |+ def m(self) -> typing.Self: ...