Repository navigation
Reject weak references to staticmethod, classmethod and property objects - #9906
youknowone merged 2 commits into
Conversation
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
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Summary
staticmethod,classmethodandpropertyare declared withHAS_WEAKREF(staticmethod.rs#L99,classmethod.rs#L93,property.rs#L215), soweakref.ref()andweakref.proxy()accept their instances. In CPython all three types havetp_weaklistoffset == 0(funcobject.c#L1709,#L1975,descrobject.c#L2095).propertyis not listed in the issue; it has the same cause. The flags date from #7334, which introducedHAS_WEAKREFand set it on the builtin types meant to support weak references.AS-IS
TO-BE
(matching CPython)
Changes
HAS_WEAKREFfrom thepyclassflags ofPyStaticMethod,PyClassMethodandPyProperty. A subclass still gets a__weakref__slot unless it sets__slots__.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, plusthreading, compared with CPython 3.15.2.extra_tests/snippets/stdlib_weakref.pyfails on an unpatched build of that commit and passes with the patch.staticmethodandclassmethod, with and without__slots__ = (), apropertysubclass,functools.cached_property,abc.abstractproperty,types.DynamicClassAttributeandenum.propertyaccept 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 --checkclean,cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targetsreports 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
staticmethod,classmethod, andpropertyobjects no longer support weak references. Attempting to create one now raisesTypeError, providing consistent behavior across these built-in object types.