Skip to content

Commit 5ce0ecf

Browse files
authored
More minor updates now that more things can be imported from typing_extensions (#433)
Updates to reflect changes made in python/typing_extensions@520dcd1
1 parent 1e07452 commit 5ce0ecf

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Other changes:
1515
attributes has been removed. Use `flake8 --version` from the command line, or
1616
`importlib.metadata.version("flake8_pyi")` at runtime, to determine the
1717
version of `flake8-pyi` installed at runtime.
18+
* Y038 now flags `from typing_extensions import AbstractSet` as well as
19+
`from typing import AbstractSet`.
1820
* Y022 and Y037 now flag more imports from `typing_extensions`.
1921

2022
## 23.10.0

ERRORCODES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The following warnings are currently emitted by default:
4040
| Y035 | `__all__`, `__match_args__` and `__slots__` in a stub file should always have values, as these special variables in a `.pyi` file have identical semantics in a stub as at runtime. E.g. write `__all__ = ["foo", "bar"]` instead of `__all__: list[str]`.
4141
| 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`, `_typeshed.Unused` (a special alias for `object`) or `type[BaseException] \| None`.
4242
| Y037 | Use PEP 604 syntax instead of `typing(_extensions).Union` and `typing(_extensions).Optional`. E.g. use `str \| int` instead of `Union[str, int]`, and use `str \| None` instead of `Optional[str]`.
43-
| Y038 | Use `from collections.abc import Set as AbstractSet` instead of `from typing import AbstractSet`.
43+
| Y038 | Use `from collections.abc import Set as AbstractSet` instead of `from typing import AbstractSet` or `from typing_extensions import AbstractSet`.
4444
| Y039 | Use `str` instead of `typing.Text`.
4545
| Y040 | Never explicitly inherit from `object`, as all classes implicitly inherit from `object` in Python 3.
4646
| Y041 | Y041 detects redundant numeric unions in the context of parameter annotations. For example, PEP 484 specifies that type checkers should allow `int` objects to be passed to a function, even if the function states that it accepts a `float`. As such, `int` is redundant in the union `int \| float` in the context of a parameter annotation. In the same way, `int` is sometimes redundant in the union `int \| complex`, and `float` is sometimes redundant in the union `float \| complex`.

pyi.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ def _is_name(node: ast.AST | None, name: str) -> bool:
258258

259259

260260
_TYPING_MODULES = frozenset({"typing", "typing_extensions"})
261+
_TYPING_OR_COLLECTIONS_ABC = _TYPING_MODULES | {"collections.abc"}
261262

262263

263264
def _is_object(node: ast.AST | None, name: str, *, from_: Container[str]) -> bool:
@@ -269,8 +270,7 @@ def _is_object(node: ast.AST | None, name: str, *, from_: Container[str]) -> boo
269270
where <parent> is a string that can be found within the `from_` collection of
270271
strings.
271272
272-
>>> modules = _TYPING_MODULES | {"collections.abc"}
273-
>>> _is_AsyncIterator = partial(_is_object, name="AsyncIterator", from_=modules)
273+
>>> _is_AsyncIterator = partial(_is_object, name="AsyncIterator", from_=_TYPING_OR_COLLECTIONS_ABC)
274274
>>> _is_AsyncIterator(_ast_node_for("AsyncIterator"))
275275
True
276276
>>> _is_AsyncIterator(_ast_node_for("typing.AsyncIterator"))
@@ -302,26 +302,24 @@ def _is_object(node: ast.AST | None, name: str, *, from_: Container[str]) -> boo
302302
)
303303
_is_Literal = partial(_is_object, name="Literal", from_=_TYPING_MODULES)
304304
_is_abstractmethod = partial(_is_object, name="abstractmethod", from_={"abc"})
305-
_is_Any = partial(_is_object, name="Any", from_={"typing"})
305+
_is_Any = partial(_is_object, name="Any", from_=_TYPING_MODULES)
306306
_is_overload = partial(_is_object, name="overload", from_=_TYPING_MODULES)
307307
_is_final = partial(_is_object, name="final", from_=_TYPING_MODULES)
308308
_is_Self = partial(_is_object, name="Self", from_=({"_typeshed"} | _TYPING_MODULES))
309309
_is_TracebackType = partial(_is_object, name="TracebackType", from_={"types"})
310310
_is_builtins_object = partial(_is_object, name="object", from_={"builtins"})
311311
_is_builtins_type = partial(_is_object, name="type", from_={"builtins"})
312312
_is_Unused = partial(_is_object, name="Unused", from_={"_typeshed"})
313-
_is_Iterable = partial(_is_object, name="Iterable", from_={"typing", "collections.abc"})
313+
_is_Iterable = partial(_is_object, name="Iterable", from_=_TYPING_OR_COLLECTIONS_ABC)
314314
_is_AsyncIterable = partial(
315-
_is_object, name="AsyncIterable", from_={"collections.abc"} | _TYPING_MODULES
315+
_is_object, name="AsyncIterable", from_=_TYPING_OR_COLLECTIONS_ABC
316316
)
317317
_is_Protocol = partial(_is_object, name="Protocol", from_=_TYPING_MODULES)
318318
_is_NoReturn = partial(_is_object, name="NoReturn", from_=_TYPING_MODULES)
319319
_is_Final = partial(_is_object, name="Final", from_=_TYPING_MODULES)
320-
_is_Generator = partial(
321-
_is_object, name="Generator", from_=_TYPING_MODULES | {"collections.abc"}
322-
)
320+
_is_Generator = partial(_is_object, name="Generator", from_=_TYPING_OR_COLLECTIONS_ABC)
323321
_is_AsyncGenerator = partial(
324-
_is_object, name="AsyncGenerator", from_=_TYPING_MODULES | {"collections.abc"}
322+
_is_object, name="AsyncGenerator", from_=_TYPING_OR_COLLECTIONS_ABC
325323
)
326324
_is_Generic = partial(_is_object, name="Generic", from_=_TYPING_MODULES)
327325

@@ -454,7 +452,7 @@ def _get_collections_abc_obj_id(node: ast.expr | None) -> str | None:
454452
if not isinstance(node, ast.Subscript):
455453
return None
456454
return _get_name_of_class_if_from_modules(
457-
node.value, modules=_TYPING_MODULES | {"collections.abc"}
455+
node.value, modules=_TYPING_OR_COLLECTIONS_ABC
458456
)
459457

460458

@@ -974,8 +972,8 @@ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
974972
for object_name in imported_names:
975973
self._check_import_or_attribute(node, module_name, object_name)
976974

977-
if module_name == "typing" and "AbstractSet" in imported_names:
978-
self.error(node, Y038)
975+
if module_name in _TYPING_MODULES and "AbstractSet" in imported_names:
976+
self.error(node, Y038.format(module=module_name))
979977

980978
def _check_for_typevarlike_assignments(
981979
self, node: ast.Assign, function: ast.expr, object_name: str
@@ -2138,7 +2136,7 @@ def parse_options(options: argparse.Namespace) -> None:
21382136
Y037 = "Y037 Use PEP 604 union types instead of {old_syntax} (e.g. {example})."
21392137
Y038 = (
21402138
'Y038 Use "from collections.abc import Set as AbstractSet" '
2141-
'instead of "from typing import AbstractSet" (PEP 585 syntax)'
2139+
'instead of "from {module} import AbstractSet" (PEP 585 syntax)'
21422140
)
21432141
Y039 = 'Y039 Use "str" instead of "typing.Text"'
21442142
Y040 = 'Y040 Do not inherit from "object" explicitly, as it is redundant in Python 3'

tests/imports.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ from typing_extensions import runtime_checkable # Y023 Use "typing.runtime_chec
147147
from collections import namedtuple # Y024 Use "typing.NamedTuple" instead of "collections.namedtuple"
148148
from collections.abc import Set # Y025 Use "from collections.abc import Set as AbstractSet" to avoid confusion with "builtins.set"
149149
from typing import AbstractSet # Y038 Use "from collections.abc import Set as AbstractSet" instead of "from typing import AbstractSet" (PEP 585 syntax)
150+
from typing_extensions import AbstractSet # Y038 Use "from collections.abc import Set as AbstractSet" instead of "from typing_extensions import AbstractSet" (PEP 585 syntax)
150151
from typing import Text # Y039 Use "str" instead of "typing.Text"
151152
from typing import ByteString # Y057 Do not use typing.ByteString, which has unclear semantics and is deprecated
152153
from collections.abc import ByteString # Y057 Do not use collections.abc.ByteString, which has unclear semantics and is deprecated

0 commit comments

Comments
 (0)