Add ClassVar support for dataclass

This commit is contained in:
Eric Masseran
2025-03-15 13:15:19 +01:00
parent 68c7bf35ce
commit 472ee75e3c
2 changed files with 10 additions and 1 deletions

View File

@@ -146,6 +146,10 @@ def get_dataclass_param_names(cls):
d = name.tree_name.get_definition() d = name.tree_name.get_definition()
annassign = d.children[1] annassign = d.children[1]
if d.type == 'expr_stmt' and annassign.type == 'annassign': if d.type == 'expr_stmt' and annassign.type == 'annassign':
node = annassign.children[1]
if node.type == "atom_expr" and node.children[0].value == "ClassVar":
continue
if len(annassign.children) < 4: if len(annassign.children) < 4:
default = None default = None
else: else:

View File

@@ -392,13 +392,18 @@ def test_dataclass_signature(
""" """
name: str name: str
foo = 3 foo = 3
blob: ClassVar[str]
price: float price: float
quantity: int = 0.0 quantity: int = 0.0
X(""" X("""
) )
code = 'from dataclasses import dataclass\n' + start + code code = (
"from dataclasses import dataclass\nfrom typing import ClassVar\n"
+ start
+ code
)
sig, = Script(code).get_signatures() sig, = Script(code).get_signatures()
expected_params = ( expected_params = (