diff options
| author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-06-21 18:59:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-21 18:59:59 +0200 |
| commit | 6e36cd88ebc181a35e148b6418dd6b898e7b361a (patch) | |
| tree | 800e8f547d6e0721ad32bef9c7763d5a1a91fc9c | |
| parent | 5c93f84f2aa127eb34f1ca6a4961315daef7d679 (diff) | |
| download | astroid-git-6e36cd88ebc181a35e148b6418dd6b898e7b361a.tar.gz | |
Type ``safe_infer``, ``Proxy.infer`` and fix ``InferenceResult`` (#1636)
| -rw-r--r-- | astroid/bases.py | 7 | ||||
| -rw-r--r-- | astroid/helpers.py | 6 | ||||
| -rw-r--r-- | astroid/typing.py | 2 |
3 files changed, 11 insertions, 4 deletions
diff --git a/astroid/bases.py b/astroid/bases.py index 8c79f0a3..bfa60413 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -10,7 +10,7 @@ from __future__ import annotations import collections import collections.abc from collections.abc import Sequence -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any, TypeVar from astroid import decorators from astroid.const import PY310_PLUS @@ -36,6 +36,7 @@ manager = lazy_import("manager") if TYPE_CHECKING: from astroid import nodes +_ProxyT = TypeVar("_ProxyT", bound="Proxy") # TODO: check if needs special treatment BOOL_SPECIAL_METHOD = "__bool__" @@ -118,7 +119,9 @@ class Proxy: return self.__dict__[name] return getattr(self._proxied, name) - def infer(self, context=None): + def infer( + self: _ProxyT, context: InferenceContext | None = None, **kwargs: Any + ) -> collections.abc.Generator[_ProxyT, None, None]: yield self diff --git a/astroid/helpers.py b/astroid/helpers.py index 8462f87d..6924e4af 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -6,6 +6,7 @@ Various helper utilities. """ +from __future__ import annotations from astroid import bases, manager, nodes, raw_building, util from astroid.context import CallContext, InferenceContext @@ -17,6 +18,7 @@ from astroid.exceptions import ( _NonDeducibleTypeHierarchy, ) from astroid.nodes import scoped_nodes +from astroid.typing import InferenceResult def _build_proxy_class(cls_name, builtins): @@ -138,7 +140,9 @@ def object_issubclass(node, class_or_seq, context=None): return _object_type_is_subclass(node, class_or_seq, context=context) -def safe_infer(node, context=None): +def safe_infer( + node: nodes.NodeNG | bases.Proxy, context: InferenceContext | None = None +) -> InferenceResult | None: """Return the inferred value for the given node. Return None if inference failed or if there is some ambiguity (more than diff --git a/astroid/typing.py b/astroid/typing.py index 6afce566..5cb83fc9 100644 --- a/astroid/typing.py +++ b/astroid/typing.py @@ -41,4 +41,4 @@ class AstroidManagerBrain(TypedDict): _transform: transforms.TransformVisitor -InferenceResult = Union["nodes.NodeNG", "type[util.Uninferable]", "bases.Instance"] +InferenceResult = Union["nodes.NodeNG", "type[util.Uninferable]", "bases.Proxy"] |
