Skip to content

[dataclass_transform] detect transform spec changes in incremental mode #14695

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

Merged
merged 6 commits into from
Feb 22, 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
4 changes: 2 additions & 2 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3907,7 +3907,7 @@ def serialize(self) -> JsonDict:
"order_default": self.order_default,
"kw_only_default": self.kw_only_default,
"frozen_only_default": self.frozen_default,
"field_specifiers": self.field_specifiers,
"field_specifiers": list(self.field_specifiers),
}

@classmethod
Expand All @@ -3917,7 +3917,7 @@ def deserialize(cls, data: JsonDict) -> DataclassTransformSpec:
order_default=data.get("order_default"),
kw_only_default=data.get("kw_only_default"),
frozen_default=data.get("frozen_default"),
field_specifiers=data.get("field_specifiers"),
field_specifiers=tuple(data.get("field_specifiers", [])),
)


Expand Down
8 changes: 8 additions & 0 deletions mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'
TypeVarTupleExpr,
Var,
)
from mypy.semanal_shared import find_dataclass_transform_spec
from mypy.types import (
AnyType,
CallableType,
Expand Down Expand Up @@ -230,6 +231,7 @@ def snapshot_definition(node: SymbolNode | None, common: SymbolSnapshot) -> Symb
elif isinstance(node, OverloadedFuncDef) and node.impl:
impl = node.impl.func if isinstance(node.impl, Decorator) else node.impl
is_trivial_body = impl.is_trivial_body if impl else False
dataclass_transform_spec = find_dataclass_transform_spec(node)
return (
"Func",
common,
Expand All @@ -239,6 +241,7 @@ def snapshot_definition(node: SymbolNode | None, common: SymbolSnapshot) -> Symb
node.is_static,
signature,
is_trivial_body,
dataclass_transform_spec.serialize() if dataclass_transform_spec is not None else None,
)
elif isinstance(node, Var):
return ("Var", common, snapshot_optional_type(node.type), node.is_final)
Expand All @@ -256,6 +259,10 @@ def snapshot_definition(node: SymbolNode | None, common: SymbolSnapshot) -> Symb
snapshot_definition(node.func, common),
)
elif isinstance(node, TypeInfo):
dataclass_transform_spec = node.dataclass_transform_spec
if dataclass_transform_spec is None:
dataclass_transform_spec = find_dataclass_transform_spec(node)

attrs = (
node.is_abstract,
node.is_enum,
Expand All @@ -280,6 +287,7 @@ def snapshot_definition(node: SymbolNode | None, common: SymbolSnapshot) -> Symb
tuple(snapshot_type(tdef) for tdef in node.defn.type_vars),
[snapshot_type(base) for base in node.bases],
[snapshot_type(p) for p in node._promote],
dataclass_transform_spec.serialize() if dataclass_transform_spec is not None else None,
)
prefix = node.fullname
symbol_table = snapshot_symbol_table(prefix, node.names)
Expand Down
92 changes: 92 additions & 0 deletions test-data/unit/fine-grained-dataclass-transform.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
[case updateDataclassTransformParameterViaDecorator]
# flags: --python-version 3.11
from m import my_dataclass

@my_dataclass
class Foo:
x: int

foo = Foo(1)
foo.x = 2

[file m.py]
from typing import dataclass_transform

@dataclass_transform(frozen_default=False)
def my_dataclass(cls): return cls

[file m.py.2]
from typing import dataclass_transform

@dataclass_transform(frozen_default=True)
def my_dataclass(cls): return cls

[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]

[out]
==
main:9: error: Property "x" defined in "Foo" is read-only

[case updateDataclassTransformParameterViaParentClass]
# flags: --python-version 3.11
from m import Dataclass

class Foo(Dataclass):
x: int

foo = Foo(1)
foo.x = 2

[file m.py]
from typing import dataclass_transform

@dataclass_transform(frozen_default=False)
class Dataclass: ...

[file m.py.2]
from typing import dataclass_transform

@dataclass_transform(frozen_default=True)
class Dataclass: ...

[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]

[out]
==
main:8: error: Property "x" defined in "Foo" is read-only

[case updateBaseClassToUseDataclassTransform]
# flags: --python-version 3.11
from m import A

class B(A):
y: int

B(x=1, y=2)

[file m.py]
class Dataclass: ...

class A(Dataclass):
x: int

[file m.py.2]
from typing import dataclass_transform

@dataclass_transform()
class Dataclass: ...

class A(Dataclass):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here the change in MRO will trigger dependencies, so this doesn't perfectly test how applying dataclass transform behaves. You can work around this by having a Dataclass base class in the original version, but omit the dataclass_transform decorator.

x: int

[typing fixtures/typing-full.pyi]
[builtins fixtures/dataclasses.pyi]

[out]
main:7: error: Unexpected keyword argument "x" for "B"
builtins.pyi:12: note: "B" defined here
main:7: error: Unexpected keyword argument "y" for "B"
builtins.pyi:12: note: "B" defined here
==