summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-10-04 11:58:19 +0200
committerGitHub <noreply@github.com>2022-10-04 11:58:19 +0200
commit1ffe4001633488c8c0cf1160d98793bbe3f79da9 (patch)
treecf797ea2847e031ed8f4f6e353abb7ba660cf174 /tests
parent2a1b0d346ab0a2d2c3ec6753d21eb03f83504550 (diff)
downloadastroid-git-1ffe4001633488c8c0cf1160d98793bbe3f79da9.tar.gz
Fix regression in the creation of the ``__init__`` of dataclasses (#1812)
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_brain_dataclasses.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/unittest_brain_dataclasses.py b/tests/unittest_brain_dataclasses.py
index 7d69b359..a65a8dec 100644
--- a/tests/unittest_brain_dataclasses.py
+++ b/tests/unittest_brain_dataclasses.py
@@ -918,6 +918,7 @@ def test_dataclass_with_multiple_inheritance() -> None:
"""Regression test for dataclasses with multiple inheritance.
Reported in https://github.com/PyCQA/pylint/issues/7427
+ Reported in https://github.com/PyCQA/pylint/issues/7434
"""
first, second, overwritten, overwriting, mixed = astroid.extract_node(
"""
@@ -991,6 +992,75 @@ def test_dataclass_with_multiple_inheritance() -> None:
assert [a.name for a in mixed_init.args.args] == ["self", "_abc", "ghi"]
assert [a.value for a in mixed_init.args.defaults] == [1, 3]
+ first = astroid.extract_node(
+ """
+ from dataclasses import dataclass
+
+ @dataclass
+ class BaseParent:
+ required: bool
+
+ @dataclass
+ class FirstChild(BaseParent):
+ ...
+
+ @dataclass
+ class SecondChild(BaseParent):
+ optional: bool = False
+
+ @dataclass
+ class GrandChild(FirstChild, SecondChild):
+ ...
+
+ GrandChild.__init__ #@
+ """
+ )
+
+ first_init: bases.UnboundMethod = next(first.infer())
+ assert [a.name for a in first_init.args.args] == ["self", "required", "optional"]
+ assert [a.value for a in first_init.args.defaults] == [False]
+
+
+@pytest.mark.xfail(reason="Transforms returning Uninferable isn't supported.")
+def test_dataclass_non_default_argument_after_default() -> None:
+ """Test that a non-default argument after a default argument is not allowed.
+
+ This should succeed, but the dataclass brain is a transform
+ which currently can't return an Uninferable correctly. Therefore, we can't
+ set the dataclass ClassDef node to be Uninferable currently.
+ Eventually it can be merged into test_dataclass_with_multiple_inheritance.
+ """
+
+ impossible = astroid.extract_node(
+ """
+ from dataclasses import dataclass
+
+ @dataclass
+ class BaseParent:
+ required: bool
+
+ @dataclass
+ class FirstChild(BaseParent):
+ ...
+
+ @dataclass
+ class SecondChild(BaseParent):
+ optional: bool = False
+
+ @dataclass
+ class ThirdChild:
+ other: bool = False
+
+ @dataclass
+ class ImpossibleGrandChild(FirstChild, SecondChild, ThirdChild):
+ ...
+
+ ImpossibleGrandChild() #@
+ """
+ )
+
+ assert next(impossible.infer()) is Uninferable
+
def test_dataclass_inits_of_non_dataclasses() -> None:
"""Regression test for __init__ mangling for non dataclasses.