---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
snapshot_kind: text
---
PYI019.pyi:7:62: PYI019 Methods like `__new__` should return `Self` instead of a custom `TypeVar`
  |
6 | class BadClass:
7 |     def __new__(cls: type[_S], *args: str, **kwargs: int) -> _S: ...  # PYI019
  |                                                              ^^ PYI019
  |
  = help: Replace with `Self`

ℹ Display-only 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.pyi:10:54: PYI019 Methods like `bad_instance_method` should return `Self` instead of a custom `TypeVar`
   |
10 |     def bad_instance_method(self: _S, arg: bytes) -> _S: ...  # PYI019
   |                                                      ^^ PYI019
   |
   = help: Replace with `Self`

ℹ Display-only 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.pyi:14:54: PYI019 Methods like `bad_class_method` should return `Self` instead of a custom `TypeVar`
   |
13 |     @classmethod
14 |     def bad_class_method(cls: type[_S], arg: int) -> _S: ...  # PYI019
   |                                                      ^^ PYI019
   |
   = help: Replace with `Self`

ℹ Display-only 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.pyi:18:55: PYI019 [*] Methods like `bad_posonly_class_method` should return `Self` instead of a custom `TypeVar`
   |
17 |     @classmethod
18 |     def bad_posonly_class_method(cls: type[_S], /) -> _S: ...  # PYI019
   |                                                       ^^ PYI019
   |
   = help: Replace 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.pyi:39:63: PYI019 Methods like `__new__` should return `Self` instead of a custom `TypeVar`
   |
37 | # Python > 3.12
38 | class PEP695BadDunderNew[T]:
39 |   def __new__[S](cls: type[S], *args: Any, ** kwargs: Any) -> S: ...  # PYI019
   |                                                               ^ PYI019
   |
   = help: Replace with `Self`

ℹ Display-only 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.pyi:42:46: PYI019 [*] Methods like `generic_instance_method` should return `Self` instead of a custom `TypeVar`
   |
42 |   def generic_instance_method[S](self: S) -> S: ...  # PYI019
   |                                              ^ PYI019
   |
   = help: Replace 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.pyi:54:32: PYI019 [*] Methods like `foo` should return `Self` instead of a custom `TypeVar`
   |
52 |    # in the settings for this test:
53 |    @foo_classmethod
54 |    def foo[S](cls: type[S]) -> S: ...  # PYI019
   |                                ^ PYI019
   |
   = help: Replace 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.pyi:61:48: PYI019 [*] Methods like `__new__` should return `Self` instead of a custom `TypeVar`
   |
59 | # Only .pyi gets fixes, no fixes for .py
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 with `Self`

ℹ Safe fix
58 58 | 
59 59 | # Only .pyi gets fixes, no fixes for .py
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.pyi:63:47: PYI019 [*] Methods like `__init_subclass__` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:65:43: PYI019 [*] Methods like `__neg__` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:67:32: PYI019 [*] Methods like `__pos__` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:69:53: PYI019 [*] Methods like `__add__` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:71:42: PYI019 [*] Methods like `__sub__` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:74:59: PYI019 [*] Methods like `class_method_bound` should return `Self` instead of a custom `TypeVar`
   |
73 |     @classmethod
74 |     def class_method_bound[S: PEP695Fix](cls: type[S]) -> S: ...
   |                                                           ^ PYI019
75 | 
76 |     @classmethod
   |
   = help: Replace 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.pyi:77:50: PYI019 [*] Methods like `class_method_unbound` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:79:57: PYI019 [*] Methods like `instance_method_bound` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:81:48: PYI019 [*] Methods like `instance_method_unbound` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:83:90: PYI019 [*] Methods like `instance_method_bound_with_another_parameter` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:85:81: PYI019 [*] Methods like `instance_method_unbound_with_another_parameter` should return `Self` instead of a custom `TypeVar`
   |
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 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.pyi:87:94: PYI019 Methods like `multiple_type_vars` should return `Self` instead of a custom `TypeVar`
   |
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 with `Self`

ℹ Display-only 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: ...

PYI019.pyi:89:75: PYI019 Methods like `mixing_old_and_new_style_type_vars` should return `Self` instead of a custom `TypeVar`
   |
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 with `Self`

ℹ Display-only 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: ...
