diff --git a/mypy/plugins/dataclasses.py b/mypy/plugins/dataclasses.py index 90c983b0bacd..50e9c927df9d 100644 --- a/mypy/plugins/dataclasses.py +++ b/mypy/plugins/dataclasses.py @@ -40,7 +40,6 @@ TempNode, TypeAlias, TypeInfo, - TypeVarExpr, Var, ) from mypy.plugin import ClassDefContext, FunctionSigContext, SemanticAnalyzerPluginInterface @@ -291,22 +290,6 @@ 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"] - ): - # Type variable for self types in generated methods. - obj_type = self._api.named_type("builtins.object") - self_tvar_expr = TypeVarExpr( - SELF_TVAR_NAME, - info.fullname + "." + SELF_TVAR_NAME, - [], - obj_type, - AnyType(TypeOfAny.from_omitted_generics), - ) - info.names[SELF_TVAR_NAME] = SymbolTableNode(MDEF, self_tvar_expr) - # Add <, >, <=, >=, but only if the class has an eq method. if decorator_arguments["order"]: if not decorator_arguments["eq"]: diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index 887a9052d0b9..e51a66b953a7 100644 --- a/test-data/unit/check-dataclasses.test +++ b/test-data/unit/check-dataclasses.test @@ -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] diff --git a/test-data/unit/fixtures/tuple.pyi b/test-data/unit/fixtures/tuple.pyi index d01cd0034d26..22462023aad4 100644 --- a/test-data/unit/fixtures/tuple.pyi +++ b/test-data/unit/fixtures/tuple.pyi @@ -9,6 +9,7 @@ _Tco = TypeVar('_Tco', covariant=True) class object: def __init__(self) -> None: pass def __new__(cls) -> Self: ... + def __eq__(self, other: object) -> bool: pass class type: def __init__(self, *a: object) -> None: pass