Skip to content

Commit 9ee6de4

Browse files
Change Y034 rule such that it does not emit errors in protocols (#541)
Closes #539. --------- Co-authored-by: Sebastian Rittau <sebastian.rittau@zfutura.de>
1 parent e8a834b commit 9ee6de4

4 files changed

Lines changed: 9 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ flake8-pyi uses Calendar Versioning (CalVer).
1010
avoid false positives in stub files. This monkey patch has been removed.
1111
Instead, we now recommend to disable F821 when running flake8 on stub files.
1212
* Remove the now unnecessary `--no-pyi-aware-file-checker` option.
13+
* Y034 no longer triggers in protocol class definitions.
1314

1415
### New Error Codes
1516

ERRORCODES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The following warnings are currently emitted by default:
4747
| <a id="Y031" href="#Y031">Y031</a> | `TypedDict`s should use class-based syntax instead of assignment-based syntax wherever possible. (In situations where this is not possible, such as if a field is a Python keyword or an invalid identifier, this error will not be emitted.) | Style
4848
| <a id="Y032" href="#Y032">Y032</a> | The second argument of an `__eq__` or `__ne__` method should usually be annotated with `object` rather than `Any`. | Correctness
4949
| <a id="Y033" href="#Y033">Y033</a> | Do not use type comments (e.g. `x = ... # type: int`) in stubs. Always use annotations instead (e.g. `x: int`). | Style
50-
| <a id="Y034" href="#Y034">Y034</a> | Y034 detects common errors where certain methods are annotated as having a fixed return type, despite returning `self` at runtime. Such methods should be annotated with `typing_extensions.Self`. This check looks for:<br><br>&nbsp;&nbsp;**1.**&nbsp;&nbsp;Any in-place BinOp dunder methods (`__iadd__`, `__ior__`, etc.) that do not return `Self`.<br>&nbsp;&nbsp;**2.**&nbsp;&nbsp;`__new__`, `__enter__` and `__aenter__` methods that return the class's name unparameterised.<br>&nbsp;&nbsp;**3.**&nbsp;&nbsp;`__iter__` methods that return `Iterator`, even if the class inherits directly from `Iterator`.<br>&nbsp;&nbsp;**4.**&nbsp;&nbsp;`__aiter__` methods that return `AsyncIterator`, even if the class inherits directly from `AsyncIterator`.<br><br>This check excludes methods decorated with `@overload` or `@abstractmethod`. | Correctness
50+
| <a id="Y034" href="#Y034">Y034</a> | Y034 detects common errors where certain methods are annotated as having a fixed return type, despite returning `self` at runtime. Such methods should be annotated with `typing_extensions.Self`. This check looks for:<br><br>&nbsp;&nbsp;**1.**&nbsp;&nbsp;Any in-place BinOp dunder methods (`__iadd__`, `__ior__`, etc.) that do not return `Self`.<br>&nbsp;&nbsp;**2.**&nbsp;&nbsp;`__new__`, `__enter__` and `__aenter__` methods that return the class's name unparameterised.<br>&nbsp;&nbsp;**3.**&nbsp;&nbsp;`__iter__` methods that return `Iterator`, even if the class inherits directly from `Iterator`.<br>&nbsp;&nbsp;**4.**&nbsp;&nbsp;`__aiter__` methods that return `AsyncIterator`, even if the class inherits directly from `AsyncIterator`.<br><br>Note that this check excludes methods decorated with `@overload` or `@abstractmethod`, and those in the bodies of protocol classes. | Correctness
5151
| <a id="Y035" href="#Y035">Y035</a> | `__all__`, `__match_args__` and `__slots__` in a stub file should always have values, as these special variables in a `.pyi` file have identical semantics in a stub as at runtime. E.g. write `__all__ = ["foo", "bar"]` instead of `__all__: list[str]`. | Correctness
5252
| <a id="Y036" href="#Y036">Y036</a> | Y036 detects common errors in `__exit__` and `__aexit__` methods. For example, the first argument in an `__exit__` method should either be annotated with `object`, `_typeshed.Unused` (a special alias for `object`) or `type[BaseException] \| None`. | Correctness
5353
| <a id="Y037" href="#Y037">Y037</a> | Use PEP 604 syntax instead of `typing(_extensions).Union` and `typing(_extensions).Optional`. E.g. use `str \| int` instead of `Union[str, int]`, and use `str \| None` instead of `Optional[str]`. | Style

flake8_pyi/visitor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ def _has_bad_hardcoded_returns(
366366
if class_ctx.is_metaclass:
367367
return False
368368

369+
# Return false if in a protocol (class inheriting from typing.Protocol); see #539
370+
if class_ctx.is_protocol_class:
371+
return False
372+
369373
# Much too complex for our purposes to worry
370374
# about overloaded functions or abstractmethods
371375
if any(

tests/classdefs.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class InvalidButPluginDoesNotCrash:
8989
def __enter__() -> InvalidButPluginDoesNotCrash: ...
9090
async def __aenter__() -> InvalidButPluginDoesNotCrash: ...
9191

92+
class ProtocolsAreExemptedFromY034(typing.Protocol):
93+
def __iadd__(self, other: Self, /) -> object: ...
94+
9295
class BadIterator1(Iterator[int]):
9396
def __iter__(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: ..."
9497

0 commit comments

Comments
 (0)