Skip to content

Reject weak references to staticmethod, classmethod and property objects - #9906

Merged
youknowone merged 2 commits into
RustPython:mainfrom
jseop-lim:fix/staticmethod-classmethod-no-weakref
Nov 17, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
jseop-lim:fix/staticmethod-classmethod-no-weakref

Conversation

@jseop-lim

@jseop-lim jseop-lim commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

staticmethod, classmethod and property are declared with HAS_WEAKREF (staticmethod.rs#L99, classmethod.rs#L93, property.rs#L215), so weakref.ref() and weakref.proxy() accept their instances. In CPython all three types have tp_weaklistoffset == 0 (funcobject.c#L1709, #L1975, descrobject.c#L2095). property is not listed in the issue; it has the same cause. The flags date from #7334, which introduced HAS_WEAKREF and set it on the builtin types meant to support weak references.

AS-IS

>>> import weakref
>>> weakref.ref(staticmethod(len))
<weakref at 0x...; to 'staticmethod' at 0x...>
>>> weakref.ref(classmethod(len))
<weakref at 0x...; to 'classmethod' at 0x...>
>>> weakref.ref(property(len))
<weakref at 0x...; to 'property' at 0x...>

TO-BE

>>> weakref.ref(staticmethod(len))
TypeError: cannot create weak reference to 'staticmethod' object
>>> weakref.ref(classmethod(len))
TypeError: cannot create weak reference to 'classmethod' object
>>> weakref.ref(property(len))
TypeError: cannot create weak reference to 'property' object

(matching CPython)

Changes

  • Drop HAS_WEAKREF from the pyclass flags of PyStaticMethod, PyClassMethod and PyProperty. A subclass still gets a __weakref__ slot unless it sets __slots__.
  • Extend extra_tests/snippets/stdlib_weakref.py.

Test plan

Built and run on macOS aarch64 against upstream/main 7abccf0, using the CI feature set --no-default-features --features stdlib,importlib,stdio,encodings,sqlite,ssl-rustls-aws-lc,host_env,doc,ucd_3_2_0, plus threading, compared with CPython 3.15.2.

  • The extended extra_tests/snippets/stdlib_weakref.py fails on an unpatched build of that commit and passes with the patch.
  • Subclasses of staticmethod and classmethod, with and without __slots__ = (), a property subclass, functools.cached_property, abc.abstractproperty, types.DynamicClassAttribute and enum.property accept or reject weak references as CPython 3.15.2 does.
  • rustpython -m test test_weakref test_descr test_property test_functools test_abc test_enum test_types test_inspect test_typing test_dataclasses: 3,385 run, 90 skipped, all pass.
  • cargo fmt --check clean, cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targets reports nothing new (one existing warning in _ctypes/base.rs), and the ruff and rustfmt pre-commit hooks pass on the changed files.

I used Claude Code (claude-fable-5-2) to draft the analysis, the patch and this description; I reviewed the change and ran every command above myself.

Summary by CodeRabbit

  • Bug Fixes
    • staticmethod, classmethod, and property objects no longer support weak references. Attempting to create one now raises TypeError, providing consistent behavior across these built-in object types.

Both types were declared with HAS_WEAKREF, so weakref.ref() accepted
their instances. Subclasses still get a __weakref__ slot.

Assisted-by: Claude Code:claude-fable-5-2
property was declared with HAS_WEAKREF too. Subclasses still get a
__weakref__ slot.

Assisted-by: Claude Code:claude-fable-5-2
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T09:50:52.743556Z 5b136f6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The PyClassMethod, PyProperty, and PyStaticMethod types no longer declare the HAS_WEAKREF flag. Tests now expect weakref.ref to raise TypeError for instances of these types.

Changes: Weak-reference behavior

Layer / File(s) Summary
Descriptor flags and behavior test
crates/vm/src/builtins/classmethod.rs, crates/vm/src/builtins/property.rs, crates/vm/src/builtins/staticmethod.rs, extra_tests/snippets/stdlib_weakref.py
The three descriptor types no longer declare HAS_WEAKREF. Tests expect ref to raise TypeError for staticmethod, classmethod, and property objects.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related issues

  • RustPython/RustPython issue 9748: Item 2 describes weakref.ref accepting staticmethod and classmethod objects, and this change removes the flags identified as the cause.

Suggested reviewers: youknowone, shaharnaveh

Merge Risk: Minimal · up to 5b136

This change makes weak references to staticmethod, classmethod and property objects raise TypeError, as CPython does, while subclasses keep weak-reference support. No concrete merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: weak references to staticmethod, classmethod and property objects are rejected. The raw summary shows HAS_WEAKREF removed from PyStaticMethod, PyClassMethod and PyProp…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: weak references to staticmethod, classmethod and property objects are rejected. The raw summary shows HAS_WEAKREF removed from PyStaticMethod, PyClassMethod and PyProperty, and a test asserting that ref() raises TypeError for these types. The objectives describe the same behavior change, so the title matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7abccf0 and 5b136f6.

Files selected for processing: 4

  • crates/vm/src/builtins/classmethod.rs
  • crates/vm/src/builtins/property.rs
  • crates/vm/src/builtins/staticmethod.rs
  • extra_tests/snippets/stdlib_weakref.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@youknowone
youknowone merged commit 3ea800c into RustPython:main Nov 17, 2026
20 checks passed
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.

2 participants