-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Changes from 2 commits
2abeb6d
78a7092
432c0a4
5cb117b
33ac67f
9def364
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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, | ||
|
@@ -239,6 +241,7 @@ def snapshot_definition(node: SymbolNode | None, common: SymbolSnapshot) -> Symb | |
node.is_static, | ||
signature, | ||
is_trivial_body, | ||
dataclass_transform_spec, | ||
) | ||
elif isinstance(node, Var): | ||
return ("Var", common, snapshot_optional_type(node.type), node.is_final) | ||
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 | ||
== | ||
|
There was a problem hiding this comment.
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.