---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI034 [*] `__new__` methods in classes like `Bad` usually return `self` at runtime
  --> PYI034.pyi:20:9
   |
18 |     object
19 | ):  # Y040 Do not inherit from "object" explicitly, as it is redundant in Python 3
20 |     def __new__(
   |         ^^^^^^^
21 |         cls, *args: Any, **kwargs: Any
22 |     ) -> Bad: ...  # Y034 "__new__" methods usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__new__"…
   |
help: Use `Self` as return type
   |
21 |         cls, *args: Any, **kwargs: Any
   -     ) -> Bad: ...  # Y034 "__new__" methods usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__new__", e.g. "def __new__(cls, *args: Any, **kwargs: Any) -> Self: ..."
22 +     ) -> typing.Self: ...  # Y034 "__new__" methods usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__new__", e.g. "def __new__(cls, *args: Any, **kwargs: Any) -> Self: ..."
23 |     def __repr__(
   |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__enter__` methods in classes like `Bad` usually return `self` at runtime
  --> PYI034.pyi:35:9
   |
33 |         self, other: typing.Any
34 |     ) -> typing.Any: ...  # Y032 Prefer "object" to "Any" for the second parameter in "__ne__" methods
35 |     def __enter__(
   |         ^^^^^^^^^
36 |         self,
37 |     ) -> Bad: ...  # Y034 "__enter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extension…
   |
help: Use `Self` as return type
   |
36 |         self,
   -     ) -> Bad: ...  # Y034 "__enter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__enter__", e.g. "def __enter__(self) -> Self: ..."
37 +     ) -> typing.Self: ...  # Y034 "__enter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__enter__", e.g. "def __enter__(self) -> Self: ..."
38 |     async def __aenter__(
   |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__aenter__` methods in classes like `Bad` usually return `self` at runtime
  --> PYI034.pyi:38:15
   |
36 |         self,
37 |     ) -> Bad: ...  # Y034 "__enter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extension…
38 |     async def __aenter__(
   |               ^^^^^^^^^^
39 |         self,
40 |     ) -> Bad: ...  # Y034 "__aenter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensio…
   |
help: Use `Self` as return type
   |
39 |         self,
   -     ) -> Bad: ...  # Y034 "__aenter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__aenter__", e.g. "async def __aenter__(self) -> Self: ..."
40 +     ) -> typing.Self: ...  # Y034 "__aenter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__aenter__", e.g. "async def __aenter__(self) -> Self: ..."
41 |     def __iadd__(
   |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iadd__` methods in classes like `Bad` usually return `self` at runtime
  --> PYI034.pyi:41:9
   |
39 |         self,
40 |     ) -> Bad: ...  # Y034 "__aenter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensio…
41 |     def __iadd__(
   |         ^^^^^^^^
42 |         self, other: Bad
43 |     ) -> Bad: ...  # Y034 "__iadd__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions…
   |
help: Use `Self` as return type
   |
42 |         self, other: Bad
   -     ) -> Bad: ...  # Y034 "__iadd__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__iadd__", e.g. "def __iadd__(self, other: Bad) -> Self: ..."
43 +     ) -> typing.Self: ...  # Y034 "__iadd__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions.Self" in "Bad.__iadd__", e.g. "def __iadd__(self, other: Bad) -> Self: ..."
44 |
   |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iter__` methods in classes like `BadIterator1` usually return `self` at runtime
   --> PYI034.pyi:104:9
    |
103 | class BadIterator1(Iterator[int]):
104 |     def __iter__(
    |         ^^^^^^^^
105 |         self,
106 |     ) -> Iterator[
    |
help: Use `Self` as return type
    |
105 |         self,
    -     ) -> Iterator[
    -         int
    -     ]: ...  # Y034 "__iter__" methods in classes like "BadIterator1" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadIterator1.__iter__", e.g. "def __iter__(self) -> Self: ..."
106 +     ) -> typing.Self: ...  # Y034 "__iter__" methods in classes like "BadIterator1" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadIterator1.__iter__", e.g. "def __iter__(self) -> Self: ..."
107 |
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iter__` methods in classes like `BadIterator2` usually return `self` at runtime
   --> PYI034.pyi:113:9
    |
111 |     typing.Iterator[int]
112 | ):  # Y022 Use "collections.abc.Iterator[T]" instead of "typing.Iterator[T]" (PEP 585 syntax)
113 |     def __iter__(
    |         ^^^^^^^^
114 |         self,
115 |     ) -> Iterator[
    |
help: Use `Self` as return type
    |
114 |         self,
    -     ) -> Iterator[
    -         int
    -     ]: ...  # Y034 "__iter__" methods in classes like "BadIterator2" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadIterator2.__iter__", e.g. "def __iter__(self) -> Self: ..."
115 +     ) -> typing.Self: ...  # Y034 "__iter__" methods in classes like "BadIterator2" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadIterator2.__iter__", e.g. "def __iter__(self) -> Self: ..."
116 |
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iter__` methods in classes like `BadIterator3` usually return `self` at runtime
   --> PYI034.pyi:122:9
    |
