Skip to main content

A plugin for flake8 to enable linting .pyi files.

Project description

flake8-pyi

A plugin for Flake8 that provides specializations for type hinting stub files, especially interesting for linting typeshed.

Functionality

  1. Adds the .pyi extension to the default value of the --filename command-line argument to Flake8. This means stubs are linted by default with this plugin enabled, without needing to explicitly list every file.

  2. Modifies PyFlakes runs for .pyi files to defer checking type annotation expressions after the entire file has been read. This enables support for first-class forward references that stub files use.

  3. Provides a number of .pyi-specific warnings that enforce typeshed's style guide.

Note: Be careful when using this plugin in the same environment as other flake8 plugins, as they might generate errors that are inappropriate for .pyi files (e.g., about missing docstrings). We recommend running flake8-pyi in a dedicated environment in your CI.

List of warnings

This plugin reserves codes starting with Y0. The following warnings are currently emitted:

Code Description
Y001 Names of TypeVars, ParamSpecs and TypeVarTuples in stubs should usually start with _. This makes sure you don't accidentally expose names internal to the stub.
Y002 If 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.
Y003 Unrecognized sys.version_info check. Similar, but triggers on some comparisons involving version checks.
Y004 Version 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.
Y005 Version comparison must be against a length-n tuple.
Y006 Use only < and >= for version comparisons. Comparisons involving > and <= may produce unintuitive results when tools do use the full sys.version_info tuple.
Y007 Unrecognized sys.platform check. Platform checks should be simple string comparisons.
Y008 Unrecognized 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").
Y009 Empty body should contain ..., not pass. This is just a stylistic choice, but it's the one typeshed made.
Y010 Function body must contain only .... Stub files should not contain code, so function bodies should be empty.
Y011 All 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.
Y012 Class body must not contain pass.
Y013 Non-empty class body must not contain ....
Y014 All default values for arguments must be .... A stronger version of Y011 that includes arguments without type annotations.
Y015 Attribute must not have a default value other than ....
Y016 Unions shouldn't contain duplicates, e.g. str | str is not allowed.
Y017 Stubs should not contain assignments with multiple targets or non-name targets.
Y018 A private TypeVar should be used at least once in the file in which it is defined.
Y019 Certain 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.
Y020 Quoted annotations should never be used in stubs.
Y021 Docstrings should not be included in stubs.
Y022 Imports 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.).
Y023 Where there is no detriment to backwards compatibility, import objects such as ClassVar and NoReturn from typing rather than typing_extensions.
Y024 Use typing.NamedTuple instead of collections.namedtuple, as it allows for more precise type inference.
Y025 Always alias collections.abc.Set when importing it, so as to avoid confusion with builtins.set.
Y026 Type aliases should be explicitly demarcated with typing.TypeAlias.
Y027 Same 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.
Y028 Always use class-based syntax for typing.NamedTuple, instead of assignment-based syntax.
Y029 It 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__.
Y030 Union expressions should never have more than one Literal member, as Literal[1] | Literal[2] is semantically identical to Literal[1, 2].
Y031 TypedDicts 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.)
Y032 The second argument of an __eq__ or __ne__ method should usually be annotated with object rather than Any.
Y033 Do 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).
Y034 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 _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].
Y036 Y036 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.
Y037 Use 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].
Y038 Use 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.
Y039 Use str instead of typing.Text. This error code is incompatible with stubs supporting Python 2.
Y040 Never explicitly inherit from object, as all classes implicitly inherit from object in Python 3. This error code is incompatible with stubs supporting Python 2.

Many error codes enforce modern conventions, and some cannot yet be used in all cases:

  • Y027, Y038, Y039 and Y040 are incompatible with Python 2. These should only be used in stubs that are meant only for Python 3.
  • Y037 (enforcing PEP 604 syntax everywhere) is not yet fully compatible with the mypy type checker, which has a few bugs regarding PEP 604 type aliases.

License

MIT

Authors

Originally created by Łukasz Langa and now maintained by Jelle Zijlstra, Alex Waygood, Sebastian Rittau, Akuli, and Shantanu.

See also

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-pyi-22.5.1.tar.gz (34.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_pyi-22.5.1-py37-none-any.whl (20.9 kB view details)

Uploaded Python 3.7

File details

Details for the file flake8-pyi-22.5.1.tar.gz.

File metadata

  • Download URL: flake8-pyi-22.5.1.tar.gz
  • Upload date:
  • Size: 34.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for flake8-pyi-22.5.1.tar.gz
Algorithm Hash digest
SHA256 52152ae25efbd6329c6426a3b29a9073a696ba744502fb3e48581a47426ad4c8
MD5 b98a286ca134bb0623022d55faa899c2
BLAKE2b-256 ae6896c7e5f17e3a1d0f015ac5fc21cee85a348df658c1da299b9e48e4558d9c

See more details on using hashes here.

File details

Details for the file flake8_pyi-22.5.1-py37-none-any.whl.

File metadata

  • Download URL: flake8_pyi-22.5.1-py37-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.13

File hashes

Hashes for flake8_pyi-22.5.1-py37-none-any.whl
Algorithm Hash digest
SHA256 dfd385389f7bfed6c6666df1bc0a1a70fb03577e4e3ccd79229053d1e2854a96
MD5 093451913ce0db6ea2e84a913558a842
BLAKE2b-256 d30fabb521f99dd1c27cf663d78c450c682f67ce0d1f2d1f4dfa4f1668c05596

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page