Skip to content

Commit f6a8037

Browse files
authored
Adjust inconsistent dataclasses plugin error messages (#14637)
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.
1 parent 35b2926 commit f6a8037

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

mypy/plugins/dataclasses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def transform(self) -> bool:
229229
# Add <, >, <=, >=, but only if the class has an eq method.
230230
if decorator_arguments["order"]:
231231
if not decorator_arguments["eq"]:
232-
ctx.api.fail("eq must be True if order is True", ctx.cls)
232+
ctx.api.fail('"eq" must be True if "order" is True', ctx.reason)
233233

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

test-data/unit/check-dataclass-transform.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def my_dataclass(*, eq: bool, order: bool) -> Callable[[Type], Type]:
5555
return cls
5656
return transform
5757

58-
@my_dataclass(eq=False, order=True)
59-
class Person: # E: eq must be True if order is True
58+
@my_dataclass(eq=False, order=True) # E: "eq" must be True if "order" is True
59+
class Person:
6060
name: str
6161
age: int
6262

test-data/unit/check-dataclasses.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,8 @@ app1 >= app3
672672
# flags: --python-version 3.7
673673
from dataclasses import dataclass
674674

675-
@dataclass(eq=False, order=True)
676-
class Application: # E: eq must be True if order is True
675+
@dataclass(eq=False, order=True) # E: "eq" must be True if "order" is True
676+
class Application:
677677
...
678678

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

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

690690
[builtins fixtures/dataclasses.pyi]

0 commit comments

Comments
 (0)