Skip to content

Commit

Permalink
Define generators on ComprehensionScope (#1476)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord authored May 4, 2022
1 parent 8358d7d commit dd37ad3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
3 changes: 3 additions & 0 deletions astroid/nodes/scoped_nodes/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
53 changes: 23 additions & 30 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"})
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit dd37ad3

Please sign in to comment.