diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-07-03 18:15:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-03 18:15:51 +0200 |
commit | a6fdf0b92af0190dfc8334a69548919cc7130204 (patch) | |
tree | c33a18d2a6708573c974741d132cfb5804d7e667 /astroid/bases.py | |
parent | dd80a68fc93d507c3a8182ea5b94f6cbe0e348a7 (diff) | |
download | astroid-git-a6fdf0b92af0190dfc8334a69548919cc7130204.tar.gz |
Add ``__new__`` and ``__call__`` to``ObjectModel`` and ``ClassModel`` (#1606)
Diffstat (limited to 'astroid/bases.py')
-rw-r--r-- | astroid/bases.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/astroid/bases.py b/astroid/bases.py index c1607260..8ac03505 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -10,9 +10,9 @@ from __future__ import annotations import collections import collections.abc from collections.abc import Sequence -from typing import TYPE_CHECKING, Any +from typing import Any -from astroid import decorators +from astroid import decorators, nodes from astroid.const import PY310_PLUS from astroid.context import ( CallContext, @@ -33,9 +33,6 @@ objectmodel = lazy_import("interpreter.objectmodel") helpers = lazy_import("helpers") manager = lazy_import("manager") -if TYPE_CHECKING: - from astroid import nodes - # TODO: check if needs special treatment BOOL_SPECIAL_METHOD = "__bool__" @@ -271,10 +268,20 @@ class BaseInstance(Proxy): else: yield attr - def infer_call_result(self, caller, context=None): + def infer_call_result( + self, caller: nodes.Call | Proxy, context: InferenceContext | None = None + ): """infer what a class instance is returning when called""" context = bind_context_to_node(context, self) inferred = False + + # If the call is an attribute on the instance, we infer the attribute itself + if isinstance(caller, nodes.Call) and isinstance(caller.func, nodes.Attribute): + for res in self.igetattr(caller.func.attrname, context): + inferred = True + yield res + + # Otherwise we infer the call to the __call__ dunder normally for node in self._proxied.igetattr("__call__", context): if node is Uninferable or not node.callable(): continue @@ -418,9 +425,6 @@ class UnboundMethod(Proxy): ) -> collections.abc.Generator[ nodes.Const | Instance | type[Uninferable], None, None ]: - # pylint: disable-next=import-outside-toplevel; circular import - from astroid import nodes - if not caller.args: return # Attempt to create a constant |