You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Forbid imports of collections.abc aliases from typing (#213)
A regression test for python/typeshed#7635
Refs #46 (but doesn't quite close it).
Changes made:
* Expand Y027 to cover all objects in typing that are aliases to objects in collections.abc, except for AbstractSet
* Add new Y038 error code to forbid import AbstractSet from typing
* Change the error message in Y023 to make it clear that imports from collections.abc are now preferred to imports from typing.
* Refactor Y023 logic in pyi.py so as to quell flake8 from complaining that _check_import_or_attribute was getting too complex.
* Some small refactorings of test files to make them compatible with the new checks.
Copy file name to clipboardExpand all lines: README.md
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -69,11 +69,12 @@ currently emitted:
69
69
| 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]`.
70
70
| 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`.
71
71
| 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]`.
72
+
| 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.
72
73
73
74
Many error codes enforce modern conventions, and some cannot yet be used in
74
75
all cases:
75
76
76
-
* Y027 is incompatible with Python 2 and should only be used in stubs
77
+
* Y027 and Y038 are incompatible with Python 2. These should only be used in stubs
77
78
that are meant only for Python 3.
78
79
* Y037 (enforcing PEP 604 syntax everywhere) is not yet fully compatible with
@@ -69,14 +69,14 @@ class InvalidButPluginDoesNotCrash:
69
69
classBadIterator1(Iterator[int]):
70
70
def__iter__(self) ->Iterator[int]: ... # Y034 "__iter__" methods in classes like "BadIterator1" usually return "self" at runtime. Consider using "_typeshed.Self" in "BadIterator1.__iter__", e.g. "def __iter__(self: Self) -> Self: ..."
71
71
72
-
classBadIterator2(typing.Iterator[int]):
72
+
classBadIterator2(typing.Iterator[int]):# Y027 Use "collections.abc.Iterator[T]" instead of "typing.Iterator[T]" (PEP 585 syntax)
73
73
def__iter__(self) ->Iterator[int]: ... # Y034 "__iter__" methods in classes like "BadIterator2" usually return "self" at runtime. Consider using "_typeshed.Self" in "BadIterator2.__iter__", e.g. "def __iter__(self: Self) -> Self: ..."
classBadIterator3(typing.Iterator[int]):# Y027 Use "collections.abc.Iterator[T]" instead of "typing.Iterator[T]" (PEP 585 syntax)
76
76
def__iter__(self) ->collections.abc.Iterator[int]: ... # Y034 "__iter__" methods in classes like "BadIterator3" usually return "self" at runtime. Consider using "_typeshed.Self" in "BadIterator3.__iter__", e.g. "def __iter__(self: Self) -> Self: ..."
def__aiter__(self) ->typing.AsyncIterator[str]: ... # Y034 "__aiter__" methods in classes like "BadAsyncIterator" usually return "self" at runtime. Consider using "_typeshed.Self" in "BadAsyncIterator.__aiter__", e.g. "def __aiter__(self: Self) -> Self: ..."
79
+
def__aiter__(self) ->typing.AsyncIterator[str]: ... # Y034 "__aiter__" methods in classes like "BadAsyncIterator" usually return "self" at runtime. Consider using "_typeshed.Self" in "BadAsyncIterator.__aiter__", e.g. "def __aiter__(self: Self) -> Self: ..." # Y027 Use "collections.abc.AsyncIterator[T]" instead of "typing.AsyncIterator[T]" (PEP 585 syntax)
0 commit comments