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

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

PYI034 [*] `__aenter__` methods in classes like `Bad` usually return `self` at runtime
  --> PYI034.py:39:15
   |
37 |         ...  # Y034 "__enter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions.Self…
38 |
39 |     async def __aenter__(self) -> Bad:
   |               ^^^^^^^^^^
40 |         ...  # Y034 "__aenter__" methods in classes like "Bad" usually return "self" at runtime. Consider using "typing_extensions.Sel…
   |
help: Use `Self` as return type
   |
38 |
   -     async def __aenter__(self) -> Bad:
39 +     async def __aenter__(self) -> typing.Self:
40 |         ...  # 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: ..."
   |
note: This is an unsafe fix and may change runtime behavior

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

PYI034 [*] `__iter__` methods in classes like `BadIterator1` usually return `self` at runtime
   --> PYI034.py:165:9
    |
164 | class BadIterator1(Iterator[int]):
165 |     def __iter__(self) -> Iterator[int]:
    |         ^^^^^^^^
166 |         ...  # Y034 "__iter__" methods in classes like "BadIterator1" usually return "self" at runtime. Consider using "typing_extens…
    |
help: Use `Self` as return type
    |
164 | class BadIterator1(Iterator[int]):
    -     def __iter__(self) -> Iterator[int]:
165 +     def __iter__(self) -> typing.Self:
166 |         ...  # 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: ..."
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iter__` methods in classes like `BadIterator2` usually return `self` at runtime
   --> PYI034.py:172:9
    |
170 |     typing.Iterator[int]
171 | ):  # Y022 Use "collections.abc.Iterator[T]" instead of "typing.Iterator[T]" (PEP 585 syntax)
172 |     def __iter__(self) -> Iterator[int]:
    |         ^^^^^^^^
173 |         ...  # Y034 "__iter__" methods in classes like "BadIterator2" usually return "self" at runtime. Consider using "typing_extens…
    |
help: Use `Self` as return type
    |
171 | ):  # Y022 Use "collections.abc.Iterator[T]" instead of "typing.Iterator[T]" (PEP 585 syntax)
    -     def __iter__(self) -> Iterator[int]:
172 +     def __iter__(self) -> typing.Self:
173 |         ...  # 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: ..."
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iter__` methods in classes like `BadIterator3` usually return `self` at runtime
   --> PYI034.py:179:9
    |
177 |     typing.Iterator[int]
178 | ):  # Y022 Use "collections.abc.Iterator[T]" instead of "typing.Iterator[T]" (PEP 585 syntax)
179 |     def __iter__(self) -> collections.abc.Iterator[int]:
    |         ^^^^^^^^
180 |         ...  # Y034 "__iter__" methods in classes like "BadIterator3" usually return "self" at runtime. Consider using "typing_extens…
    |
help: Use `Self` as return type
    |
178 | ):  # Y022 Use "collections.abc.Iterator[T]" instead of "typing.Iterator[T]" (PEP 585 syntax)
    -     def __iter__(self) -> collections.abc.Iterator[int]:
179 +     def __iter__(self) -> typing.Self:
180 |         ...  # 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: ..."
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iter__` methods in classes like `BadIterator4` usually return `self` at runtime
   --> PYI034.py:185:9
    |
183 | class BadIterator4(Iterator[int]):
184 |     # Note: *Iterable*, not *Iterator*, returned!
185 |     def __iter__(self) -> Iterable[int]:
    |         ^^^^^^^^
186 |         ...  # Y034 "__iter__" methods in classes like "BadIterator4" usually return "self" at runtime. Consider using "typing_extens…
    |
help: Use `Self` as return type
    |
184 |     # Note: *Iterable*, not *Iterator*, returned!
    -     def __iter__(self) -> Iterable[int]:
185 +     def __iter__(self) -> typing.Self:
186 |         ...  # 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: ..."
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__aiter__` methods in classes like `BadAsyncIterator` usually return `self` at runtime
   --> PYI034.py:195:9
    |
194 | class BadAsyncIterator(collections.abc.AsyncIterator[str]):
195 |     def __aiter__(self) -> typing.AsyncIterator[str]:
    |         ^^^^^^^^^
