-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Broaden appropriate flake8-pyi rules to check non-stub code too (#6297)
Of the rules that flake8-pyi enforces for `.pyi` type stubs, many of them equally make sense to check in normal runtime code with type annotations. Broaden these rules to check all files: PYI013 ellipsis-in-non-empty-class-body PYI016 duplicate-union-member PYI018 unused-private-type-var PYI019 custom-type-var-return-type PYI024 collections-named-tuple PYI025 unaliased-collections-abc-set-import PYI030 unnecessary-literal-union PYI032 any-eq-ne-annotation PYI034 non-self-return-type PYI036 bad-exit-annotation PYI041 redundant-numeric-union PYI042 snake-case-type-alias PYI043 t-suffixed-type-alias PYI045 iter-method-return-iterable PYI046 unused-private-protocol PYI047 unused-private-type-alias PYI049 unused-private-typed-dict PYI050 no-return-argument-annotation-in-stub (Python ≥ 3.11) PYI051 redundant-literal-union PYI056 unsupported-method-call-on-all The other rules are stub-specific and remain enabled only in `.pyi` files. PYI001 unprefixed-type-param PYI002 complex-if-statement-in-stub PYI003 unrecognized-version-info-check PYI004 patch-version-comparison PYI005 wrong-tuple-length-version-comparison (could make sense to broaden, see #6297 (comment)) PYI006 bad-version-info-comparison (same) PYI007 unrecognized-platform-check PYI008 unrecognized-platform-name PYI009 pass-statement-stub-body PYI010 non-empty-stub-body PYI011 typed-argument-default-in-stub PYI012 pass-in-class-body PYI014 argument-default-in-stub PYI015 assignment-default-in-stub PYI017 complex-assignment-in-stub PYI020 quoted-annotation-in-stub PYI021 docstring-in-stub PYI026 type-alias-without-annotation (could make sense to broaden, but gives many false positives on runtime code as currently implemented) PYI029 str-or-repr-defined-in-stub PYI033 type-comment-in-stub PYI035 unassigned-special-variable-in-stub PYI044 future-annotations-in-stub PYI048 stub-body-multiple-statements PYI052 unannotated-assignment-in-stub PYI053 string-or-bytes-too-long PYI054 numeric-literal-too-long Signed-off-by: Anders Kaseorg <andersk@mit.edu>
- Loading branch information
Showing
30 changed files
with
1,274 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import collections | ||
|
||
person: collections.namedtuple # OK | ||
person: collections.namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
|
||
from collections import namedtuple | ||
|
||
person: namedtuple # OK | ||
person: namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
|
||
person = namedtuple("Person", ["name", "age"]) # OK | ||
person = namedtuple( | ||
"Person", ["name", "age"] | ||
) # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,38 @@ | ||
import typing | ||
import typing_extensions | ||
from typing import Literal | ||
# Shouldn't emit for any cases in the non-stub file for compatibility with flake8-pyi. | ||
# Note that this rule could be applied here in the future. | ||
|
||
# Shouldn't affect non-union field types. | ||
field1: Literal[1] # OK | ||
field2: Literal[1] | Literal[2] # OK | ||
|
||
def func1(arg1: Literal[1] | Literal[2]): # OK | ||
# Should emit for duplicate field types. | ||
field2: Literal[1] | Literal[2] # Error | ||
|
||
# Should emit for union types in arguments. | ||
def func1(arg1: Literal[1] | Literal[2]): # Error | ||
print(arg1) | ||
|
||
|
||
def func2() -> Literal[1] | Literal[2]: # OK | ||
# Should emit for unions in return types. | ||
def func2() -> Literal[1] | Literal[2]: # Error | ||
return "my Literal[1]ing" | ||
|
||
|
||
field3: Literal[1] | Literal[2] | str # OK | ||
field4: str | Literal[1] | Literal[2] # OK | ||
field5: Literal[1] | str | Literal[2] # OK | ||
field6: Literal[1] | bool | Literal[2] | str # OK | ||
field7 = Literal[1] | Literal[2] # OK | ||
field8: Literal[1] | (Literal[2] | str) # OK | ||
field9: Literal[1] | (Literal[2] | str) # OK | ||
field10: (Literal[1] | str) | Literal[2] # OK | ||
field11: dict[Literal[1] | Literal[2], str] # OK | ||
# Should emit in longer unions, even if not directly adjacent. | ||
field3: Literal[1] | Literal[2] | str # Error | ||
field4: str | Literal[1] | Literal[2] # Error | ||
field5: Literal[1] | str | Literal[2] # Error | ||
field6: Literal[1] | bool | Literal[2] | str # Error | ||
|
||
# Should emit for non-type unions. | ||
field7 = Literal[1] | Literal[2] # Error | ||
|
||
# Should emit for parenthesized unions. | ||
field8: Literal[1] | (Literal[2] | str) # Error | ||
|
||
# Should handle user parentheses when fixing. | ||
field9: Literal[1] | (Literal[2] | str) # Error | ||
field10: (Literal[1] | str) | Literal[2] # Error | ||
|
||
# Should emit for union in generic parent type. | ||
field11: dict[Literal[1] | Literal[2], str] # Error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.