Skip to content

Commit

Permalink
Flag typing_extensions.Text with Y039, not Y023 (#437)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
AlexWaygood authored Nov 2, 2023
1 parent 0ccf764 commit bf40960
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Other changes:
for metaclasses. While reliably determining whether a class is a metaclass in
all cases would be impossible for flake8-pyi, the new heuristics should
reduce the number of false positives from this check.
* Attempting to import `typing_extensions.Text` now causes Y039 to be emitted
rather than Y023.

## 23.10.0

Expand Down
2 changes: 1 addition & 1 deletion ERRORCODES.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The following warnings are currently emitted by default:
| 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`.
| 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]`.
| Y038 | Use `from collections.abc import Set as AbstractSet` instead of `from typing import AbstractSet` or `from typing_extensions import AbstractSet`.
| Y039 | Use `str` instead of `typing.Text`.
| Y039 | Use `str` instead of `typing.Text` or `typing_extensions.Text`.
| Y040 | Never explicitly inherit from `object`, as all classes implicitly inherit from `object` in Python 3.
| 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`.
| Y042 | Type alias names should use CamelCase rather than snake_case.
Expand Down
14 changes: 8 additions & 6 deletions pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ class TypeVarInfo(NamedTuple):
"runtime_checkable",
"NewType",
"overload",
"Text",
"NoReturn",
# ClassVar deliberately omitted,
# as it's the only one in this group that should be parameterised.
# It is special-cased elsewhere.
#
# Text is also deliberately omitted,
# as you shouldn't be importing it from anywhere! (Y039)
}
)

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

# Y039 errors
elif module_name in _TYPING_MODULES and object_name == "Text":
error_message = Y039.format(module=module_name)

# Y023 errors
elif module_name == "typing_extensions":
if object_name in _BAD_TYPINGEXTENSIONS_Y023_IMPORTS:
Expand All @@ -961,10 +967,6 @@ def _check_import_or_attribute(
elif fullname == "collections.namedtuple":
error_message = Y024

# Y039 errors
elif fullname == "typing.Text":
error_message = Y039

else:
return

Expand Down Expand Up @@ -2185,7 +2187,7 @@ def parse_options(options: argparse.Namespace) -> None:
'Y038 Use "from collections.abc import Set as AbstractSet" '
'instead of "from {module} import AbstractSet" (PEP 585 syntax)'
)
Y039 = 'Y039 Use "str" instead of "typing.Text"'
Y039 = 'Y039 Use "str" instead of "{module}.Text"'
Y040 = 'Y040 Do not inherit from "object" explicitly, as it is redundant in Python 3'
Y041 = (
'Y041 Use "{implicit_supertype}" '
Expand Down
1 change: 1 addition & 0 deletions tests/imports.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ from collections.abc import Set # Y025 Use "from collections.abc import Set as
from typing import AbstractSet # Y038 Use "from collections.abc import Set as AbstractSet" instead of "from typing import AbstractSet" (PEP 585 syntax)
from typing_extensions import AbstractSet # Y038 Use "from collections.abc import Set as AbstractSet" instead of "from typing_extensions import AbstractSet" (PEP 585 syntax)
from typing import Text # Y039 Use "str" instead of "typing.Text"
from typing_extensions import Text # Y039 Use "str" instead of "typing_extensions.Text"
from typing import ByteString # Y057 Do not use typing.ByteString, which has unclear semantics and is deprecated
from collections.abc import ByteString # Y057 Do not use collections.abc.ByteString, which has unclear semantics and is deprecated

Expand Down

0 comments on commit bf40960

Please sign in to comment.