summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2023-01-17 23:13:35 +0100
committerGitHub <noreply@github.com>2023-01-17 23:13:35 +0100
commit29834bf2cabd77ea60ac1155188ad486e1222c87 (patch)
tree4b49b7a70effa59074abee70dd1d0e27514faaa3 /tests
parent6898ad688aa204ac2b6160182f6823bcc72115a9 (diff)
downloadastroid-git-29834bf2cabd77ea60ac1155188ad486e1222c87.tar.gz
Fix order of overwritten attributes in inherited dataclasses (#1970)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_brain_dataclasses.py69
1 files changed, 65 insertions, 4 deletions
diff --git a/tests/unittest_brain_dataclasses.py b/tests/unittest_brain_dataclasses.py
index 6de25b06..34b7b7e4 100644
--- a/tests/unittest_brain_dataclasses.py
+++ b/tests/unittest_brain_dataclasses.py
@@ -376,6 +376,67 @@ def test_inference_inherited(module: str):
assert inferred[1].name == "str"
+def test_dataclass_order_of_inherited_attributes():
+ """Test that an attribute in a child does not get put at the end of the init."""
+ child, normal, keyword_only = astroid.extract_node(
+ """
+ from dataclass import dataclass
+
+
+ @dataclass
+ class Parent:
+ a: str
+ b: str
+
+
+ @dataclass
+ class Child(Parent):
+ c: str
+ a: str
+
+
+ @dataclass(kw_only=True)
+ class KeywordOnlyParent:
+ a: int
+ b: str
+
+
+ @dataclass
+ class NormalChild(KeywordOnlyParent):
+ c: str
+ a: str
+
+
+ @dataclass(kw_only=True)
+ class KeywordOnlyChild(KeywordOnlyParent):
+ c: str
+ a: str
+
+
+ Child.__init__ #@
+ NormalChild.__init__ #@
+ KeywordOnlyChild.__init__ #@
+ """
+ )
+ child_init: bases.UnboundMethod = next(child.infer())
+ assert [a.name for a in child_init.args.args] == ["self", "a", "b", "c"]
+
+ normal_init: bases.UnboundMethod = next(normal.infer())
+ if PY310_PLUS:
+ assert [a.name for a in normal_init.args.args] == ["self", "a", "c"]
+ assert [a.name for a in normal_init.args.kwonlyargs] == ["b"]
+ else:
+ assert [a.name for a in normal_init.args.args] == ["self", "a", "b", "c"]
+ assert [a.name for a in normal_init.args.kwonlyargs] == []
+
+ keyword_only_init: bases.UnboundMethod = next(keyword_only.infer())
+ if PY310_PLUS:
+ assert [a.name for a in keyword_only_init.args.args] == ["self"]
+ assert [a.name for a in keyword_only_init.args.kwonlyargs] == ["a", "b", "c"]
+ else:
+ assert [a.name for a in keyword_only_init.args.args] == ["self", "a", "b", "c"]
+
+
def test_pydantic_field() -> None:
"""Test that pydantic.Field attributes are currently Uninferable.
@@ -628,12 +689,12 @@ def test_init_attributes_from_superclasses(module: str):
"""
)
init = next(node.infer())
- assert [a.name for a in init.args.args] == ["self", "arg0", "arg1", "arg2"]
+ assert [a.name for a in init.args.args] == ["self", "arg0", "arg2", "arg1"]
assert [a.as_string() if a else None for a in init.args.annotations] == [
None,
"float",
- "int",
"list", # not str
+ "int",
]
@@ -1035,8 +1096,8 @@ def test_dataclass_with_multiple_inheritance() -> None:
assert [a.value for a in overwritten_init.args.defaults] == ["2"]
overwriting_init: bases.UnboundMethod = next(overwriting.infer())
- assert [a.name for a in overwriting_init.args.args] == ["self", "_abc", "ef"]
- assert [a.value for a in overwriting_init.args.defaults] == [1.0, 2.0]
+ assert [a.name for a in overwriting_init.args.args] == ["self", "ef", "_abc"]
+ assert [a.value for a in overwriting_init.args.defaults] == [2.0, 1.0]
mixed_init: bases.UnboundMethod = next(mixed.infer())
assert [a.name for a in mixed_init.args.args] == ["self", "_abc", "ghi"]