120 |     typing.Iterator[int]
121 | ):  # Y022 Use "collections.abc.Iterator[T]" instead of "typing.Iterator[T]" (PEP 585 syntax)
122 |     def __iter__(
    |         ^^^^^^^^
123 |         self,
124 |     ) -> collections.abc.Iterator[
    |
help: Use `Self` as return type
    |
123 |         self,
    -     ) -> collections.abc.Iterator[
    -         int
    -     ]: ...  # Y034 "__iter__" methods in classes like "BadIterator3" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadIterator3.__iter__", e.g. "def __iter__(self) -> Self: ..."
124 +     ) -> typing.Self: ...  # Y034 "__iter__" methods in classes like "BadIterator3" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadIterator3.__iter__", e.g. "def __iter__(self) -> Self: ..."
125 |
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iter__` methods in classes like `BadIterator4` usually return `self` at runtime
   --> PYI034.pyi:130:9
    |
128 | class BadIterator4(Iterator[int]):
129 |     # Note: *Iterable*, not *Iterator*, returned!
130 |     def __iter__(
    |         ^^^^^^^^
131 |         self,
132 |     ) -> Iterable[
    |
help: Use `Self` as return type
    |
131 |         self,
    -     ) -> Iterable[
    -         int
    -     ]: ...  # Y034 "__iter__" methods in classes like "BadIterator4" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadIterator4.__iter__", e.g. "def __iter__(self) -> Self: ..."
132 +     ) -> typing.Self: ...  # Y034 "__iter__" methods in classes like "BadIterator4" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadIterator4.__iter__", e.g. "def __iter__(self) -> Self: ..."
133 |
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__aiter__` methods in classes like `BadAsyncIterator` usually return `self` at runtime
   --> PYI034.pyi:144:9
    |
143 | class BadAsyncIterator(collections.abc.AsyncIterator[str]):
144 |     def __aiter__(
    |         ^^^^^^^^^
145 |         self,
146 |     ) -> typing.AsyncIterator[
    |
help: Use `Self` as return type
    |
145 |         self,
    -     ) -> typing.AsyncIterator[
    -         str
    -     ]: ...  # Y034 "__aiter__" methods in classes like "BadAsyncIterator" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadAsyncIterator.__aiter__", e.g. "def __aiter__(self) -> Self: ..."  # Y022 Use "collections.abc.AsyncIterator[T]" instead of "typing.AsyncIterator[T]" (PEP 585 syntax)
146 +     ) -> typing.Self: ...  # Y034 "__aiter__" methods in classes like "BadAsyncIterator" usually return "self" at runtime. Consider using "typing_extensions.Self" in "BadAsyncIterator.__aiter__", e.g. "def __aiter__(self) -> Self: ..."  # Y022 Use "collections.abc.AsyncIterator[T]" instead of "typing.AsyncIterator[T]" (PEP 585 syntax)
147 |
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__new__` methods in classes like `NonGeneric1` usually return `self` at runtime
   --> PYI034.pyi:221:9
    |
220 | class NonGeneric1(tuple):
221 |     def __new__(cls: type[NonGeneric1], *args, **kwargs) -> NonGeneric1: ...
    |         ^^^^^^^
222 |     def __enter__(self: NonGeneric1) -> NonGeneric1: ...
    |
help: Use `Self` as return type
    |
220 | class NonGeneric1(tuple):
    -     def __new__(cls: type[NonGeneric1], *args, **kwargs) -> NonGeneric1: ...
221 +     def __new__(cls, *args, **kwargs) -> typing.Self: ...
222 |     def __enter__(self: NonGeneric1) -> NonGeneric1: ...
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__enter__` methods in classes like `NonGeneric1` usually return `self` at runtime
   --> PYI034.pyi:222:9
    |
