Skip to content

Commit dd37ad3

Browse files
authored
Define generators on ComprehensionScope (#1476)
1 parent 8358d7d commit dd37ad3

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

astroid/nodes/scoped_nodes/mixin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,6 @@ class ComprehensionScope(LocalsDictNodeNG):
169169
"""Scoping for different types of comprehensions."""
170170

171171
scope_lookup = LocalsDictNodeNG._scope_lookup
172+
173+
generators: List["nodes.Comprehension"]
174+
"""The generators that are looped through."""

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import sys
1414
import typing
1515
import warnings
16-
from typing import Dict, List, Optional, Set, TypeVar, Union, overload
16+
from typing import TYPE_CHECKING, Dict, List, Optional, Set, TypeVar, Union, overload
1717

1818
from astroid import bases
1919
from astroid import decorators as decorators_mod
@@ -58,6 +58,9 @@
5858

5959
from astroid.decorators import cachedproperty as cached_property
6060

61+
if TYPE_CHECKING:
62+
from astroid import nodes
63+
6164

6265
ITER_METHODS = ("__iter__", "__getitem__")
6366
EXCEPTION_BASE_CLASSES = frozenset({"Exception", "BaseException"})
@@ -674,11 +677,6 @@ class GeneratorExp(ComprehensionScope):
674677
675678
:type: NodeNG or None
676679
"""
677-
generators = None
678-
"""The generators that are looped through.
679-
680-
:type: list(Comprehension) or None
681-
"""
682680

683681
def __init__(
684682
self,
@@ -721,14 +719,15 @@ def __init__(
721719
parent=parent,
722720
)
723721

724-
def postinit(self, elt=None, generators=None):
722+
def postinit(
723+
self, elt=None, generators: Optional[List["nodes.Comprehension"]] = None
724+
):
725725
"""Do some setup after initialisation.
726726
727727
:param elt: The element that forms the output of the expression.
728728
:type elt: NodeNG or None
729729
730730
:param generators: The generators that are looped through.
731-
:type generators: list(Comprehension) or None
732731
"""
733732
self.elt = elt
734733
if generators is None:
@@ -772,11 +771,6 @@ class DictComp(ComprehensionScope):
772771
773772
:type: NodeNG or None
774773
"""
775-
generators = None
776-
"""The generators that are looped through.
777-
778-
:type: list(Comprehension) or None
779-
"""
780774

781775
def __init__(
782776
self,
@@ -819,7 +813,12 @@ def __init__(
819813
parent=parent,
820814
)
821815

822-
def postinit(self, key=None, value=None, generators=None):
816+
def postinit(
817+
self,
818+
key=None,
819+
value=None,
820+
generators: Optional[List["nodes.Comprehension"]] = None,
821+
):
823822
"""Do some setup after initialisation.
824823
825824
:param key: What produces the keys.
@@ -829,7 +828,6 @@ def postinit(self, key=None, value=None, generators=None):
829828
:type value: NodeNG or None
830829
831830
:param generators: The generators that are looped through.
832-
:type generators: list(Comprehension) or None
833831
"""
834832
self.key = key
835833
self.value = value
@@ -870,11 +868,6 @@ class SetComp(ComprehensionScope):
870868
871869
:type: NodeNG or None
872870
"""
873-
generators = None
874-
"""The generators that are looped through.
875-
876-
:type: list(Comprehension) or None
877-
"""
878871

879872
def __init__(
880873
self,
@@ -917,14 +910,15 @@ def __init__(
917910
parent=parent,
918911
)
919912

920-
def postinit(self, elt=None, generators=None):
913+
def postinit(
914+
self, elt=None, generators: Optional[List["nodes.Comprehension"]] = None
915+
):
921916
"""Do some setup after initialisation.
922917
923918
:param elt: The element that forms the output of the expression.
924919
:type elt: NodeNG or None
925920
926921
:param generators: The generators that are looped through.
927-
:type generators: list(Comprehension) or None
928922
"""
929923
self.elt = elt
930924
if generators is None:
@@ -965,12 +959,6 @@ class ListComp(ComprehensionScope):
965959
:type: NodeNG or None
966960
"""
967961

968-
generators = None
969-
"""The generators that are looped through.
970-
971-
:type: list(Comprehension) or None
972-
"""
973-
974962
def __init__(
975963
self,
976964
lineno=None,
@@ -994,7 +982,9 @@ def __init__(
994982
parent=parent,
995983
)
996984

997-
def postinit(self, elt=None, generators=None):
985+
def postinit(
986+
self, elt=None, generators: Optional[List["nodes.Comprehension"]] = None
987+
):
998988
"""Do some setup after initialisation.
999989
1000990
:param elt: The element that forms the output of the expression.
@@ -1004,7 +994,10 @@ def postinit(self, elt=None, generators=None):
1004994
:type generators: list(Comprehension) or None
1005995
"""
1006996
self.elt = elt
1007-
self.generators = generators
997+
if generators is None:
998+
self.generators = []
999+
else:
1000+
self.generators = generators
10081001

10091002
def bool_value(self, context=None):
10101003
"""Determine the boolean value of this node.

0 commit comments

Comments
 (0)