Skip to content
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
11 changes: 11 additions & 0 deletions guppylang/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ def register_impl(self, ty_id: DefId, name: str, impl_id: DefId) -> None:
frame = self.frames[impl_id].f_back
if frame:
self.frames[impl_id] = frame
# For Python 3.12 generic functions and classes, there is an additional
# inserted frame for the annotation scope. We can detect this frame by
# looking for the special ".generic_base" variable in the frame locals
# that is implicitly inserted by CPython. See
# - https://docs.python.org/3/reference/executionmodel.html#annotation-scopes
# - https://docs.python.org/3/reference/compound_stmts.html#generic-functions
# - https://jellezijlstra.github.io/pep695.html
if ".generic_base" in frame.f_locals:
frame = frame.f_back
assert frame is not None
self.frames[impl_id] = frame


DEF_STORE: DefinitionStore = DefinitionStore()
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/test_poly_py312.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ def main(s: MyStruct[int, float]) -> float:
validate(guppy.compile(main))


def test_inner_frame(validate):
"""See https://github.com/CQCL/guppylang/issues/1116"""
def make():
@guppy.struct
class MyStruct[T]:
@guppy
def foo(self: "MyStruct[int]") -> None:
pass

@guppy
def main() -> None:
MyStruct[int]().foo()

return main

validate(guppy.compile(make()))


def test_copy_bound(validate):
@guppy.struct
class MyStruct[T: Copy]:
Expand Down
Loading