From dd37ad37442d9c6114f71e2ee1f9637b79f1de6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 4 May 2022 11:03:18 +0200 Subject: [PATCH] Define ``generators`` on ``ComprehensionScope`` (#1476) --- astroid/nodes/scoped_nodes/mixin.py | 3 ++ astroid/nodes/scoped_nodes/scoped_nodes.py | 53 ++++++++++------------ 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/astroid/nodes/scoped_nodes/mixin.py b/astroid/nodes/scoped_nodes/mixin.py index bb1e76f14f..99dc5e58d9 100644 --- a/astroid/nodes/scoped_nodes/mixin.py +++ b/astroid/nodes/scoped_nodes/mixin.py @@ -169,3 +169,6 @@ class ComprehensionScope(LocalsDictNodeNG): """Scoping for different types of comprehensions.""" scope_lookup = LocalsDictNodeNG._scope_lookup + + generators: List["nodes.Comprehension"] + """The generators that are looped through.""" diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index 8d63d277f0..4762ed223d 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -13,7 +13,7 @@ import sys import typing import warnings -from typing import Dict, List, Optional, Set, TypeVar, Union, overload +from typing import TYPE_CHECKING, Dict, List, Optional, Set, TypeVar, Union, overload from astroid import bases from astroid import decorators as decorators_mod @@ -58,6 +58,9 @@ from astroid.decorators import cachedproperty as cached_property +if TYPE_CHECKING: + from astroid import nodes + ITER_METHODS = ("__iter__", "__getitem__") EXCEPTION_BASE_CLASSES = frozenset({"Exception", "BaseException"}) @@ -674,11 +677,6 @@ class GeneratorExp(ComprehensionScope): :type: NodeNG or None """ - generators = None - """The generators that are looped through. - - :type: list(Comprehension) or None - """ def __init__( self, @@ -721,14 +719,15 @@ def __init__( parent=parent, ) - def postinit(self, elt=None, generators=None): + def postinit( + self, elt=None, generators: Optional[List["nodes.Comprehension"]] = None + ): """Do some setup after initialisation. :param elt: The element that forms the output of the expression. :type elt: NodeNG or None :param generators: The generators that are looped through. - :type generators: list(Comprehension) or None """ self.elt = elt if generators is None: @@ -772,11 +771,6 @@ class DictComp(ComprehensionScope): :type: NodeNG or None """ - generators = None - """The generators that are looped through. - - :type: list(Comprehension) or None - """ def __init__( self, @@ -819,7 +813,12 @@ def __init__( parent=parent, ) - def postinit(self, key=None, value=None, generators=None): + def postinit( + self, + key=None, + value=None, + generators: Optional[List["nodes.Comprehension"]] = None, + ): """Do some setup after initialisation. :param key: What produces the keys. @@ -829,7 +828,6 @@ def postinit(self, key=None, value=None, generators=None): :type value: NodeNG or None :param generators: The generators that are looped through. - :type generators: list(Comprehension) or None """ self.key = key self.value = value @@ -870,11 +868,6 @@ class SetComp(ComprehensionScope): :type: NodeNG or None """ - generators = None - """The generators that are looped through. - - :type: list(Comprehension) or None - """ def __init__( self, @@ -917,14 +910,15 @@ def __init__( parent=parent, ) - def postinit(self, elt=None, generators=None): + def postinit( + self, elt=None, generators: Optional[List["nodes.Comprehension"]] = None + ): """Do some setup after initialisation. :param elt: The element that forms the output of the expression. :type elt: NodeNG or None :param generators: The generators that are looped through. - :type generators: list(Comprehension) or None """ self.elt = elt if generators is None: @@ -965,12 +959,6 @@ class ListComp(ComprehensionScope): :type: NodeNG or None """ - generators = None - """The generators that are looped through. - - :type: list(Comprehension) or None - """ - def __init__( self, lineno=None, @@ -994,7 +982,9 @@ def __init__( parent=parent, ) - def postinit(self, elt=None, generators=None): + def postinit( + self, elt=None, generators: Optional[List["nodes.Comprehension"]] = None + ): """Do some setup after initialisation. :param elt: The element that forms the output of the expression. @@ -1004,7 +994,10 @@ def postinit(self, elt=None, generators=None): :type generators: list(Comprehension) or None """ self.elt = elt - self.generators = generators + if generators is None: + self.generators = [] + else: + self.generators = generators def bool_value(self, context=None): """Determine the boolean value of this node.