summaryrefslogtreecommitdiff
path: root/astroid
diff options
context:
space:
mode:
authorNick Drozd <nicholasdrozd@gmail.com>2023-04-05 16:43:48 -0400
committerGitHub <noreply@github.com>2023-04-05 22:43:48 +0200
commitbee6e07a198613e3f559d22340f56269c960d1fa (patch)
tree2bf287c5b7731a1004d97397f6a9f5199e2d96ff /astroid
parenta8a8593979a579e2ba92ca46bb172f00c7d103c6 (diff)
downloadastroid-git-bee6e07a198613e3f559d22340f56269c960d1fa.tar.gz
Mandatory fields for Comprehension (#2099)
Diffstat (limited to 'astroid')
-rw-r--r--astroid/nodes/node_classes.py50
-rw-r--r--astroid/rebuilder.py10
2 files changed, 22 insertions, 38 deletions
diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py
index 56921d2d..7b7f532e 100644
--- a/astroid/nodes/node_classes.py
+++ b/astroid/nodes/node_classes.py
@@ -1494,52 +1494,28 @@ class Comprehension(NodeNG):
optional_assign = True
"""Whether this node optionally assigns a variable."""
- lineno: None
- col_offset: None
- end_lineno: None
- end_col_offset: None
-
- def __init__(self, parent: NodeNG | None = None) -> None:
- """
- :param parent: The parent node in the syntax tree.
- """
- self.target: NodeNG | None = None
- """What is assigned to by the comprehension."""
-
- self.iter: NodeNG | None = None
- """What is iterated over by the comprehension."""
+ target: NodeNG
+ """What is assigned to by the comprehension."""
- self.ifs: list[NodeNG] = []
- """The contents of any if statements that filter the comprehension."""
+ iter: NodeNG
+ """What is iterated over by the comprehension."""
- self.is_async: bool | None = None
- """Whether this is an asynchronous comprehension or not."""
+ ifs: list[NodeNG]
+ """The contents of any if statements that filter the comprehension."""
- super().__init__(parent=parent)
+ is_async: bool
+ """Whether this is an asynchronous comprehension or not."""
- # pylint: disable=redefined-builtin; same name as builtin ast module.
def postinit(
self,
- target: NodeNG | None = None,
- iter: NodeNG | None = None,
- ifs: list[NodeNG] | None = None,
- is_async: bool | None = None,
+ target: NodeNG,
+ iter: NodeNG, # pylint: disable = redefined-builtin
+ ifs: list[NodeNG],
+ is_async: bool,
) -> None:
- """Do some setup after initialisation.
-
- :param target: What is assigned to by the comprehension.
-
- :param iter: What is iterated over by the comprehension.
-
- :param ifs: The contents of any if statements that filter
- the comprehension.
-
- :param is_async: Whether this is an asynchronous comprehension or not.
- """
self.target = target
self.iter = iter
- if ifs is not None:
- self.ifs = ifs
+ self.ifs = ifs
self.is_async = is_async
assigned_stmts: ClassVar[AssignedStmtsCall[Comprehension]]
diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py
index 5252ac8f..61fbf444 100644
--- a/astroid/rebuilder.py
+++ b/astroid/rebuilder.py
@@ -1009,7 +1009,15 @@ class TreeRebuilder:
self, node: ast.comprehension, parent: NodeNG
) -> nodes.Comprehension:
"""Visit a Comprehension node by returning a fresh instance of it."""
- newnode = nodes.Comprehension(parent)
+ newnode = nodes.Comprehension(
+ parent=parent,
+ # Comprehension nodes don't have these attributes
+ # see https://docs.python.org/3/library/ast.html#abstract-grammar
+ lineno=None,
+ col_offset=None,
+ end_lineno=None,
+ end_col_offset=None,
+ )
newnode.postinit(
self.visit(node.target, newnode),
self.visit(node.iter, newnode),