220 | class NonGeneric1(tuple):
221 |     def __new__(cls: type[NonGeneric1], *args, **kwargs) -> NonGeneric1: ...
222 |     def __enter__(self: NonGeneric1) -> NonGeneric1: ...
    |         ^^^^^^^^^
223 |
224 | class NonGeneric2(tuple):
    |
help: Use `Self` as return type
    |
221 |     def __new__(cls: type[NonGeneric1], *args, **kwargs) -> NonGeneric1: ...
    -     def __enter__(self: NonGeneric1) -> NonGeneric1: ...
222 +     def __enter__(self) -> typing.Self: ...
223 |
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__new__` methods in classes like `NonGeneric2` usually return `self` at runtime
   --> PYI034.pyi:225:9
    |
224 | class NonGeneric2(tuple):
225 |     def __new__(cls: Type[NonGeneric2]) -> NonGeneric2: ...
    |         ^^^^^^^
226 |
227 | class Generic1[T](list):
    |
help: Use `Self` as return type
    |
224 | class NonGeneric2(tuple):
    -     def __new__(cls: Type[NonGeneric2]) -> NonGeneric2: ...
225 +     def __new__(cls) -> typing.Self: ...
226 |
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__new__` methods in classes like `Generic1` usually return `self` at runtime
   --> PYI034.pyi:228:9
    |
227 | class Generic1[T](list):
228 |     def __new__(cls: type[Generic1]) -> Generic1: ...
    |         ^^^^^^^
229 |     def __enter__(self: Generic1) -> Generic1: ...
    |
help: Use `Self` as return type
    |
227 | class Generic1[T](list):
    -     def __new__(cls: type[Generic1]) -> Generic1: ...
228 +     def __new__(cls) -> typing.Self: ...
229 |     def __enter__(self: Generic1) -> Generic1: ...
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__enter__` methods in classes like `Generic1` usually return `self` at runtime
   --> PYI034.pyi:229:9
    |
227 | class Generic1[T](list):
228 |     def __new__(cls: type[Generic1]) -> Generic1: ...
229 |     def __enter__(self: Generic1) -> Generic1: ...
    |         ^^^^^^^^^
    |
help: Use `Self` as return type
    |
228 |     def __new__(cls: type[Generic1]) -> Generic1: ...
    -     def __enter__(self: Generic1) -> Generic1: ...
229 +     def __enter__(self) -> typing.Self: ...
230 |
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__new__` methods in classes like `Generic2` usually return `self` at runtime
   --> PYI034.pyi:239:9
    |
238 | class Generic2(Generic[T]):
239 |     def __new__(cls: type[Generic2]) -> Generic2: ...
    |         ^^^^^^^
240 |     def __enter__(self: Generic2) -> Generic2: ...
    |
help: Use `Self` as return type
    |
238 | class Generic2(Generic[T]):
    -     def __new__(cls: type[Generic2]) -> Generic2: ...
239 +     def __new__(cls) -> typing.Self: ...
240 |     def __enter__(self: Generic2) -> Generic2: ...
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__enter__` methods in classes like `Generic2` usually return `self` at runtime
   --> PYI034.pyi:240:9
    |
238 | class Generic2(Generic[T]):
239 |     def __new__(cls: type[Generic2]) -> Generic2: ...
240 |     def __enter__(self: Generic2) -> Generic2: ...
    |         ^^^^^^^^^
241 |
242 | class Generic3(tuple[*Ts]):
    |
help: Use `Self` as return type
    |
239 |     def __new__(cls: type[Generic2]) -> Generic2: ...
    -     def __enter__(self: Generic2) -> Generic2: ...
240 +     def __enter__(self) -> typing.Self: ...
241 |
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__new__` methods in classes like `Generic3` usually return `self` at runtime
   --> PYI034.pyi:243:9
    |
