Skip to content

Commit

Permalink
Adjust inconsistent dataclasses plugin error messages (#14637)
Browse files Browse the repository at this point in the history
This commit adds quotes around Python identifiers in two
error messages, and points the error for `"eq" must be True if "order"
is True` more directly at the decorator that triggers the error message.
  • Loading branch information
wesleywright authored Feb 7, 2023
1 parent 35b2926 commit f6a8037
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def transform(self) -> bool:
# Add <, >, <=, >=, but only if the class has an eq method.
if decorator_arguments["order"]:
if not decorator_arguments["eq"]:
ctx.api.fail("eq must be True if order is True", ctx.cls)
ctx.api.fail('"eq" must be True if "order" is True', ctx.reason)

for method_name in ["__lt__", "__gt__", "__le__", "__ge__"]:
# Like for __eq__ and __ne__, we want "other" to match
Expand All @@ -247,7 +247,7 @@ def transform(self) -> bool:
if existing_method is not None and not existing_method.plugin_generated:
assert existing_method.node
ctx.api.fail(
f"You may not have a custom {method_name} method when order=True",
f'You may not have a custom "{method_name}" method when "order" is True',
existing_method.node,
)

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-dataclass-transform.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def my_dataclass(*, eq: bool, order: bool) -> Callable[[Type], Type]:
return cls
return transform

@my_dataclass(eq=False, order=True)
class Person: # E: eq must be True if order is True
@my_dataclass(eq=False, order=True) # E: "eq" must be True if "order" is True
class Person:
name: str
age: int

Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ app1 >= app3
# flags: --python-version 3.7
from dataclasses import dataclass

@dataclass(eq=False, order=True)
class Application: # E: eq must be True if order is True
@dataclass(eq=False, order=True) # E: "eq" must be True if "order" is True
class Application:
...

[builtins fixtures/dataclasses.pyi]
Expand All @@ -684,7 +684,7 @@ from dataclasses import dataclass

@dataclass(order=True)
class Application:
def __lt__(self, other: 'Application') -> bool: # E: You may not have a custom __lt__ method when order=True
def __lt__(self, other: 'Application') -> bool: # E: You may not have a custom "__lt__" method when "order" is True
...

[builtins fixtures/dataclasses.pyi]
Expand Down

0 comments on commit f6a8037

Please sign in to comment.