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

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

Merged
merged 6 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -3909,7 +3909,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 @@ -3919,7 +3919,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
4 changes: 4 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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't support equality and can't be used in snapshots. You can probably use the serialized form instead.

)
elif isinstance(node, Var):
return ("Var", common, snapshot_optional_type(node.type), node.is_final)
Expand Down Expand Up @@ -280,6 +283,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],
node.dataclass_transform_spec or find_dataclass_transform_spec(node),
Copy link
Collaborator Author

@wesleywright wesleywright Feb 13, 2023

Choose a reason for hiding this comment

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

this may be somewhat expensive; an alternative may be to attach the relevant spec to each class in the hierarchy during semantic analysis rather than only attaching the spec to the transformer class

Copy link
Collaborator

Choose a reason for hiding this comment

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

Class hierarchies are almost always quite shallow, so this shouldn't be expensive. Similar to above, the snapshots must support equality. Using the serialized form should do the trick.

)
prefix = node.fullname
symbol_table = snapshot_symbol_table(prefix, node.names)
Expand Down
91 changes: 91 additions & 0 deletions test-data/unit/fine-grained-dataclass-transform.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
[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 A:
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
==