Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infer_call_result() crash on methods called with_metaclass() #2118

Merged
merged 3 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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