-
-
Notifications
You must be signed in to change notification settings - Fork 296
Add is_dataclass
attribute to ClassDef
#1391
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
Changes from all commits
f127726
6904e3d
2d1a80c
382ca86
c73bf43
dc18800
70c2d2f
94c6a9c
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 |
---|---|---|
|
@@ -184,6 +184,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): | ||
|
@@ -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): | ||
|
@@ -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): | ||
|
@@ -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 | ||
|
@@ -683,3 +687,30 @@ class A: | |
inferred = code.inferred() | ||
assert len(inferred) == 1 | ||
assert isinstance(inferred[0], nodes.ClassDef) | ||
assert inferred[0].is_dataclass | ||
|
||
|
||
def test_non_dataclass_is_not_dataclass() -> None: | ||
"""Test that something that isn't a dataclass has the correct attribute.""" | ||
module = astroid.parse( | ||
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. You could also use 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. I'll change it. I always use 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. Both work fine. |
||
""" | ||
class A: | ||
val: field() | ||
|
||
def dataclass(): | ||
return | ||
|
||
@dataclass | ||
class B: | ||
val: field() | ||
""" | ||
) | ||
class_a = module.body[0].inferred() | ||
assert len(class_a) == 1 | ||
assert isinstance(class_a[0], nodes.ClassDef) | ||
assert not class_a[0].is_dataclass | ||
|
||
class_b = module.body[2].inferred() | ||
assert len(class_b) == 1 | ||
assert isinstance(class_b[0], nodes.ClassDef) | ||
assert not class_b[0].is_dataclass |
Uh oh!
There was an error while loading. Please reload this page.