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

Remove non-existing _DT from ordered dataclass #18846

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 1 addition & 5 deletions mypy/plugins/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,7 @@ def transform(self) -> bool:
self._api, self._cls, "__init__", args=args, return_type=NoneType()
)

if (
decorator_arguments["eq"]
and info.get("__eq__") is None
or decorator_arguments["order"]
):
if decorator_arguments["eq"] and info.get("__eq__") is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this still keep creating _DT on @dataclass(eq=True)? The problematic bit is actually creating a fake TypeVarExpr and putting it in dataclass' symbol table.

Furthermore, this will actually break @dataclass(eq=False, order=True) definitions, because below self._api.fail() does not abort early, we'll still create methods referencing non-existent _DT, maybe leading to some crashes down the road.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I removed _DT from @dataclass(eq=True).

But I don't actually understand about @dataclass(eq=False, order=True) definitions, newbie in mypy:)

# Type variable for self types in generated methods.
obj_type = self._api.named_type("builtins.object")
self_tvar_expr = TypeVarExpr(
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -2594,3 +2594,15 @@ class B2(B1): # E: A NamedTuple cannot be a dataclass
pass

[builtins fixtures/tuple.pyi]

[case testOrderedDataclassSelfTVARNotExist]
from dataclasses import dataclass

@dataclass(order=True)
class A:
b: int

a = A(1)
a._DT # E: "A" has no attribute "_DT"

[builtins fixtures/tuple.pyi]
1 change: 1 addition & 0 deletions test-data/unit/fixtures/tuple.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ _Tco = TypeVar('_Tco', covariant=True)
class object:
def __init__(self) -> None: pass
def __new__(cls) -> Self: ...
def __eq__(self, other) -> bool: pass

class type:
def __init__(self, *a: object) -> None: pass
Expand Down
Loading