Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ flake8-pyi uses Calendar Versioning (CalVer).
avoid false positives in stub files. This monkey patch has been removed.
Instead, we now recommend to disable F821 when running flake8 on stub files.
* Remove the now unnecessary `--no-pyi-aware-file-checker` option.
* Y034 no longer triggers in protocol class definitions.

### New Error Codes

Expand Down
2 changes: 1 addition & 1 deletion ERRORCODES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The following warnings are currently emitted by default:
| <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
| <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
| <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
| <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
| <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
| <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
| <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
| <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
Expand Down
4 changes: 4 additions & 0 deletions flake8_pyi/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ def _has_bad_hardcoded_returns(
if class_ctx.is_metaclass:
return False

# Return false if in a protocol (class inheriting from typing.Protocol); see #539
if class_ctx.is_protocol_class:
return False

# Much too complex for our purposes to worry
# about overloaded functions or abstractmethods
if any(
Expand Down
3 changes: 3 additions & 0 deletions tests/classdefs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class InvalidButPluginDoesNotCrash:
def __enter__() -> InvalidButPluginDoesNotCrash: ...
async def __aenter__() -> InvalidButPluginDoesNotCrash: ...

class ProtocolsAreExemptedFromY034(typing.Protocol):
def __iadd__(self, other: Self, /) -> object: ...

class BadIterator1(Iterator[int]):
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: ..."

Expand Down
Loading