summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-09-13 13:18:09 +0200
committerGitHub <noreply@github.com>2021-09-13 13:18:09 +0200
commit8fbef58f84dc81da69add3c3bb77ad47de8609c5 (patch)
treeacb64f9f2da16a2ff61e72427a63d75e74fbdf06
parentaf0724ba515b61dbdead945f43baf29116d44d4a (diff)
downloadastroid-git-8fbef58f84dc81da69add3c3bb77ad47de8609c5.tar.gz
Add typing to NodeNG.nodes_of_class (#1168)
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
-rw-r--r--astroid/nodes/node_ng.py62
-rw-r--r--astroid/nodes/scoped_nodes.py2
2 files changed, 53 insertions, 11 deletions
diff --git a/astroid/nodes/node_ng.py b/astroid/nodes/node_ng.py
index 19420c7f..26f1bb27 100644
--- a/astroid/nodes/node_ng.py
+++ b/astroid/nodes/node_ng.py
@@ -1,7 +1,7 @@
import pprint
import typing
from functools import singledispatch as _singledispatch
-from typing import ClassVar, Optional
+from typing import ClassVar, Iterator, Optional, Tuple, Type, TypeVar, Union, overload
from astroid import decorators, util
from astroid.exceptions import AstroidError, InferenceError, UseInferenceDefault
@@ -9,6 +9,12 @@ from astroid.manager import AstroidManager
from astroid.nodes.as_string import AsStringVisitor
from astroid.nodes.const import OP_PRECEDENCE
+# Types for 'NodeNG.nodes_of_class()'
+T_Nodes = TypeVar("T_Nodes", bound="NodeNG")
+T_Nodes2 = TypeVar("T_Nodes2", bound="NodeNG")
+T_Nodes3 = TypeVar("T_Nodes3", bound="NodeNG")
+SkipKlassT = Union[None, Type["NodeNG"], Tuple[Type["NodeNG"], ...]]
+
class NodeNG:
"""A node of the new Abstract Syntax Tree (AST).
@@ -179,12 +185,8 @@ class NodeNG:
func = getattr(visitor, "visit_" + self.__class__.__name__.lower())
return func(self)
- def get_children(self):
- """Get the child nodes below this node.
-
- :returns: The children.
- :rtype: iterable(NodeNG)
- """
+ def get_children(self) -> Iterator["NodeNG"]:
+ """Get the child nodes below this node."""
for field in self._astroid_fields:
attr = getattr(self, field)
if attr is None:
@@ -402,18 +404,56 @@ class NodeNG:
"""
self.parent.set_local(name, stmt)
- def nodes_of_class(self, klass, skip_klass=None):
+ @overload
+ def nodes_of_class(
+ self,
+ klass: Type[T_Nodes],
+ skip_klass: SkipKlassT = None,
+ ) -> Iterator[T_Nodes]:
+ ...
+
+ @overload
+ def nodes_of_class(
+ self,
+ klass: Tuple[Type[T_Nodes], Type[T_Nodes2]],
+ skip_klass: SkipKlassT = None,
+ ) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2]]:
+ ...
+
+ @overload
+ def nodes_of_class(
+ self,
+ klass: Tuple[Type[T_Nodes], Type[T_Nodes2], Type[T_Nodes3]],
+ skip_klass: SkipKlassT = None,
+ ) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2], Iterator[T_Nodes3]]:
+ ...
+
+ @overload
+ def nodes_of_class(
+ self,
+ klass: Tuple[Type[T_Nodes], ...],
+ skip_klass: SkipKlassT = None,
+ ) -> Iterator[T_Nodes]:
+ ...
+
+ def nodes_of_class( # type: ignore # mypy doesn't correctly recognize the overloads
+ self,
+ klass: Union[
+ Type[T_Nodes],
+ Tuple[Type[T_Nodes], Type[T_Nodes2]],
+ Tuple[Type[T_Nodes], Type[T_Nodes2], Type[T_Nodes3]],
+ Tuple[Type[T_Nodes], ...],
+ ],
+ skip_klass: SkipKlassT = None,
+ ) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2], Iterator[T_Nodes3]]:
"""Get the nodes (including this one or below) of the given types.
:param klass: The types of node to search for.
- :type klass: builtins.type or tuple(builtins.type)
:param skip_klass: The types of node to ignore. This is useful to ignore
subclasses of :attr:`klass`.
- :type skip_klass: builtins.type or tuple(builtins.type)
:returns: The node of the given types.
- :rtype: iterable(NodeNG)
"""
if isinstance(self, klass):
yield self
diff --git a/astroid/nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes.py
index 3b4858b4..5d52dc9f 100644
--- a/astroid/nodes/scoped_nodes.py
+++ b/astroid/nodes/scoped_nodes.py
@@ -1734,6 +1734,8 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda):
:returns: What the function yields
:rtype: iterable(NodeNG or Uninferable) or None
"""
+ # pylint: disable=not-an-iterable
+ # https://github.com/PyCQA/astroid/issues/1015
for yield_ in self.nodes_of_class(node_classes.Yield):
if yield_.value is None:
const = node_classes.Const(None)