- Breaking change: Previously, flake8-pyi monkey patched flake8's F821 (undefined name) check to 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-checkeroption.
New error codes:
- Introduce Y068: Don't use
@overridein stub files.
New error codes:
- Introduce Y067: Don't use
Incomplete | None = None. - Introduce Y091: Protocol method parameters should not be positional-or-keyword.
Other changes:
- Y011/Y015 will now allow all defaults that include an attribute access,
for example
math.infor enum members. - Development-only dependencies are now declared using dependency groups rather than optional dependencies.
- The plugin now exists as a
flake8_pyipackage rather than a singlepyi.pyfile. - Declare support for Python 3.14
Bugfixes
- Don't emit Y053 for long strings inside
Literalslices or metadata strings insideAnnotatedslices.
Other changes:
flake8-pyino longer supports being run using Python 3.8. As a result, it not longer depends on the third-partyast_decompilerpackage.
Bugfixes
- Allow the use of
typing_extensions.TypeVarin stubs.typing_extensions.TypeVarhas the default parameter, which only exists on Python 3.13+ when usingtyping.TypeVar. - Reduce false positives from Y052 in relation to enum subclasses.
Other changes
- Declare support for Python 3.13
New error codes:
- Y066: When using if/else with
sys.version_info, put the code for new Python versions first.
Bugfixes:
- Fix Y026 false positive: allow simple assignment to
Nonein class scopes if the class is known to be an enum class.
New error codes:
- Y064: Use simpler syntax to define final literal types.
For example, use
x: Final = 42instead ofx: Final[Literal[42]] - Y065: Don't use bare
Incompletein parameter and return annotations.
Bugfixes:
- Y090: Fix false positive for
tuple[Unpack[Ts]].
New error codes:
- Y063: Use PEP 570 syntax to mark positional-only arguments, rather than the older Python 3.7-compatible syntax described in PEP 484.
New error codes:
- Y062: Disallow duplicate elements inside
Literal[]slices.
Other features:
- Support flake8>=7.0.0
- Y061 is no longer emitted in situations where Y062 would also be emitted.
- Improve error message for Y060.
- Y023 now bans more imports from
typing_extensionsnow that typeshed has dropped support for Python 3.7.
Bugfixes:
- Y016: Fix false positive if a method had positional-only parameters (using PEP 570 syntax and the first positional-or-keyword parameter following the positional-only parameters used a custom TypeVar (see #455).
- Y046: Fix false negative where an unused protocol would not be detected if the protocol was generic.
New error codes:
- Y058: Use
Iteratorrather thanGeneratoras the return value for simple__iter__methods, andAsyncIteratorrather thanAsyncGeneratoras the return value for simple__aiter__methods. - Y059:
Generic[]should always be the last base class, if it is present in the bases of a class. - Y060, which flags redundant inheritance from
Generic[]. - Y061: Do not use
Noneinside aLiteral[]slice. For example, useLiteral["foo"] | Noneinstead ofLiteral["foo", None].
Other changes:
- The undocumented
pyi.__version__andpyi.PyiTreeChecker.versionattributes has been removed. Useflake8 --versionfrom the command line, orimportlib.metadata.version("flake8_pyi")at runtime, to determine the version offlake8-pyiinstalled at runtime. - Y038 now flags
from typing_extensions import AbstractSetas well asfrom typing import AbstractSet. - Y022 and Y037 now flag more imports from
typing_extensions. - Y034 now attempts to avoid flagging methods inside classes that inherit from
builtins.type,abc.ABCMetaand/orenum.EnumMeta. Classes that have one or more of these as bases are metaclasses, and PEP 673 forbids the use oftyping(_extensions).Selffor metaclasses. While reliably determining whether a class is a metaclass in all cases would be impossible for flake8-pyi, the new heuristics should reduce the number of false positives from this check. - Attempting to import
typing_extensions.Textnow causes Y039 to be emitted rather than Y023. - Y053 will no longer be emitted for the argument to
@typing_extensions.deprecated.
-
Introduce Y090, which warns if you have an annotation such as
tuple[int]orTuple[int]. These mean "a tuple of length 1, in which the sole element is of typeint". This is sometimes what you want, but more usually you'll wanttuple[int, ...], which means "a tuple of arbitrary (possibly 0) length, in which all elements are of typeint".This error code is disabled by default due to the risk of false-positive errors. To enable it, use the
--extend-select=Y090option. -
Y011 now ignores
sentineland_typeshed.sentinelin default values.
Features:
- Support Python 3.12
- Support PEP 695 syntax for declaring type aliases
- Correctly emit Y019 errors for PEP-695 methods that are generic around a
TypeVarinstead of returningtyping_extensions.Self - Introduce Y057: Do not use
typing.ByteStringorcollections.abc.ByteString. These types have unclear semantics, and are deprecated; usetyping_extensions.Bufferor a union such asbytes | bytearray | memoryviewinstead. See PEP 688 for more details. - The way in which flake8-pyi modifies pyflakes runs has been improved:
-
When flake8-pyi is installed, pyflakes will now complain about forward references in default values for function and method parameters (the same as pyflakes does when it checks
.pyfiles). Unlike in.pyfiles, forward references in default values are legal in stub files. However, they are never necessary, and are considered bad style. (Forward references for parameter annotations are still allowed.)Contributed by tomasr8.
-
When flake8-pyi is installed, pyflakes's F822 check now produces many fewer false positives when flake8 is run on
.pyifiles. It now understands thatx: intin a stub file is sufficient forxto be considered "bound", and that"x"can therefore be included in__all__.
-
Bugfixes:
- Y018, Y046, Y047 and Y049 previously failed to detect unused
TypeVars/ParamSpecs/TypeAliases/TypedDicts/Protocols if the object in question had
multiple definitions in the same file (e.g. across two branches of an
if sys.version_info >= (3, 10)check). This bug has now been fixed. - Y020 was previously not emitted if quoted annotations were used in TypeVar constraints. This bug has now been fixed.
Other changes:
- flake8-pyi no longer supports being run on Python 3.7, which has reached its end of life.
- flake8-pyi no longer supports being run with flake8 <v6.
-
flake8-pyi no longer supports being run with flake8 <5.0.4.
-
The way in which flake8-pyi modifies pyflakes runs has been improved:
-
When flake8-pyi is installed, pyflakes now correctly recognises an annotation as being equivalent to a binding assignment in a stub file, reducing false positives from flake8's F821 error code.
-
When flake8-pyi is installed, there are now fewer pyflakes positives from class definitions that have forward references in the bases tuple for the purpose of creating recursive or circular type definitions. These are invalid in
.pyfiles, but are supported in stub files. -
When flake8-pyi is installed, pyflakes will also complain about code which (in combination with flake8-pyi) it previously had no issue with. For example, it will now complain about this code:
class Foo(Bar): ... class Bar: ...
Although the above code is legal in a stub file, it is considered poor style, and the forward reference serves no purpose (there is no recursive or circular definition). As such, it is now disallowed by pyflakes when flake8-pyi is installed.
Contributed by tomasr8.
-
-
Introduce Y056: Various type checkers have different levels of support for method calls on
__all__. Use__all__ += ["foo", "bar"]instead, as this is known to be supported by all major type checkers.
New error codes:
- Y055: Unions of the form
type[X] | type[Y]can be simplified totype[X | Y]. Similarly,Union[type[X], type[Y]]can be simplified totype[Union[X, Y]]. Contributed by tomasr8.
- Update error messages for Y019 and Y034 to recommend using
typing_extensions.Selfrather than_typeshed.Self.
New error codes:
- Y053: Disallow string or bytes literals with length >50 characters. Previously this rule only applied to parameter default values; it now applies everywhere.
- Y054: Disallow numeric literals with a string representation >10 characters long. Previously this rule only applied to parameter default values; it now applies everywhere.
Other changes:
- Y011/Y014/Y015: Simple container literals (
list,dict,tupleandsetliterals) are now allowed as default values. - Y052 is now emitted more consistently.
- Some things that used to result in Y011, Y014 or Y015 being emitted now result in Y053 or Y054 being emitted.
- Y011/Y014/Y015: Allow
mathconstantsmath.inf,math.nan,math.e,math.pi,math.tau, and their negatives in default values. Some other semantically equivalent values, such asx = inf(from math import inf), orx = np.inf(import numpy as np), should be rewritten tox = math.inf. Contributed by XuehaiPan.
- Y011/Y014/Y015: Increase the maximum character length of literal numbers in default values from 7 to 10, allowing hexadecimal representation of 32-bit integers. Contributed by Avasam.
New error codes:
- Y052: Disallow default values in global or class namespaces where the
assignment does not have a type annotation. Stubs should be explicit about
the type of all variables in the stub; without type annotations, the type
checker is forced to make inferences, which may have unpredictable
consequences. Enum members are excluded from this check, as are various
special assignments such as
__all__and__match_args__.
Other changes:
- Disallow numeric default values where
len(str(default)) > 7. If a function has a default value where the string representation is greater than 7 characters, it is likely to be an implementation detail or a constant that varies depending on the system you're running on, such assys.maxsize. - Disallow
strorbytesdefaults where the default is >50 characters long, for similar reasons. - Allow
ast.Attributenodes as default values for a small number of special cases, such assys.maxsizeandsys.executable. - Fewer Y020 false positives are now emitted when encountering default values in stub files.
Bugfixes:
- Do not emit Y020 (quoted annotations) for strings in parameter defaults.
- Fix checking of defaults for functions with positional-only parameters.
Other changes:
- Modify Y036 so that
_typeshed.Unusedis allowed as an annotation for parameters in__(a)exit__methods. Contributed by Avasam - Several changes have been made to error codes relating to imports:
- The Y027 error code has been removed.
- All errors that used to result in Y027 being emitted now result in Y022 being emitted instead.
- Some errors that used to result in Y023 being emitted now result in Y022 being emitted instead.
typing.Matchandtyping.Patternhave been added to the list of imports banned by Y022. Usere.Matchandre.Patterninstead.
- flake8-pyi no longer supports stub files that aim to support Python 2. If your stubs need to support Python 2, pin flake8-pyi to 22.11.0 or lower.
- Y011, Y014 and Y015 have all been significantly relaxed.
None,bools,ints,floats,complexnumbers, strings andbytesare all now allowed as default values for parameter annotations or assignments. - Hatchling is now used as the build backend. This should have minimal, if any, user-facing impact.
Bugfixes:
- Specify encoding when opening files. Prevents
UnicodeDecodeErroron Windows when the file contains non-CP1252 characters. Contributed by Avasam. - Significant changes have been made to the Y041 check. Previously, Y041 flagged
"redundant numeric unions" (e.g.
float | int,complex | floatorcomplex | int) in all contexts outside of type aliases. This was incorrect. PEP 484 only specifies that type checkers should treatintas an implicit subtype offloatin the specific context of parameter annotations for functions and methods. Y041 has therefore been revised to only emit errors on "redundant numeric unions" in the context of parameter annotations.
Other changes:
- Support running with flake8 v6.
Bugfixes:
- Do not emit Y020 for empty strings. Y020 concerns "quoted annotations", but an empty string can never be a quoted annotation.
- Add special-casing so that Y020 is not emitted for
__slots__definitions insideclassblocks. - Expand Y035 to cover
__slots__definitions as well as__match_args__and__all__definitions. - Expand Y015 so that errors are emitted for assignments to negative numbers.
Other changes:
- Since v22.8.1, flake8-pyi has emitted a
FutureWarningif run with flake8<5, warning that the plugin would soon become incompatible with flake8<5. Due to some issues that mean that some users are unable to upgrade to flake8>=5, however, flake8-pyi no longer intends to remove support for running the plugin with flake8<5 before Python 3.7 has reached end-of-life. As such, theFutureWarningis no longer emitted.
New error codes:
- Y047: Detect unused
TypeAliasdeclarations. - Y049: Detect unused
TypedDictdefinitions. - Y050: Prefer
typing_extensions.Neverfor argument annotations overtyping.NoReturn. - Y051: Detect redundant unions between
Literaltypes and builtin supertypes (e.g.Literal["foo"] | str, orLiteral[5] | int).
Other enhancements:
- Support
mypy_extensions.TypedDict.
- Add support for flake8 >= 5.0.0.
New error codes:
- Y046: Detect unused
Protocols. - Y048: Function bodies should contain exactly one statement.
Bugfixes:
- Improve error message for the case where a function body contains a docstring
and a
...orpassstatement.
Other changes:
- Pin required flake8 version to <5.0.0 (flake8-pyi is not currently compatible with flake8 5.0.0).
New error codes:
- Introduce Y041: Ban redundant numeric unions (
int | float,int | complex,float | complex). - Introduce Y042: Type alias names should use CamelCase rather than snake_case
- Introduce Y043: Ban type aliases from having names ending with an uppercase "T".
- Introduce Y044: Discourage unnecessary
from __future__ import annotationsimport. Contributed by Torsten Wörtwein. - Introduce Y045: Ban returning
(Async)Iterablefrom__(a)iter__methods.
Other enhancements and behaviour changes:
- Improve error message for Y026 check.
- Expand Y026 check. Since version 22.4.0, this has only emitted an error for
assignments to
typing.Literal,typing.Union, and PEP 604 unions. It now also emits an error for any subscription on the right-hand side of a simple assignment, as well as for assignments totyping.AnyandNone. - Support
typing_extensions.overloadandtyping_extensions.NamedTuple. - Slightly expand Y034 to cover the case where a class inheriting from
(Async)Iteratorreturns(Async)Iterablefrom__(a)iter__. These classes should nearly always returnSelffrom these methods. - Support Python 3.11.
Behaviour changes:
- Relax Y020 check slightly, enabling the idiom
__all__ += ["foo", "bar"]to be used in a stub file.
Features:
- Introduce Y039: Use
strinstead oftyping.Textfor Python 3 stubs. - Teach the Y036 check that
builtins.object(as well as the unqualifiedobject) is acceptable as an annotation for an__(a)exit__method argument. - Teach the Y029 check to emit errors for
__repr__and__str__methods that returnbuiltins.str(as opposed to the unqualifiedstr). - Introduce Y040: Never explicitly inherit from
objectin Python 3 stubs.
Features:
- Expand Y027 check to prohibit importing any objects from the
typingmodule that are aliases for objects livingcollections.abc(except fortyping.AbstractSet, which is special-cased). - Introduce Y038: Use
from collections.abc import Set as AbstractSetinstead offrom typing import AbstractSet.
Bugfixes:
- Improve inaccurate error messages for Y036.
Features:
- Introduce Y036 (check for badly defined
__exit__and__aexit__methods). - Introduce Y037 (Use PEP 604 syntax instead of
typing.Unionandtyping.Optional). Contributed by Oleg Höfling.
Behaviour changes:
- Expand Y035 to cover
__match_args__inside class definitions, as well as__all__in the global scope.
Bugfixes:
- Improve Y026 check (regarding
typing.TypeAlias) to reduce false-positive errors emitted when the plugin encountered variable aliases in a stub file.
Bugfixes:
- fix bug where incorrect quoted annotations were not detected within
ifblocks
Behaviour changes:
- Add special-casing so that string literals are allowed in the context of
__match_args__assignments inside a class definition. - Add special-casing so that arbitrary values can be assigned to a variable in
a stub file if the variable is annotated with
Final.
Bugfixes:
- fix bugs in several error codes so that e.g.
_T = typing.TypeVar("_T")is recognised as aTypeVardefinition (previously only_T = TypeVar("_T")was recognised). - fix bug where
foo = Falseat the module level did not trigger a Y015 error. - fix bug where
TypeVars were erroneously flagged as unused if they were only used in atyping.Unionsubscript. - improve unclear error messages for Y022, Y023 and Y027 error codes.
Features:
- introduce Y032 (prefer
objecttoAnyfor the second argument in__eq__and__ne__methods). - introduce Y033 (always use annotations in stubs, rather than type comments).
- introduce Y034 (detect common errors where return types are hardcoded, but they
should use
TypeVars instead). - introduce Y035 (
__all__in a stub has the same semantics as at runtime).
- extend Y001 to cover
ParamSpecandTypeVarTuplein addition toTypeVar - detect usage of non-integer indices in
sys.version_infochecks - extend Y010 to check async functions in addition to normal functions
- extend Y010 to cover what was previously included in Y090 (disallow
assignments in
__init__methods) and Y091 (disallowraisestatements). The previous checks were disabled by default. - introduce Y016 (duplicate union member)
- introduce Y017 (disallows assignments with multiple targets or non-name targets)
- introduce Y018 (detect unused
TypeVars) - introduce Y019 (detect
TypeVars that should be_typeshed.Self, but aren't) - introduce Y020 (never use quoted annotations in stubs)
- introduce Y021 (docstrings should not be included in stubs)
- introduce Y022 (prefer stdlib classes over
typingaliases) - introduce Y023 (prefer
typingovertyping_extensions) - introduce Y024 (prefer
typing.NamedTupletocollections.namedtuple) - introduce Y026 (require using
TypeAliasfor type aliases) - introduce Y025 (always alias
collections.abc.Set) - introduce Y027 (Python 2-incompatible extension of Y022)
- introduce Y028 (Use class-based syntax for
NamedTuples) - introduce Y029 (never define
__repr__or__str__) - introduce Y030 (use
Literal['foo', 'bar']instead ofLiteral['foo'] | Literal['bar']) - introduce Y031 (use class-based syntax for
TypedDicts where possible) - all errors are now enabled by default
- remove Y092 (top-level attribute must not have a default value)
attrsis no longer a dependencyast_decompilerhas been added as a dependency on Python 3.8 and 3.7- support Python 3.10
- discontinue support for Python 3.6
- support Python 3.9
- support flake8 3.8.0
- introduce Y091 (function body must not contain
raise) - introduce Y015 (attribute must not have a default value other than
...) - introduce Y092 (top-level attribute must not have a default value)
- update pyflakes dependency
- support Python 3.7
- add a check for non-ellipsis, non-typed arguments
- add checks for checking empty classes
- use --stdin-display-name as the filename when reading from stdin
- introduce Y011
- (release herp derp, don't use)
- introduce Y001 - Y010
- introduce optional Y090
- handle
delstatements in stub files
- handle annotated assignments in 3.6+ with forward reference support
-
handle forward references during subclassing on module level
-
handle forward references during type aliasing assignments on module level
-
first published version
-
date-versioned