Skip to content
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
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Release date: TBA

Closes #1330

* ``ClassDef`` now has the ``is_dataclass`` attribute.
Comment thread
DanielNoord marked this conversation as resolved.
Outdated

* Use ``sysconfig`` instead of ``distutils`` to determine the location of
python stdlib files and packages.

Expand Down
1 change: 1 addition & 0 deletions astroid/brain/brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def is_decorated_with_dataclass(node, decorator_names=DATACLASSES_DECORATORS):

def dataclass_transform(node: ClassDef) -> None:
"""Rewrite a dataclass to be easily understood by pylint"""
node.is_dataclass = True

for assign_node in _get_dataclass_attributes(node):
name = assign_node.target.name
Expand Down
3 changes: 3 additions & 0 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,9 @@ def my_meth(self, arg):
_other_other_fields = ("locals", "_newstyle")
_newstyle = None

is_dataclass = False
"""Whether this class is a dataclass."""
Comment thread
cdce8p marked this conversation as resolved.
Outdated

def __init__(
self,
name=None,
Expand Down
5 changes: 5 additions & 0 deletions tests/unittest_brain_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class A:
inferred = next(class_def.infer())
assert isinstance(inferred, nodes.ClassDef)
assert inferred.instance_attrs == {}
assert inferred.is_dataclass
Comment thread
cdce8p marked this conversation as resolved.

# Both the class and instance can still access the attribute
for node in (klass, instance):
Expand Down Expand Up @@ -216,6 +217,7 @@ class A:
inferred = next(class_def.infer())
assert isinstance(inferred, nodes.ClassDef)
assert inferred.instance_attrs == {}
assert inferred.is_dataclass

# Both the class and instance can still access the attribute
for node in (klass, instance):
Expand Down Expand Up @@ -248,6 +250,7 @@ class A:
inferred = next(class_def.infer())
assert isinstance(inferred, nodes.ClassDef)
assert inferred.instance_attrs == {}
assert inferred.is_dataclass

# Both the class and instance can still access the attribute
for node in (klass, instance):
Expand Down Expand Up @@ -666,6 +669,7 @@ class A:
inferred = node.inferred()
assert len(inferred) == 1 and isinstance(inferred[0], nodes.ClassDef)
assert "attribute" in inferred[0].instance_attrs
assert inferred[0].is_dataclass


@parametrize_module
Expand All @@ -683,3 +687,4 @@ class A:
inferred = code.inferred()
assert len(inferred) == 1
assert isinstance(inferred[0], nodes.ClassDef)
assert inferred[0].is_dataclass