Skip to content

fix: rewrite __class__ closure cells of methods hidden in decorators - #1618

Open
TrueFurina wants to merge 1 commit into
python-attrs:mainfrom
TrueFurina:fix/decorated-super-closure-cell
Open

fix: rewrite __class__ closure cells of methods hidden in decorators#1618
TrueFurina wants to merge 1 commit into
python-attrs:mainfrom
TrueFurina:fix/decorated-super-closure-cell

Conversation

@TrueFurina

Copy link
Copy Markdown

When a slotted class is re-created by _ClassBuilder, closure cells that reference the old class (baked in by no-arg super() or class) are rewritten to point at the new class. Methods wrapped in decorators hide their function - and thus the class cell baked into it - behind the wrapper's own closure, so those cells were never rewritten. This caused no-arg super() inside such wrapped methods to raise:

TypeError: super(type, obj): obj is not an instance or subtype of type

Collect functions referenced by other functions' closures as well, so their cells are rewritten too. Fixes #1038.

Summary

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch.
  • There's tests for all new and changed code.
  • Changes or additions to public APIs are reflected in our type stubs (files ending in .pyi).
    • ...and used in the stub test file typing_tests/baseline.py or, if necessary, typing_tests/mypy.py.
    • If they've been added to attr/__init__.pyi, they've also been re-imported in attrs/__init__.pyi.
  • The documentation has been updated.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changes to the signatures of @attr.s() and @attrs.define() have to be added by hand too.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
      The next version is the second number in the current release + 1.
      The first number represents the current year.
      So if the current version on PyPI is 26.2.0, the next version is gonna be 26.3.0.
      If the next version is the first in the new year, it'll be 27.1.0.
    • Documentation in .rst and .md files is written using semantic newlines.
  • Changes have news fragments in changelog.d.

When a slotted class is re-created by _ClassBuilder, closure cells that
reference the old class (baked in by no-arg super() or __class__) are
rewritten to point at the new class.  Methods wrapped in decorators hide
their function - and thus the __class__ cell baked into it - behind the
wrapper's own closure, so those cells were never rewritten.  This caused
no-arg super() inside such wrapped methods to raise:

  TypeError: super(type, obj): obj is not an instance or subtype of type

Collect functions referenced by other functions' closures as well, so
their cells are rewritten too.  Fixes python-attrs#1038.
@TrueFurina
TrueFurina marked this pull request as ready for review September 3, 2026 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError when calling super() in decorated method

1 participant