Skip to content

fix: handle non-callable __class_getitem__ in ClassDef.getitem#3073

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

fix: handle non-callable __class_getitem__ in ClassDef.getitem#3073
Pierre-Sassoulas wants to merge 1 commit into
mainfrom
fix-crash-3064

Conversation

@Pierre-Sassoulas
Copy link
Copy Markdown
Member

Type of Changes

Type
🐛 Bug fix

Description

When subscripting a class, getitem falls back to __class_getitem__ and assumes the resolved node is callable. If it instead resolves to a non-callable node such as an AssignName (e.g. dataclasses.Field, whose __class_getitem__ is assigned rather than def-ined), calling infer_call_result raised an AttributeError that escaped inference. Raise AstroidTypeError instead, matching getitem's documented contract.

Closes #3064

When subscripting a class, ``getitem`` falls back to ``__class_getitem__``
and assumes the resolved node is callable. If it instead resolves to a
non-callable node such as an ``AssignName`` (e.g. ``dataclasses.Field``,
whose ``__class_getitem__`` is assigned rather than ``def``-ined), calling
``infer_call_result`` raised an ``AttributeError`` that escaped inference.
Raise ``AstroidTypeError`` instead, matching ``getitem``'s documented
contract.

Closes #3064
@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 (b80f4db).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3073      +/-   ##
==========================================
- Coverage   93.52%   93.52%   -0.01%     
==========================================
  Files          92       92              
  Lines       11329    11331       +2     
==========================================
+ Hits        10596    10597       +1     
- Misses        733      734       +1     
Flag Coverage Δ
linux 93.38% <100.00%> (-0.01%) ⬇️
pypy 93.52% <100.00%> (-0.01%) ⬇️
windows 93.49% <100.00%> (-0.01%) ⬇️

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

Files with missing lines Coverage Δ
astroid/nodes/scoped_nodes/scoped_nodes.py 93.26% <100.00%> (-0.07%) ⬇️
🚀 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: 'AssignName' object has no attribute 'infer_call_result' in getitem

1 participant