Skip to content

Commit bf40960

Browse files
authored
Flag typing_extensions.Text with Y039, not Y023 (#437)
This is a minor change, but I think it's more consistent to flag this with Y039 rather than Y023. Otherwise we're telling people to use `typing.Text` rather than `typing_extensions.Text` via Y023, and then when they do so, we immediately complain at them to not use `Text` at all (via Y039)
1 parent 0ccf764 commit bf40960

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Other changes:
2727
for metaclasses. While reliably determining whether a class is a metaclass in
2828
all cases would be impossible for flake8-pyi, the new heuristics should
2929
reduce the number of false positives from this check.
30+
* Attempting to import `typing_extensions.Text` now causes Y039 to be emitted
31+
rather than Y023.
3032

3133
## 23.10.0
3234

ERRORCODES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The following warnings are currently emitted by default:
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]`.
4343
| Y038 | Use `from collections.abc import Set as AbstractSet` instead of `from typing import AbstractSet` or `from typing_extensions import AbstractSet`.
44-
| Y039 | Use `str` instead of `typing.Text`.
44+
| Y039 | Use `str` instead of `typing.Text` or `typing_extensions.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`.
4747
| Y042 | Type alias names should use CamelCase rather than snake_case.

pyi.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,13 @@ class TypeVarInfo(NamedTuple):
129129
"runtime_checkable",
130130
"NewType",
131131
"overload",
132-
"Text",
133132
"NoReturn",
134133
# ClassVar deliberately omitted,
135134
# as it's the only one in this group that should be parameterised.
136135
# It is special-cased elsewhere.
136+
#
137+
# Text is also deliberately omitted,
138+
# as you shouldn't be importing it from anywhere! (Y039)
137139
}
138140
)
139141

@@ -942,6 +944,10 @@ def _check_import_or_attribute(
942944
old_syntax=fullname, example='"int | str" instead of "Union[int, str]"'
943945
)
944946

947+
# Y039 errors
948+
elif module_name in _TYPING_MODULES and object_name == "Text":
949+
error_message = Y039.format(module=module_name)
950+
945951
# Y023 errors
946952
elif module_name == "typing_extensions":
947953
if object_name in _BAD_TYPINGEXTENSIONS_Y023_IMPORTS:
@@ -961,10 +967,6 @@ def _check_import_or_attribute(
961967
elif fullname == "collections.namedtuple":
962968
error_message = Y024
963969

964-
# Y039 errors
965-
elif fullname == "typing.Text":
966-
error_message = Y039
967-
968970
else:
969971
return
970972

@@ -2185,7 +2187,7 @@ def parse_options(options: argparse.Namespace) -> None:
21852187
'Y038 Use "from collections.abc import Set as AbstractSet" '
21862188
'instead of "from {module} import AbstractSet" (PEP 585 syntax)'
21872189
)
2188-
Y039 = 'Y039 Use "str" instead of "typing.Text"'
2190+
Y039 = 'Y039 Use "str" instead of "{module}.Text"'
21892191
Y040 = 'Y040 Do not inherit from "object" explicitly, as it is redundant in Python 3'
21902192
Y041 = (
21912193
'Y041 Use "{implicit_supertype}" '

tests/imports.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ from collections.abc import Set # Y025 Use "from collections.abc import Set as
149149
from typing import AbstractSet # Y038 Use "from collections.abc import Set as AbstractSet" instead of "from typing import AbstractSet" (PEP 585 syntax)
150150
from typing_extensions import AbstractSet # Y038 Use "from collections.abc import Set as AbstractSet" instead of "from typing_extensions import AbstractSet" (PEP 585 syntax)
151151
from typing import Text # Y039 Use "str" instead of "typing.Text"
152+
from typing_extensions import Text # Y039 Use "str" instead of "typing_extensions.Text"
152153
from typing import ByteString # Y057 Do not use typing.ByteString, which has unclear semantics and is deprecated
153154
from collections.abc import ByteString # Y057 Do not use collections.abc.ByteString, which has unclear semantics and is deprecated
154155

0 commit comments

Comments
 (0)