196 |         ...  # Y034 "__aiter__" methods in classes like "BadAsyncIterator" usually return "self" at runtime. Consider using "typing_e…
    |
help: Use `Self` as return type
    |
194 | class BadAsyncIterator(collections.abc.AsyncIterator[str]):
    -     def __aiter__(self) -> typing.AsyncIterator[str]:
195 +     def __aiter__(self) -> typing.Self:
196 |         ...  # 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)
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iter__` methods in classes like `SubclassOfBadIterator3` usually return `self` at runtime
   --> PYI034.py:199:9
    |
198 | class SubclassOfBadIterator3(BadIterator3):
199 |     def __iter__(self) -> Iterator[int]:  # Y034
    |         ^^^^^^^^
200 |         ...
    |
help: Use `Self` as return type
    |
198 | class SubclassOfBadIterator3(BadIterator3):
    -     def __iter__(self) -> Iterator[int]:  # Y034
199 +     def __iter__(self) -> typing.Self:  # Y034
200 |         ...
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__aiter__` methods in classes like `SubclassOfBadAsyncIterator` usually return `self` at runtime
   --> PYI034.py:203:9
    |
202 | class SubclassOfBadAsyncIterator(BadAsyncIterator):
203 |     def __aiter__(self) -> collections.abc.AsyncIterator[str]:  # Y034
    |         ^^^^^^^^^
204 |         ...
    |
help: Use `Self` as return type
    |
202 | class SubclassOfBadAsyncIterator(BadAsyncIterator):
    -     def __aiter__(self) -> collections.abc.AsyncIterator[str]:  # Y034