242 | class Generic3(tuple[*Ts]):
243 |     def __new__(cls: type[Generic3]) -> Generic3: ...
    |         ^^^^^^^
244 |     def __enter__(self: Generic3) -> Generic3: ...
    |
help: Use `Self` as return type
    |
242 | class Generic3(tuple[*Ts]):
    -     def __new__(cls: type[Generic3]) -> Generic3: ...
243 +     def __new__(cls) -> typing.Self: ...
244 |     def __enter__(self: Generic3) -> Generic3: ...
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__enter__` methods in classes like `Generic3` usually return `self` at runtime
   --> PYI034.pyi:244:9
    |
242 | class Generic3(tuple[*Ts]):
243 |     def __new__(cls: type[Generic3]) -> Generic3: ...
244 |     def __enter__(self: Generic3) -> Generic3: ...
    |         ^^^^^^^^^
245 |
246 | class Generic4(collections.abc.Callable[P, ...]):
    |
help: Use `Self` as return type
    |
243 |     def __new__(cls: type[Generic3]) -> Generic3: ...
    -     def __enter__(self: Generic3) -> Generic3: ...
244 +     def __enter__(self) -> typing.Self: ...
245 |
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__new__` methods in classes like `Generic4` usually return `self` at runtime
   --> PYI034.pyi:247:9
    |
246 | class Generic4(collections.abc.Callable[P, ...]):
247 |     def __new__(cls: type[Generic4]) -> Generic4: ...
    |         ^^^^^^^
248 |     def __enter__(self: Generic4) -> Generic4: ...
    |
help: Use `Self` as return type
    |
246 | class Generic4(collections.abc.Callable[P, ...]):
    -     def __new__(cls: type[Generic4]) -> Generic4: ...
247 +     def __new__(cls) -> typing.Self: ...
248 |     def __enter__(self: Generic4) -> Generic4: ...
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__enter__` methods in classes like `Generic4` usually return `self` at runtime
   --> PYI034.pyi:248:9
    |
246 | class Generic4(collections.abc.Callable[P, ...]):
247 |     def __new__(cls: type[Generic4]) -> Generic4: ...
248 |     def __enter__(self: Generic4) -> Generic4: ...
    |         ^^^^^^^^^
249 |
250 | from some_module import PotentialTypeVar
    |
help: Use `Self` as return type
    |
247 |     def __new__(cls: type[Generic4]) -> Generic4: ...
    -     def __enter__(self: Generic4) -> Generic4: ...
248 +     def __enter__(self) -> typing.Self: ...
249 |
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__new__` methods in classes like `Generic5` usually return `self` at runtime
   --> PYI034.pyi:253:9
    |
252 | class Generic5(list[PotentialTypeVar]):
253 |     def __new__(cls: type[Generic5]) -> Generic5: ...
    |         ^^^^^^^
254 |     def __enter__(self: Generic5) -> Generic5: ...
    |
help: Use `Self` as return type
    |
252 | class Generic5(list[PotentialTypeVar]):
    -     def __new__(cls: type[Generic5]) -> Generic5: ...
253 +     def __new__(cls) -> typing.Self: ...
254 |     def __enter__(self: Generic5) -> Generic5: ...
    |
note: This is a display-only fix and is likely to be incorrect

PYI034 [*] `__enter__` methods in classes like `Generic5` usually return `self` at runtime
   --> PYI034.pyi:254:9
    |
252 | class Generic5(list[PotentialTypeVar]):
253 |     def __new__(cls: type[Generic5]) -> Generic5: ...
254 |     def __enter__(self: Generic5) -> Generic5: ...
    |         ^^^^^^^^^
    |
help: Use `Self` as return type
    |
253 |     def __new__(cls: type[Generic5]) -> Generic5: ...
    -     def __enter__(self: Generic5) -> Generic5: ...
254 +     def __enter__(self) -> typing.Self: ...
255 |
    |
note: This is a display-only fix and is likely to be incorrect
