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

Don't transform dataclass ClassVars #914

Merged
merged 2 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Release Date: TBA

Closes #895 #899

* Don't transform dataclass ClassVars


What's New in astroid 2.5?
============================
Release Date: 2021-02-15
Expand Down
12 changes: 12 additions & 0 deletions astroid/brain/brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ def dataclass_transform(node):
if not isinstance(assign_node, (astroid.AnnAssign, astroid.Assign)):
continue

if (
isinstance(assign_node, astroid.AnnAssign)
and isinstance(assign_node.annotation, astroid.Subscript)
and (
isinstance(assign_node.annotation.value, astroid.Name)
and assign_node.annotation.value.name == "ClassVar"
or isinstance(assign_node.annotation.value, astroid.Attribute)
and assign_node.annotation.value.attrname == "ClassVar"
)
):
continue

targets = (
assign_node.targets
if hasattr(assign_node, "targets")
Expand Down
12 changes: 12 additions & 0 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,8 @@ def test_dataclasses():
code = """
import dataclasses
from dataclasses import dataclass
import typing
from typing import ClassVar

@dataclass
class InventoryItem:
Expand All @@ -2101,6 +2103,8 @@ class InventoryItem:
@dataclasses.dataclass
class Other:
name: str
CONST_1: ClassVar[int] = 42
CONST_2: typing.ClassVar[int] = 42
"""

module = astroid.parse(code)
Expand All @@ -2119,6 +2123,14 @@ class Other:
assert len(name) == 1
assert isinstance(name[0], astroid.Unknown)

const_1 = second.getattr("CONST_1")
assert len(const_1) == 1
assert isinstance(const_1[0], astroid.AssignName)

const_2 = second.getattr("CONST_2")
assert len(const_2) == 1
assert isinstance(const_2[0], astroid.AssignName)


@pytest.mark.parametrize(
"code,expected_class,expected_value",
Expand Down