Skip to content
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

Improve the error message for Y060 #453

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ New error codes:

Other changes:
* Y061 is no longer emitted in situations where Y062 would also be emitted.
* Improve error message for Y060.

## 23.11.0

Expand Down
5 changes: 3 additions & 2 deletions pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,8 @@ def _check_class_bases(self, bases: list[ast.expr]) -> None:
if len(subscript_bases) > 1 and all_equal(
ast.dump(subscript_base.slice) for subscript_base in subscript_bases
):
self.error(Generic_basenode, Y060)
msg = Y060.format(redundant_base=unparse(Generic_basenode))
self.error(Generic_basenode, msg)

def visit_ClassDef(self, node: ast.ClassDef) -> None:
if node.name.startswith("_") and not self.in_class.active:
Expand Down Expand Up @@ -2295,7 +2296,7 @@ def parse_options(options: argparse.Namespace) -> None:
)
Y059 = 'Y059 "Generic[]" should always be the last base class'
Y060 = (
'Y060 Redundant inheritance from "Generic[]"; '
'Y060 Redundant inheritance from "{redundant_base}"; '
"class would be inferred as generic anyway"
)
Y061 = 'Y061 None inside "Literal[]" expression. Replace with "{suggestion}"'
Expand Down
10 changes: 5 additions & 5 deletions tests/classdefs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,19 @@ class GoodGeneric3(int, str, typing_extensions.Generic[_T]): ...
class BadGeneric4(Generic[_T], Iterable[int], str): ... # Y059 "Generic[]" should always be the last base class
class GoodGeneric4(Iterable[int], str, Generic[_T]): ...

class RedundantGeneric1(Iterable[_T], Generic[_T]): ... # Y060 Redundant inheritance from "Generic[]"; class would be inferred as generic anyway
class RedundantGeneric1(Iterable[_T], Generic[_T]): ... # Y060 Redundant inheritance from "Generic[_T]"; class would be inferred as generic anyway
class Corrected1(Iterable[_T]): ...

class RedundantGeneric2(Generic[_S], GoodGeneric[_S]): ... # Y059 "Generic[]" should always be the last base class # Y060 Redundant inheritance from "Generic[]"; class would be inferred as generic anyway
class RedundantGeneric2(Generic[_S], GoodGeneric[_S]): ... # Y059 "Generic[]" should always be the last base class # Y060 Redundant inheritance from "Generic[_S]"; class would be inferred as generic anyway
class Corrected2(GoodGeneric[_S]): ...

class RedundantGeneric3(int, Iterator[_T], str, float, memoryview, bytes, Generic[_T]): ... # Y060 Redundant inheritance from "Generic[]"; class would be inferred as generic anyway
class RedundantGeneric3(int, Iterator[_T], str, float, memoryview, bytes, Generic[_T]): ... # Y060 Redundant inheritance from "Generic[_T]"; class would be inferred as generic anyway
class Corrected3(int, Iterator[_T], str, float, memoryview, bytes): ...

class RedundantGeneric4(Iterable[_T], Iterator[_T], Generic[_T]): ... # Y060 Redundant inheritance from "Generic[]"; class would be inferred as generic anyway
class RedundantGeneric4(Iterable[_T], Iterator[_T], Generic[_T]): ... # Y060 Redundant inheritance from "Generic[_T]"; class would be inferred as generic anyway
class Corrected4(Iterable[_T], Iterator[_T]): ...

class BadAndRedundantGeneric(object, Generic[_T], Container[_T]): ... # Y040 Do not inherit from "object" explicitly, as it is redundant in Python 3 # Y059 "Generic[]" should always be the last base class # Y060 Redundant inheritance from "Generic[]"; class would be inferred as generic anyway
class BadAndRedundantGeneric(object, Generic[_T], Container[_T]): ... # Y040 Do not inherit from "object" explicitly, as it is redundant in Python 3 # Y059 "Generic[]" should always be the last base class # Y060 Redundant inheritance from "Generic[_T]"; class would be inferred as generic anyway
class Corrected5(Container[_T]): ...

# Strictly speaking this inheritance from Generic is "redundant",
Expand Down