Implement flake8-pyi
flake8-pyi
Error codes
- [ ]
Y001Names of TypeVars, ParamSpecs and TypeVarTuples in stubs should usually start with _. This makes sure you don't accidentally expose names internal to the stub. - [ ]
Y002If test must be a simple comparison against sys.platform or sys.version_info. Stub files support simple conditionals to indicate differences between Python versions or platforms, but type checkers only understand a limited subset of Python syntax, and this warning triggers on conditionals that type checkers will probably not understand. - [ ]
Y003Unrecognized sys.version_info check. Similar, but triggers on some comparisons involving version checks. - [ ]
Y004Version comparison must use only major and minor version. Type checkers like mypy don't know about patch versions of Python (e.g. 3.4.3 versus 3.4.4), only major and minor versions (3.3 versus 3.4). Therefore, version checks in stubs should only use the major and minor versions. If new functionality was introduced in a patch version, pretend that it was there all along. - [ ]
Y005Version comparison must be against a length-n tuple. - [ ]
Y006Use only < and >= for version comparisons. Comparisons involving > and <= may produce unintuitive results when tools do use the full sys.version_info tuple. - [ ]
Y007Unrecognized sys.platform check. Platform checks should be simple string comparisons. - [ ]
Y008Unrecognized platform. To prevent you from typos, we warn if you use a platform name outside a small set of known platforms (e.g. "linux" and "win32"). - [ ]
Y009Empty body should contain ..., not pass. This is just a stylistic choice, but it's the one typeshed made. - [ ]
Y010Function body must contain only .... Stub files should not contain code, so function bodies should be empty. - [ ]
Y011All default values for typed function arguments must be .... Type checkers ignore the default value, so the default value is not useful information in a stub file. - [ ]
Y012Class body must not contain pass. - [ ]
Y013Non-empty class body must not contain .... - [ ]
Y014All default values for arguments must be .... A stronger version of Y011 that includes arguments without type annotations. - [ ]
Y015Attribute must not have a default value other than .... - [ ]
Y016Unions shouldn't contain duplicates, e.g. str | str is not allowed. - [ ]
Y017Stubs should not contain assignments with multiple targets or non-name targets. - [ ]
Y018A private TypeVar should be used at least once in the file in which it is defined. - [ ]
Y019Certain kinds of methods should use _typeshed.Self instead of defining custom TypeVars for their return annotation. This check currently applies for instance methods that return self, class methods that return an instance of cls, and__new__methods. - [ ]
Y020Quoted annotations should never be used in stubs. - [ ]
Y021Docstrings should not be included in stubs. - [ ]
Y022Imports linting: use typing-module aliases to stdlib objects as little as possible (e.g. builtins.list over typing.List, collections.Counter over typing.Counter, etc.). - [ ]
Y023Where there is no detriment to backwards compatibility, import objects such as ClassVar and NoReturn from typing rather than typing_extensions. - [ ]
Y024Use typing.NamedTuple instead of collections.namedtuple, as it allows for more precise type inference. - [ ]
Y025Always alias collections.abc.Set when importing it, so as to avoid confusion with builtins.set. - [ ]
Y026Type aliases should be explicitly demarcated with typing.TypeAlias. - [ ]
Y027Same as Y022. Unlike Y022, however, the imports disallowed with this error code are required if you wish to write Python 2-compatible stubs. Switch this error code off in your config file if you support Python 2. - [ ]
Y028Always use class-based syntax for typing.NamedTuple, instead of assignment-based syntax. - [ ]
Y029It is almost always redundant to define__str__or__repr__in a stub file, as the signatures are almost always identical to object.__str__and object.__repr__. - [ ]
Y030Union expressions should never have more than one Literal member, as Literal[1] | Literal[2] is semantically identical to Literal[1, 2]. - [ ]
Y031TypedDicts 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 raised.) - [ ]
Y032The second argument of an__eq__or__ne__method should usually be annotated with object rather than Any. - [ ]
Y033Do not use type comments (e.g. x = ... # type: int) in stubs, even if the stub supports Python 2. Always use annotations instead (e.g. x: int). - [ ]
Y034Y034 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 _typeshed.Self. This check looks for:
1. Any in-place BinOp dunder methods (__iadd__, __ior__, etc.) that do not return Self.
2. __new__, __enter__ and __aenter__ methods that return the class's name unparameterised.
3. __iter__ methods that return Iterator, even if the class inherits directly from Iterator.
4. __aiter__ methods that return AsyncIterator, even if the class inherits directly from AsyncIterator.
This check excludes methods decorated with @overload or @abstractmethod.
- [ ]
Y035__all__and__match_args__in a stub file should always have values, as these special variables in a .pyi file have identical semantics to__all__and__match_args__in a .py file. E.g. write__all__= ["foo", "bar"] instead of__all__: list[str]. - [ ]
Y036Y036 detects common errors in__exit__and__aexit__methods. For example, the first argument in an__exit__method should either be annotated with object or type[BaseException] | None. - [ ]
Y037Use PEP 604 syntax instead of typing.Union and typing.Optional. E.g. use str | int instead of Union[str, int], and use str | None instead of Optional[str]. - [ ]
Y038Use from collections.abc import Set as AbstractSet instead of from typing import AbstractSet. Like Y027, this error code should be switched off in your config file if your stubs support Python 2. - [ ]
Y039Use str instead of typing.Text. This error code is incompatible with stubs supporting Python 2. - [ ]
Y040Never explicitly inherit from object, as all classes implicitly inherit from object in Python 3. This error code is incompatible with stubs supporting Python 2. - [ ]
Y041Y041 detects redundant numeric unions. For example, PEP 484 specifies that type checkers should treat int as an implicit subtype of float, so int is redundant in the union int | float. In the same way, int is redundant in the union int | complex, and float is redundant in the union float | complex. - [ ]
Y042Type alias names should use CamelCase rather than snake_case - [ ]
Y043Do not use names ending in "T" for private type aliases. (The "T" suffix implies that an object is a TypeVar.) - [ ]
Y044from__future__import annotations has no effect in stub files, since type checkers automatically treat stubs as having those semantics. - [ ]
Y045__iter__methods should never return Iterable[T], as they should always return some kind of iterator. - [ ]
Y046A private Protocol should be used at least once in the file in which it is defined. - [ ]
Y047A private TypeAlias should be used at least once in the file in which it is defined. - [ ]
Y048Function bodies should contain exactly one statement. (Note that if a function body includes a docstring, the docstring counts as a "statement".) - [ ]
Y049A private TypedDict should be used at least once in the file in which it is defined. - [ ]
Y050Prefer typing_extensions.Never over typing.NoReturn for argument annotations. This is a purely stylistic choice in the name of readability. - [ ]
Y051Y051 detect redundant unions between Literal types and builtin supertypes. For example, Literal[5] is redundant in the union int | Literal[5], and Literal[True] is redundant in the union Literal[True] | bool.
Flake8-pyi currently only works with .pyi files (https://github.com/PyCQA/flake8-pyi/issues/118), it would be great if Ruff could support .py files.
Flake8-pyi author list some rules in https://github.com/qutebrowser/qutebrowser/issues/7098#issuecomment-1109959035 that can be easily apply to .py files
In addition, there are some rules that overlap with pyupgrade, such as Y037 and U007.
These are being implemented under the PYI prefix (we tend to avoid single-letter prefixes like Y, since they lead to conflicts).
Since opening this issue Y014 and Y011 have changed their behavior to allow some defaults, do you think we should mirror the new behavior?
https://github.com/PyCQA/flake8-pyi/pull/326 https://github.com/PyCQA/flake8-pyi/issues/316
@sbdchd - Yeah I'd vote to mirror their behavior.
I second @SigureMo, it would be great if some rules like Y006 would apply to *.py files as well.
I'm implementing PYI030
I am implementing PYI036
I'm implementing PYI041
I'm implementing PYI026
I'm implementing PYI017
I'm implementing PYI056
I'm implementing PYI018 and PYI046, PYI047, PYI049 (which are similar to PYI018)
I'm implementing PYI051
I'm implementing PYI019
I'm implementing PYI055
I'm implementing PYI023
Done! Thanks everyone!