203 +     def __aiter__(self) -> typing.Self:  # Y034
204 |         ...
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__new__` methods in classes like `NonGeneric1` usually return `self` at runtime
   --> PYI034.py:327:9
    |
326 | class NonGeneric1(tuple):
327 |     def __new__(cls: type[NonGeneric1], *args, **kwargs) -> NonGeneric1: ...
    |         ^^^^^^^
328 |     def __enter__(self: NonGeneric1) -> NonGeneric1: ...
    |
help: Use `Self` as return type
    |
326 | class NonGeneric1(tuple):
    -     def __new__(cls: type[NonGeneric1], *args, **kwargs) -> NonGeneric1: ...
327 +     def __new__(cls, *args, **kwargs) -> typing.Self: ...
328 |     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.py:328:9
    |
326 | class NonGeneric1(tuple):
327 |     def __new__(cls: type[NonGeneric1], *args, **kwargs) -> NonGeneric1: ...
328 |     def __enter__(self: NonGeneric1) -> NonGeneric1: ...
    |         ^^^^^^^^^
329 |
330 | class NonGeneric2(tuple):
    |
help: Use `Self` as return type
    |
327 |     def __new__(cls: type[NonGeneric1], *args, **kwargs) -> NonGeneric1: ...
    -     def __enter__(self: NonGeneric1) -> NonGeneric1: ...
328 +     def __enter__(self) -> typing.Self: ...
329 |
    |
note: This is an unsafe fix and may change runtime behavior

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

PYI034 [*] `__new__` methods in classes like `Generic1` usually return `self` at runtime
   --> PYI034.py:334:9
    |
333 | class Generic1[T](list):
334 |     def __new__(cls: type[Generic1]) -> Generic1: ...
    |         ^^^^^^^
335 |     def __enter__(self: Generic1) -> Generic1: ...
    |
help: Use `Self` as return type
    |
333 | class Generic1[T](list):
    -     def __new__(cls: type[Generic1]) -> Generic1: ...
334 +     def __new__(cls) -> typing.Self: ...
335 |     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.py:335:9
    |
333 | class Generic1[T](list):
334 |     def __new__(cls: type[Generic1]) -> Generic1: ...
335 |     def __enter__(self: Generic1) -> Generic1: ...
    |         ^^^^^^^^^
    |
help: Use `Self` as return type
    |
334 |     def __new__(cls: type[Generic1]) -> Generic1: ...
    -     def __enter__(self: Generic1) -> Generic1: ...
335 +     def __enter__(self) -> typing.Self: ...
336 |
    |
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.py:345:9
    |
344 | class Generic2(Generic[T]):
345 |     def __new__(cls: type[Generic2]) -> Generic2: ...
    |         ^^^^^^^
346 |     def __enter__(self: Generic2) -> Generic2: ...
    |
help: Use `Self` as return type
    |
344 | class Generic2(Generic[T]):
    -     def __new__(cls: type[Generic2]) -> Generic2: ...
345 +     def __new__(cls) -> typing.Self: ...
346 |     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.py:346:9
    |
344 | class Generic2(Generic[T]):
345 |     def __new__(cls: type[Generic2]) -> Generic2: ...
346 |     def __enter__(self: Generic2) -> Generic2: ...
    |         ^^^^^^^^^
347 |
348 | class Generic3(tuple[*Ts]):
    |
help: Use `Self` as return type
    |
345 |     def __new__(cls: type[Generic2]) -> Generic2: ...
    -     def __enter__(self: Generic2) -> Generic2: ...
346 +     def __enter__(self) -> typing.Self: ...
347 |
    |
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.py:349:9
    |
348 | class Generic3(tuple[*Ts]):
349 |     def __new__(cls: type[Generic3]) -> Generic3: ...
    |         ^^^^^^^
350 |     def __enter__(self: Generic3) -> Generic3: ...
    |
help: Use `Self` as return type
    |
348 | class Generic3(tuple[*Ts]):
    -     def __new__(cls: type[Generic3]) -> Generic3: ...
349 +     def __new__(cls) -> typing.Self: ...
350 |     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.py:350:9
    |
348 | class Generic3(tuple[*Ts]):
349 |     def __new__(cls: type[Generic3]) -> Generic3: ...
350 |     def __enter__(self: Generic3) -> Generic3: ...
    |         ^^^^^^^^^
351 |
352 | class Generic4(collections.abc.Callable[P, ...]):
    |
help: Use `Self` as return type
    |
349 |     def __new__(cls: type[Generic3]) -> Generic3: ...
    -     def __enter__(self: Generic3) -> Generic3: ...
350 +     def __enter__(self) -> typing.Self: ...
351 |
    |
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.py:353:9
    |
352 | class Generic4(collections.abc.Callable[P, ...]):
353 |     def __new__(cls: type[Generic4]) -> Generic4: ...
    |         ^^^^^^^
354 |     def __enter__(self: Generic4) -> Generic4: ...
    |
help: Use `Self` as return type
    |
352 | class Generic4(collections.abc.Callable[P, ...]):
    -     def __new__(cls: type[Generic4]) -> Generic4: ...
353 +     def __new__(cls) -> typing.Self: ...
354 |     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.py:354:9
    |
352 | class Generic4(collections.abc.Callable[P, ...]):
353 |     def __new__(cls: type[Generic4]) -> Generic4: ...
354 |     def __enter__(self: Generic4) -> Generic4: ...
    |         ^^^^^^^^^
355 |
356 | from some_module import PotentialTypeVar
    |
help: Use `Self` as return type
    |
353 |     def __new__(cls: type[Generic4]) -> Generic4: ...
    -     def __enter__(self: Generic4) -> Generic4: ...
354 +     def __enter__(self) -> typing.Self: ...
355 |
    |
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.py:359:9
    |
358 | class Generic5(list[PotentialTypeVar]):
359 |     def __new__(cls: type[Generic5]) -> Generic5: ...
    |         ^^^^^^^
360 |     def __enter__(self: Generic5) -> Generic5: ...
    |
help: Use `Self` as return type
    |
358 | class Generic5(list[PotentialTypeVar]):
    -     def __new__(cls: type[Generic5]) -> Generic5: ...
359 +     def __new__(cls) -> typing.Self: ...
360 |     def __enter__(self: Generic5) -> Generic5: ...
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__enter__` methods in classes like `Generic5` usually return `self` at runtime
   --> PYI034.py:360:9
    |
358 | class Generic5(list[PotentialTypeVar]):
359 |     def __new__(cls: type[Generic5]) -> Generic5: ...
360 |     def __enter__(self: Generic5) -> Generic5: ...
    |         ^^^^^^^^^
    |
