Skip to content

Commit

Permalink
Use the inference cache when no context is provided (#2257)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored Jul 19, 2023
1 parent 3f812fa commit cf8763a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ Release date: TBA
Closes pylint-dev/pylint#7464
Closes pylint-dev/pylint#8074

* Use the global inference cache when inferring, even without an explicit
``InferenceContext``. This is a significant performance improvement given how
often methods default to ``None`` for the context argument. (Linting ``astroid``
itself now takes ~5% less time on Python 3.12; other projects requiring more
complex inference calculations will see greater speedups.)

Refs #529

* Fix interrupted ``InferenceContext`` call chains, thereby addressing performance
problems when linting ``sqlalchemy``.

Expand Down
16 changes: 3 additions & 13 deletions astroid/nodes/node_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,13 @@ def infer(
:returns: The inferred values.
:rtype: iterable
"""
if context is not None:
if context is None:
context = InferenceContext()
else:
context = context.extra_context.get(self, context)
if self._explicit_inference is not None:
# explicit_inference is not bound, give it self explicitly
try:
if context is None:
yield from self._explicit_inference(
self, # type: ignore[arg-type]
context,
**kwargs,
)
return
for result in self._explicit_inference(
self, # type: ignore[arg-type]
context,
Expand All @@ -161,11 +156,6 @@ def infer(
except UseInferenceDefault:
pass

if not context:
# nodes_inferred?
yield from self._infer(context=context, **kwargs)
return

key = (self, context.lookupname, context.callcontext, context.boundnode)
if key in context.inferred:
yield from context.inferred[key]
Expand Down
18 changes: 18 additions & 0 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -6157,6 +6157,24 @@ class InferMeTwice:
assert util.Uninferable not in instance.igetattr("item", context_to_be_used_twice)


@patch("astroid.nodes.Call._infer")
def test_cache_usage_without_explicit_context(mock) -> None:
code = """
class InferMeTwice:
item = 10
InferMeTwice()
"""
call = extract_node(code)
mock.return_value = [Uninferable]

# no explicit InferenceContext
call.inferred()
call.inferred()

mock.assert_called_once()


def test_infer_context_manager_with_unknown_args() -> None:
code = """
class client_log(object):
Expand Down

0 comments on commit cf8763a

Please sign in to comment.