-
Notifications
You must be signed in to change notification settings - Fork 23
Introduce Y022/Y023/Y024/Y025: Imports linting error codes #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
03775f8
Introduce `Y022` and `Y023`: checking various imports
AlexWaygood 08d6a61
Add test, fix string
AlexWaygood a3e49be
Python 2 compat
AlexWaygood 3c4fb42
`contextlib` details, improve comments
AlexWaygood 7dbd846
Improve README description
AlexWaygood ac9421b
Black
AlexWaygood 1568c01
`alias` has no attribute `lineno`
AlexWaygood e2602bd
Correct type annotations
AlexWaygood 09a330e
in the middle
AlexWaygood fefcfde
merge
AlexWaygood a3836cf
Address review, resolve merge conflicts, add tests, fix bugs
AlexWaygood 14b507f
Better ALLCAPS names
AlexWaygood a23878d
Better syntax suggestions for `contextlib`/`collections.abc`
AlexWaygood 7d5468d
Code review: Improve control flow readability
AlexWaygood c221fa5
Update README.rst
AlexWaygood e3d3eb3
Update pyi.py
JelleZijlstra 2d20a05
Improve tests to cover attribute access inside functions and methods
AlexWaygood 9776225
Merge branch 'imports' of https://github.com/AlexWaygood/flake8-pyi i…
AlexWaygood 7644212
Disallow importing `Type` from `typing_extensions`, improve tests more
AlexWaygood c644a6c
Finish addressing review
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# NOTE: F401 & F811 are ignored in this file in the .flake8 config file | ||
|
||
# GOOD IMPORTS | ||
import typing | ||
import typing_extensions | ||
import collections | ||
import collections.abc | ||
from collections import ChainMap, Counter, OrderedDict, UserDict, UserList, UserString, defaultdict, deque | ||
from collections.abc import ( | ||
Awaitable, | ||
Coroutine, | ||
AsyncIterable, | ||
AsyncIterator, | ||
AsyncGenerator, | ||
Hashable, | ||
Iterable, | ||
Iterator, | ||
Generator, | ||
Reversible, | ||
Set as AbstractSet, | ||
Sized, | ||
Container, | ||
Callable, | ||
Collection, | ||
MutableSet, | ||
MutableMapping, | ||
MappingView, | ||
KeysView, | ||
ItemsView, | ||
ValuesView, | ||
Sequence, | ||
MutableSequence, | ||
ByteString | ||
) | ||
|
||
# Things that are of no use for stub files are intentionally omitted. | ||
from typing import ( | ||
Any, | ||
Callable, | ||
ClassVar, | ||
Generic, | ||
Optional, | ||
Protocol, | ||
TypeVar, | ||
Union, | ||
AbstractSet, | ||
ByteString, | ||
Container, | ||
Hashable, | ||
ItemsView, | ||
Iterable, | ||
Iterator, | ||
KeysView, | ||
Mapping, | ||
MappingView, | ||
MutableMapping, | ||
MutableSequence, | ||
MutableSet, | ||
Sequence, | ||
Sized, | ||
ValuesView, | ||
Awaitable, | ||
AsyncIterator, | ||
AsyncIterable, | ||
Coroutine, | ||
Collection, | ||
AsyncGenerator, | ||
Reversible, | ||
SupportsAbs, | ||
SupportsBytes, | ||
SupportsComplex, | ||
SupportsFloat, | ||
SupportsIndex, | ||
SupportsInt, | ||
SupportsRound, | ||
Generator, | ||
BinaryIO, | ||
IO, | ||
NamedTuple, | ||
Match, | ||
Pattern, | ||
TextIO, | ||
AnyStr, | ||
NewType, | ||
NoReturn, | ||
overload, | ||
ContextManager # ContextManager must be importable from typing (but not typing_extensions) for Python 2 compabitility | ||
) | ||
from typing_extensions import ( | ||
Concatenate, | ||
Final, | ||
ParamSpec, | ||
SupportsIndex, | ||
final, | ||
Literal, | ||
TypeAlias, | ||
TypeGuard, | ||
Annotated, | ||
TypedDict, | ||
OrderedDict # OrderedDict must be importable from typing_extensions (but not typing) for Python 2 compatibility | ||
) | ||
|
||
|
||
# BAD IMPORTS (Y022 code) | ||
from typing import Dict # Y022 Use "builtins.dict" instead of "typing.Dict" | ||
from typing import Counter # Y022 Use "collections.Counter" instead of "typing.Counter" | ||
from typing import AsyncContextManager # Y022 Use "contextlib.AbstractAsyncContextManager" instead of "typing.AsyncContextManager" | ||
from typing import ChainMap # Y022 Use "collections.ChainMap" instead of "typing.ChainMap" | ||
from typing_extensions import DefaultDict # Y022 Use "collections.defaultdict" instead of "typing_extensions.DefaultDict" | ||
from typing_extensions import ChainMap # Y022 Use "collections.ChainMap" instead of "typing_extensions.ChainMap" | ||
from typing_extensions import AsyncContextManager # Y022 Use "contextlib.AbstractAsyncContextManager" instead of "typing_extensions.AsyncContextManager" | ||
|
||
# BAD IMPORTS (Y023 code) | ||
from typing_extensions import ClassVar # Y023 Use "typing.ClassVar" instead of "typing_extensions.ClassVar" | ||
from typing_extensions import Awaitable # Y023 Use "collections.abc.Awaitable" or "typing.Awaitable" instead of "typing_extensions.Awaitable" | ||
from typing_extensions import ContextManager # Y023 Use "contextlib.AbstractContextManager" or "typing.ContextManager" instead of "typing_extensions.ContextManager" | ||
|
||
# BAD IMPORTS: OTHER | ||
from collections import namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" | ||
from collections.abc import Set # Y025 Use "from collections.abc import Set as AbstractSet" to avoid confusion with "builtins.set" | ||
|
||
# GOOD ATTRIBUTE ACCESS | ||
foo: typing.SupportsIndex | ||
bar: typing_extensions.Final[int] | ||
baz: collections.abc.Sized | ||
blah: collections.deque[int] | ||
|
||
# BAD ATTRIBUTE ACCESS (Y022 code) | ||
a: typing.Dict[str, int] # Y022 Use "builtins.dict" instead of "typing.Dict" | ||
b: typing.Counter[float] # Y022 Use "collections.Counter" instead of "typing.Counter" | ||
c: typing.AsyncContextManager[None] # Y022 Use "contextlib.AbstractAsyncContextManager" instead of "typing.AsyncContextManager" | ||
d: typing.ChainMap[int, str] # Y022 Use "collections.ChainMap" instead of "typing.ChainMap" | ||
e: typing_extensions.DefaultDict[bytes, bytes] # Y022 Use "collections.defaultdict" instead of "typing_extensions.DefaultDict" | ||
f: typing_extensions.ChainMap[str, str] # Y022 Use "collections.ChainMap" instead of "typing_extensions.ChainMap" | ||
g: typing_extensions.AsyncContextManager[Any] # Y022 Use "contextlib.AbstractAsyncContextManager" instead of "typing_extensions.AsyncContextManager" | ||
|
||
# BAD ATTRIBUTE ACCESS (Y023 code) | ||
class Foo: | ||
attribute: typing_extensions.ClassVar[int] # Y023 Use "typing.ClassVar" instead of "typing_extensions.ClassVar" | ||
|
||
|
||
h: typing_extensions.Awaitable[float] # Y023 Use "collections.abc.Awaitable" or "typing.Awaitable" instead of "typing_extensions.Awaitable" | ||
i: typing_extensions.ContextManager[None] # Y023 Use "contextlib.AbstractContextManager" or "typing.ContextManager" instead of "typing_extensions.ContextManager" | ||
|
||
# BAD ATTRIBUTE ACCESS: OTHER | ||
j: collections.namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple" |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.