diff --git a/astroid/__init__.py b/astroid/__init__.py index 41b8fffd1e..22d1618947 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -79,7 +79,7 @@ UnresolvableName, UseInferenceDefault, ) -from astroid.inference_tip import _inference_tip_cached, inference_tip +from astroid.inference_tip import inference_tip from astroid.objects import ExceptionInstance # isort: off diff --git a/astroid/inference_tip.py b/astroid/inference_tip.py index e4c54822e0..1414a1baae 100644 --- a/astroid/inference_tip.py +++ b/astroid/inference_tip.py @@ -12,7 +12,7 @@ import wrapt from astroid import bases, util -from astroid.exceptions import InferenceOverwriteError, UseInferenceDefault +from astroid.exceptions import InferenceOverwriteError from astroid.nodes import NodeNG from astroid.typing import InferFn @@ -20,30 +20,14 @@ NodeNG, bases.Instance, bases.UnboundMethod, typing.Type[util.Uninferable] ] -_cache: dict[tuple[InferFn, NodeNG], list[InferOptions] | None] = {} - - -def clear_inference_tip_cache() -> None: - """Clear the inference tips cache.""" - _cache.clear() - @wrapt.decorator -def _inference_tip_cached( +def _inference_tip( func: InferFn, instance: None, args: typing.Any, kwargs: typing.Any ) -> Iterator[InferOptions]: """Cache decorator used for inference tips""" - node = args[0] - try: - result = _cache[func, node] - # If through recursion we end up trying to infer the same - # func + node we raise here. - if result is None: - raise UseInferenceDefault() - except KeyError: - _cache[func, node] = None - result = _cache[func, node] = list(func(*args, **kwargs)) - assert result + result = list(func(*args, **kwargs)) + assert result return iter(result) @@ -84,7 +68,7 @@ def transform(node: NodeNG, infer_function: InferFn = infer_function) -> NodeNG: ) ) # pylint: disable=no-value-for-parameter - node._explicit_inference = _inference_tip_cached(infer_function) + node._explicit_inference = _inference_tip(infer_function) return node return transform diff --git a/astroid/manager.py b/astroid/manager.py index 9f88c699fa..d5115fbbc8 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -409,12 +409,9 @@ def clear_cache(self) -> None: re-register transforms.""" # import here because of cyclic imports # pylint: disable=import-outside-toplevel - from astroid.inference_tip import clear_inference_tip_cache from astroid.interpreter.objectmodel import ObjectModel from astroid.nodes.node_classes import LookupMixIn - clear_inference_tip_cache() - self.astroid_cache.clear() # NB: not a new TransformVisitor() AstroidManager.brain["_transform"].transforms = collections.defaultdict(list) diff --git a/tests/unittest_regrtest.py b/tests/unittest_regrtest.py index 806e6fe0ad..6427473bc1 100644 --- a/tests/unittest_regrtest.py +++ b/tests/unittest_regrtest.py @@ -338,6 +338,30 @@ def d(self): assert isinstance(inferred, Instance) assert inferred.qname() == ".A" + def test_inference_context_consideration(self) -> None: + # Test for https://github.com/PyCQA/astroid/issues/1828 + code = """ + class Base: + def return_type(self): + return type(self)() + + class A(Base): + def method(self): + return self.return_type() + + class B(Base): + def method(self): + return self.return_type() + + A().method() #@ + B().method() #@ + """ + node1, node2 = extract_node(code) + inferred1 = next(node1.infer()) + assert inferred1.qname() == ".A" + inferred2 = next(node2.infer()) + assert inferred2.qname() == ".B" + class Whatever: a = property(lambda x: x, lambda x: x) # type: ignore[misc] diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index 9e33de6ec0..ec1aa297da 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -1699,9 +1699,7 @@ def __init__(self): "FinalClass", "ClassB", "MixinB", - # We don't recognize what 'cls' is at time of .format() call, only - # what it is at the end. - # "strMixin", + "strMixin", "ClassA", "MixinA", "intMixin",