-
-
Notifications
You must be signed in to change notification settings - Fork 346
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 5 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 | ||
|
cdce8p marked this conversation as resolved.
|
||
|
|
||
| # 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,29 @@ 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( | ||
|
Member
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
Collaborator
Author
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
Member
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() | ||
| """ | ||
| ) | ||
| inferred = module.body[0].inferred() | ||
| assert len(inferred) == 1 | ||
| assert isinstance(inferred[0], nodes.ClassDef) | ||
| assert not inferred[0].is_dataclass | ||
| inferred = module.body[2].inferred() | ||
| assert len(inferred) == 1 | ||
| assert isinstance(inferred[0], nodes.ClassDef) | ||
| assert not inferred[0].is_dataclass | ||
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.
I would move this directly below
self.doc = doc. That way all "initial" assignments are grouped together.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.
:+1