-
Notifications
You must be signed in to change notification settings - Fork 262
fix Subscripting on classes should attempt to invoke dunder methods #259 #2282
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes issue #259 by adding support for class subscripting via metaclass __getitem__ methods. When a class is subscripted (e.g., Foo[0]), the type checker now properly looks up the __getitem__ method on the class's metaclass before falling back to type specialization.
Changes:
- Added metaclass
__getitem__lookup as a fallback between__class_getitem__(PEP 560) and type specialization - Maintained correct precedence order:
__class_getitem__→ metaclass__getitem__→ type specialization - Added regression test covering the metaclass subscripting case
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pyrefly/lib/alt/expr.rs | Implements metaclass __getitem__ lookup by refactoring the class subscripting logic to check for metaclass dunder methods after __class_getitem__ but before falling back to type specialization |
| pyrefly/lib/test/simple.rs | Adds test case test_class_metaclass_getitem to verify that subscripting a class with a metaclass that defines __getitem__ properly resolves to the return type of that method |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This comment has been minimized.
This comment has been minimized.
|
Diff from mypy_primer, showing the effect of this PR on open source code: openlibrary (https://github.com/internetarchive/openlibrary)
- ERROR openlibrary/plugins/books/readlinks.py:218:32-44: `list[tuple[str | Unknown, str | Unknown, str | Unknown]]` is not assignable to TypedDict key `tosort` with type `dict[Unknown, dict[str, list[@_] | list[Unknown] | Unknown | None]] | list[dict[str, bool | str | Unknown]]` [bad-typed-dict-key]
+ ERROR openlibrary/plugins/books/readlinks.py:218:32-44: `list[tuple[str | Unknown, str | Unknown, str | Unknown]]` is not assignable to TypedDict key `tosort` with type `dict[Unknown, dict[str, list[Unknown] | Unknown | None]] | list[dict[str, bool | str | Unknown]]` [bad-typed-dict-key]
- ::error file=openlibrary/plugins/books/readlinks.py,line=218,col=32,endLine=218,endColumn=44,title=Pyrefly bad-typed-dict-key::`list[tuple[str | Unknown, str | Unknown, str | Unknown]]` is not assignable to TypedDict key `tosort` with type `dict[Unknown, dict[str, list[@_] | list[Unknown] | Unknown | None]] | list[dict[str, bool | str | Unknown]]`
+ ::error file=openlibrary/plugins/books/readlinks.py,line=218,col=32,endLine=218,endColumn=44,title=Pyrefly bad-typed-dict-key::`list[tuple[str | Unknown, str | Unknown, str | Unknown]]` is not assignable to TypedDict key `tosort` with type `dict[Unknown, dict[str, list[Unknown] | Unknown | None]] | list[dict[str, bool | str | Unknown]]`
static-frame (https://github.com/static-frame/static-frame)
- ERROR static_frame/core/quilt.py:1007:67-76: Expected 0 type arguments for `HLoc`, got 1 [bad-specialization]
- ERROR static_frame/core/quilt.py:1164:67-88: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/core/quilt.py:1164:77-87: Expected a type form, got instance of `slice[Any, Any, Any]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1698:36-59: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1698:44-47: Expected a type form, got instance of `Literal[100]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1698:49-52: Expected a type form, got instance of `Literal['A']` [not-a-type]
- ERROR static_frame/profile/__main__.py:1698:54-58: Expected a type form, got instance of `Literal[True]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1699:36-48: Expected 0 type arguments for `HLoc`, got 1 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1699:44-47: Expected a type form, got instance of `Literal[144]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1700:36-51: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1700:44-45: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1700:47-50: Expected a type form, got instance of `Literal['|']` [not-a-type]
- ERROR static_frame/profile/__main__.py:1701:36-59: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1701:44-45: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1701:47-48: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1701:50-58: Expected a type form, got instance of `IndexHierarchyLoc.__init__.Obj` [not-a-type]
- ERROR static_frame/profile/__main__.py:1702:36-53: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1702:44-47: Expected a type form, got instance of `Literal[100]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1702:49-52: Expected a type form, got instance of `Literal['{']` [not-a-type]
- ERROR static_frame/profile/__main__.py:1703:36-58: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1703:44-47: Expected a type form, got instance of `Literal[113]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1703:49-50: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1703:52-57: Expected a type form, got instance of `Literal[False]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1704:36-57: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1704:44-45: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1704:47-50: Expected a type form, got instance of `Literal['H']` [not-a-type]
- ERROR static_frame/profile/__main__.py:1722:36-57: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1722:44-45: Expected a type form, got instance of `Literal[2]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1722:47-50: Expected a type form, got instance of `Literal['b']` [not-a-type]
- ERROR static_frame/profile/__main__.py:1722:52-56: Expected a type form, got instance of `Literal[True]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1723:36-46: Expected 0 type arguments for `HLoc`, got 1 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1723:44-45: Expected a type form, got instance of `Literal[4]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1724:36-51: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1724:44-45: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1724:47-50: Expected a type form, got instance of `Literal['a']` [not-a-type]
- ERROR static_frame/profile/__main__.py:1725:36-59: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1725:44-45: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1725:47-48: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1725:50-58: Expected a type form, got instance of `IndexHierarchyLoc.__init__.Obj` [not-a-type]
- ERROR static_frame/profile/__main__.py:1726:36-51: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1726:44-45: Expected a type form, got instance of `Literal[0]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1726:47-50: Expected a type form, got instance of `Literal['f']` [not-a-type]
- ERROR static_frame/profile/__main__.py:1727:36-56: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1727:44-45: Expected a type form, got instance of `Literal[8]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1727:47-48: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1727:50-55: Expected a type form, got instance of `Literal[False]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1728:36-57: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/profile/__main__.py:1728:44-45: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/profile/__main__.py:1728:47-50: Expected a type form, got instance of `Literal['c']` [not-a-type]
- ERROR static_frame/test/unit/test_bus.py:862:17-27: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_bus.py:862:22-23: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_bus.py:862:25-26: Expected a type form, got instance of `Literal[1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:1093:21-41: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:1093:30-31: Expected a type form, got instance of `Literal[1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:1093:34-38: Expected a type form, got instance of `Literal['dd']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2230:11-26: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2230:19-22: Expected a type form, got instance of `Literal['a']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2230:24-25: Expected a type form, got instance of `Literal[2]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2233:15-30: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2233:23-26: Expected a type form, got instance of `Literal['a']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2233:28-29: Expected a type form, got instance of `Literal[2]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2272:16-31: Expected 0 type arguments for `HLoc`, got 3 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2272:21-24: Expected a type form, got instance of `Literal['C']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2272:26-27: Expected a type form, got instance of `Literal[2]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2272:29-30: Expected a type form, got instance of `Literal[3]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2278:16-26: Expected 0 type arguments for `HLoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2278:21-24: Expected a type form, got instance of `Literal['C']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2281:12-24: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2281:17-20: Expected a type form, got instance of `Literal['C']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2281:22-23: Expected a type form, got instance of `Literal[2]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2916:13-24: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2916:21-23: Expected a type form, got instance of `Literal[-1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2930:13-24: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2930:21-23: Expected a type form, got instance of `Literal[-1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2947:33-44: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2947:41-43: Expected a type form, got instance of `Literal[-1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2959:33-44: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2959:41-43: Expected a type form, got instance of `Literal[-1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2966:13-24: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2966:21-23: Expected a type form, got instance of `Literal[-2]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2981:13-24: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2981:21-23: Expected a type form, got instance of `Literal[-2]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:2984:13-24: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:2984:21-23: Expected a type form, got instance of `Literal[-2]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:3001:17-27: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:3001:25-26: Expected a type form, got instance of `Literal[3]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:3005:17-27: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:3005:25-26: Expected a type form, got instance of `Literal[3]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:3009:17-28: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:3009:25-27: Expected a type form, got instance of `Literal[-4]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:3013:17-28: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:3013:25-27: Expected a type form, got instance of `Literal[-4]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:3265:23-51: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:3265:28-29: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:3265:32-38: Expected a type form, got instance of `Literal['muon']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:3265:40-49: Expected a type form, got instance of `Literal['strange']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:3290:23-45: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:3290:28-29: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:5038:29-44: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:5038:37-40: Expected a type form, got instance of `Literal['a']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:5038:42-43: Expected a type form, got instance of `Literal[1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:15096:20-30: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:15096:25-26: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:15096:28-29: Expected a type form, got instance of `Literal[3]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:15126:20-35: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:15126:25-26: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:15126:28-34: Expected a type form, got instance of `Literal['2018']` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:15268:20-29: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:15268:25-28: Expected a type form, got instance of `slice[Literal[-2], Literal[-2], Literal[-2] | Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:15278:32-45: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:15278:38-39: Expected a type form, got instance of `Literal[0]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:15278:41-43: Expected a type form, got instance of `Literal[-1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:17043:33-43: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:17043:41-42: Expected a type form, got instance of `Literal[0]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:17052:33-43: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:17052:41-42: Expected a type form, got instance of `Literal[0]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:21637:34-45: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:21637:42-44: Expected a type form, got instance of `slice[Literal[2], Literal[2], Literal[2] | Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:21642:13-24: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:21642:21-23: Expected a type form, got instance of `slice[Literal[2], Literal[2], Literal[2] | Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame.py:21648:17-28: Expected 0 type arguments for `ILoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame.py:21648:25-27: Expected a type form, got instance of `slice[Literal[2], Literal[2], Literal[2] | Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame_iter.py:1207:23-42: Expected 0 type arguments for `HLoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame_iter.py:1211:30-49: Expected 0 type arguments for `HLoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame_via_fill_value.py:367:39-49: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame_via_fill_value.py:367:44-45: Expected a type form, got instance of `Literal[1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame_via_fill_value.py:367:47-48: Expected a type form, got instance of `Literal[2]` [not-a-type]
- ERROR static_frame/test/unit/test_frame_via_fill_value.py:371:39-49: Expected 0 type arguments for `HLoc`, got 2 [bad-specialization]
- ERROR static_frame/test/unit/test_frame_via_fill_value.py:371:44-45: Expected a type form, got instance of `Literal[1]` [not-a-type]
- ERROR static_frame/test/unit/test_frame_via_fill_value.py:371:47-48: Expected a type form, got instance of `slice[Any, Unknown, Unknown]` [not-a-type]
- ERROR static_frame/test/unit/test_frame_via_fill_value.py:387:39-51: Expected 0 type arguments for `HLoc`, got 1 [bad-specialization]
- ERROR static_frame/test/unit/test_frame_via_fill_value.py:387:45-46: Expected a type form, got instance of `Literal[1]` [not-a-type]
... (truncated 1096 lines) ...
|
Summary
Fixes #259
Handled class subscripting via the metaclass
__getitem__before falling back to type specialization, while keeping the existing__class_getitem__precedence.Test Plan
Added a focused regression test to cover the metaclass case.