diff options
author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2023-04-30 17:41:20 +0200 |
---|---|---|
committer | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2023-04-30 22:27:15 +0200 |
commit | e04a3f98e9c422519dc5deb2b074017b20b053bf (patch) | |
tree | c32db596f1ba78f0d964aa25869b761c23fd9514 | |
parent | 706cdc9f61a533644c5b455517eb0263defe56b0 (diff) | |
download | astroid-git-e04a3f98e9c422519dc5deb2b074017b20b053bf.tar.gz |
Complete typing of ``context.py``
-rw-r--r-- | astroid/context.py | 44 |
1 files changed, 14 insertions, 30 deletions
diff --git a/astroid/context.py b/astroid/context.py index 070c6f50..a151ca62 100644 --- a/astroid/context.py +++ b/astroid/context.py @@ -8,9 +8,10 @@ from __future__ import annotations import contextlib import pprint +from collections.abc import Iterator from typing import TYPE_CHECKING, Dict, Optional, Sequence, Tuple -from astroid.typing import InferenceResult +from astroid.typing import InferenceResult, SuccessfulInferenceResult if TYPE_CHECKING: from astroid import constraint, nodes @@ -48,19 +49,16 @@ class InferenceContext: def __init__( self, - path=None, + path: set[tuple[nodes.NodeNG, str | None]] | None = None, nodes_inferred: list[int] | None = None, - ): + ) -> None: if nodes_inferred is None: self._nodes_inferred = [0] else: self._nodes_inferred = nodes_inferred self.path = path or set() - """ - :type: set(tuple(NodeNG, optional(str))) - - Path of visited nodes and their lookupname + """Path of visited nodes and their lookupname. Currently this key is ``(node, context.lookupname)`` """ @@ -73,21 +71,13 @@ class InferenceContext: """ self.callcontext: CallContext | None = None """The call arguments and keywords for the given context.""" - self.boundnode = None - """ - :type: optional[NodeNG] - - The bound node of the given context + self.boundnode: SuccessfulInferenceResult | None = None + """The bound node of the given context. e.g. the bound node of object.__new__(cls) is the object node """ - self.extra_context = {} - """ - :type: dict(NodeNG, Context) - - Context that needs to be passed down through call stacks - for call arguments - """ + self.extra_context: dict[SuccessfulInferenceResult, InferenceContext] = {} + """Context that needs to be passed down through call stacks for call arguments.""" self.constraints: dict[str, dict[nodes.If, set[constraint.Constraint]]] = {} """The constraints on nodes.""" @@ -116,11 +106,9 @@ class InferenceContext: """ return _INFERENCE_CACHE - def push(self, node) -> bool: + def push(self, node: nodes.NodeNG) -> bool: """Push node into inference path. - :return: Whether node is already in context path. - Allows one to see if the given node has already been looked at for this inference context """ @@ -147,7 +135,7 @@ class InferenceContext: return clone @contextlib.contextmanager - def restore_path(self): + def restore_path(self) -> Iterator[None]: path = set(self.path) yield self.path = path @@ -188,19 +176,15 @@ def copy_context(context: InferenceContext | None) -> InferenceContext: return InferenceContext() -def bind_context_to_node(context: InferenceContext | None, node) -> InferenceContext: +def bind_context_to_node( + context: InferenceContext | None, node: SuccessfulInferenceResult +) -> InferenceContext: """Give a context a boundnode to retrieve the correct function name or attribute value with from further inference. Do not use an existing context since the boundnode could then be incorrectly propagated higher up in the call stack. - - :param node: Node to do name lookups from - :type node NodeNG: - - :returns: A new context - :rtype: InferenceContext """ context = copy_context(context) context.boundnode = node |