Skip to content

Commit

Permalink
Fix infer_call_result() crash on methods called with_metaclass() (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored Apr 15, 2023
1 parent 495581f commit ee2d4bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ What's New in astroid 2.15.3?
=============================
Release date: TBA

* Fix ``infer_call_result()`` crash on methods called ``with_metaclass()``.

Closes #1735


What's New in astroid 2.15.2?
Expand Down
10 changes: 9 additions & 1 deletion astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,10 +1692,18 @@ def infer_call_result(self, caller=None, context: InferenceContext | None = None
# generators, and filter it out later.
if (
self.name == "with_metaclass"
and caller is not None
and len(self.args.args) == 1
and self.args.vararg is not None
):
metaclass = next(caller.args[0].infer(context), None)
if isinstance(caller.args, Arguments):
metaclass = next(caller.args.args[0].infer(context), None)
elif isinstance(caller.args, list):
metaclass = next(caller.args[0].infer(context), None)
else:
raise TypeError( # pragma: no cover
f"caller.args was neither Arguments nor list; got {type(caller.args)}"
)
if isinstance(metaclass, ClassDef):
try:
class_bases = [
Expand Down
5 changes: 5 additions & 0 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4055,6 +4055,11 @@ class C:
inferred = next(node.infer())
self.assertRaises(InferenceError, next, inferred.infer_call_result(node))

def test_infer_call_result_with_metaclass(self) -> None:
node = extract_node("def with_metaclass(meta, *bases): return 42")
inferred = next(node.infer_call_result(caller=node))
self.assertIsInstance(inferred, nodes.Const)

def test_context_call_for_context_managers(self) -> None:
ast_nodes = extract_node(
"""
Expand Down

0 comments on commit ee2d4bd

Please sign in to comment.