From 23666ce0a6036f023bb81e171cb1aed1843664f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Fri, 17 Sep 2021 16:33:03 +0200 Subject: [PATCH 1/2] Fix regression on ``ClassDef`` inference Closes PyCQA/pylint#5030 Closes PyCQA/pylint#5036 --- ChangeLog | 5 +++++ astroid/nodes/as_string.py | 3 ++- tests/unittest_nodes.py | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index f94dea5cb6..35d46c40c8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,11 @@ Release date: TBA Closes PyCQA/pylint#4899 +* Fix regression on ClassDef inference + + Closes PyCQA/pylint#5030 + Closes PyCQA/pylint#5036 + What's New in astroid 2.8.0? ============================ diff --git a/astroid/nodes/as_string.py b/astroid/nodes/as_string.py index 93be8fc3eb..e0b3d0d1ae 100644 --- a/astroid/nodes/as_string.py +++ b/astroid/nodes/as_string.py @@ -178,7 +178,8 @@ def visit_classdef(self, node): args = [n.accept(self) for n in node.bases] if node._metaclass and not node.has_metaclass_hack(): args.append("metaclass=" + node._metaclass.accept(self)) - args += [n.accept(self) for n in node.keywords] + if node.keywords: + args += [n.accept(self) for n in node.keywords] args = f"({', '.join(args)})" if args else "" docs = self._docs_dedent(node.doc) if node.doc else "" return "\n\n{}class {}{}:{}\n{}\n".format( diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py index 45fde769ef..323ae08f48 100644 --- a/tests/unittest_nodes.py +++ b/tests/unittest_nodes.py @@ -256,6 +256,7 @@ def check_as_string_ast_equality(code: str) -> None: def test_class_def(self) -> None: code = """ import abc +from typing import Tuple class A: @@ -275,6 +276,10 @@ class C(B): class D(metaclass=abc.ABCMeta): pass + + +def func(param: Tuple): + pass """ ast = abuilder.string_build(code) self.assertEqual(ast.as_string().strip(), code.strip()) From f2cabffff05bee53e7e591bda9f08d0c0cd63088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Fri, 17 Sep 2021 19:24:16 +0200 Subject: [PATCH 2/2] Code review changes --- astroid/nodes/as_string.py | 3 +-- astroid/nodes/scoped_nodes.py | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/astroid/nodes/as_string.py b/astroid/nodes/as_string.py index e0b3d0d1ae..93be8fc3eb 100644 --- a/astroid/nodes/as_string.py +++ b/astroid/nodes/as_string.py @@ -178,8 +178,7 @@ def visit_classdef(self, node): args = [n.accept(self) for n in node.bases] if node._metaclass and not node.has_metaclass_hack(): args.append("metaclass=" + node._metaclass.accept(self)) - if node.keywords: - args += [n.accept(self) for n in node.keywords] + args += [n.accept(self) for n in node.keywords] args = f"({', '.join(args)})" if args else "" docs = self._docs_dedent(node.doc) if node.doc else "" return "\n\n{}class {}{}:{}\n{}\n".format( diff --git a/astroid/nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes.py index 025cd8451e..e64f736e15 100644 --- a/astroid/nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes.py @@ -2096,7 +2096,8 @@ def postinit( :param keywords: The keywords given to the class definition. :type keywords: list(Keyword) or None """ - self.keywords = keywords + if keywords is not None: + self.keywords = keywords self.bases = bases self.body = body self.decorators = decorators