help: Use `Self` as return type
    |
359 |     def __new__(cls: type[Generic5]) -> Generic5: ...
    -     def __enter__(self: Generic5) -> Generic5: ...
360 +     def __enter__(self) -> typing.Self: ...
361 |
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__new__` methods in classes like `UsesStringizedForwardReferences` usually return `self` at runtime
   --> PYI034.py:391:9
    |
390 | class UsesStringizedForwardReferences:
391 |     def __new__(cls) -> "UsesStringizedForwardReferences": ...       # PYI034
    |         ^^^^^^^
392 |     def __enter__(self) -> "UsesStringizedForwardReferences": ...    # PYI034
393 |     async def __aenter__(self) -> "UsesStringizedForwardReferences": ...  # PYI034
    |
help: Use `Self` as return type
    |
390 | class UsesStringizedForwardReferences:
    -     def __new__(cls) -> "UsesStringizedForwardReferences": ...       # PYI034
391 +     def __new__(cls) -> typing.Self: ...       # PYI034
392 |     def __enter__(self) -> "UsesStringizedForwardReferences": ...    # PYI034
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__enter__` methods in classes like `UsesStringizedForwardReferences` usually return `self` at runtime
   --> PYI034.py:392:9
    |
390 | class UsesStringizedForwardReferences:
391 |     def __new__(cls) -> "UsesStringizedForwardReferences": ...       # PYI034
392 |     def __enter__(self) -> "UsesStringizedForwardReferences": ...    # PYI034
    |         ^^^^^^^^^
393 |     async def __aenter__(self) -> "UsesStringizedForwardReferences": ...  # PYI034
394 |     def __iadd__(self, other) -> "UsesStringizedForwardReferences": ...  # PYI034
    |
help: Use `Self` as return type
    |
391 |     def __new__(cls) -> "UsesStringizedForwardReferences": ...       # PYI034
    -     def __enter__(self) -> "UsesStringizedForwardReferences": ...    # PYI034
392 +     def __enter__(self) -> typing.Self: ...    # PYI034
393 |     async def __aenter__(self) -> "UsesStringizedForwardReferences": ...  # PYI034
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__aenter__` methods in classes like `UsesStringizedForwardReferences` usually return `self` at runtime
   --> PYI034.py:393:15
    |
391 |     def __new__(cls) -> "UsesStringizedForwardReferences": ...       # PYI034
392 |     def __enter__(self) -> "UsesStringizedForwardReferences": ...    # PYI034
393 |     async def __aenter__(self) -> "UsesStringizedForwardReferences": ...  # PYI034
    |               ^^^^^^^^^^
394 |     def __iadd__(self, other) -> "UsesStringizedForwardReferences": ...  # PYI034
    |
help: Use `Self` as return type
    |
392 |     def __enter__(self) -> "UsesStringizedForwardReferences": ...    # PYI034
    -     async def __aenter__(self) -> "UsesStringizedForwardReferences": ...  # PYI034
393 +     async def __aenter__(self) -> typing.Self: ...  # PYI034
394 |     def __iadd__(self, other) -> "UsesStringizedForwardReferences": ...  # PYI034
    |
note: This is an unsafe fix and may change runtime behavior

PYI034 [*] `__iadd__` methods in classes like `UsesStringizedForwardReferences` usually return `self` at runtime
   --> PYI034.py:394:9
    |
392 |     def __enter__(self) -> "UsesStringizedForwardReferences": ...    # PYI034
393 |     async def __aenter__(self) -> "UsesStringizedForwardReferences": ...  # PYI034
394 |     def __iadd__(self, other) -> "UsesStringizedForwardReferences": ...  # PYI034
    |         ^^^^^^^^
    |
help: Use `Self` as return type
    |
393 |     async def __aenter__(self) -> "UsesStringizedForwardReferences": ...  # PYI034
    -     def __iadd__(self, other) -> "UsesStringizedForwardReferences": ...  # PYI034
394 +     def __iadd__(self, other) -> typing.Self: ...  # PYI034
395 |
    |
note: This is an unsafe fix and may change runtime behavior
