Skip to content

fix: avoid crash in dunder lookup with a non-class metaclass#3070

Open
Pierre-Sassoulas wants to merge 1 commit into
mainfrom
fix-crash-3063
Open

fix: avoid crash in dunder lookup with a non-class metaclass#3070
Pierre-Sassoulas wants to merge 1 commit into
mainfrom
fix-crash-3063

Conversation

@Pierre-Sassoulas
Copy link
Copy Markdown
Member

Type of Changes

Type
🐛 Bug fix

Description

_class_lookup passed the result of ClassDef.metaclass() straight to _lookup_in_mro, which calls ancestors(). When a class declares an explicit metaclass that infers to a non-class node (e.g. a function), that node has no MRO and the lookup crashed with an AttributeError. Treat such a metaclass as if no special method was found.

Closes #3063

``_class_lookup`` passed the result of ``ClassDef.metaclass()`` straight
to ``_lookup_in_mro``, which calls ``ancestors()``. When a class declares
an explicit metaclass that infers to a non-class node (e.g. a function),
that node has no MRO and the lookup crashed with an ``AttributeError``.
Treat such a metaclass as if no special method was found.

Closes #3063
@Pierre-Sassoulas Pierre-Sassoulas added this to the 4.2.0 milestone May 20, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.52%. Comparing base (056f9ff) to head (487608d).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #3070   +/-   ##
=======================================
  Coverage   93.52%   93.52%           
=======================================
  Files          92       92           
  Lines       11329    11329           
=======================================
  Hits        10596    10596           
  Misses        733      733           
Flag Coverage Δ
linux 93.39% <100.00%> (ø)
pypy 93.52% <100.00%> (ø)
windows 93.50% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
astroid/interpreter/dunder_lookup.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError: 'FunctionDef' object has no attribute 'ancestors' in _lookup_in_mro

1 participant