Repository navigation
A slotted class can be referenced weakly - #336
Conversation
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| UnusedCode | 6 minor |
| Documentation | 1 minor |
🟢 Metrics 22 complexity · 0 duplication
Metric Results Complexity 22 Duplication 0
🟢 Coverage 100.00% diff coverage · +0.02% coverage variation
Metric Results Coverage variation ✅ +0.02% coverage variation Diff coverage ✅ 100.00% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (6939868) 15514 13442 86.64% Head commit (af6d1f1) 15528 (+14) 13458 (+16) 86.67% (+0.02%) Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#336) 16 16 100.00% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
❌ 2 Tests Failed:
View the top 2 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
d1c222f to
a5cc534
Compare
|
🪲 This was red on all four PyPy 3.11 jobs, and the tests were wrong, not the feature. Fixed and pushed. Both encoded CPython behaviour rather than the feature's:
The feature itself needed no change: Worth noting for the release: PyPy is the only place this difference shows, and it took CI to find it — this container has no PyPy interpreter. |
6beb632 to
cf1f113
Compare
cf1f113 to
5ce4fc1
Compare
2482203 to
bb51986
Compare
bb51986 to
bfa65b5
Compare
Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
Two of them encoded CPython's behaviour rather than the feature's, and failed on all four PyPy 3.11 jobs: * 'A slotted class is not weak-referenceable by default' is false on PyPy - every object there is weak-referenceable, slots or not. Split in two: the slot is absent everywhere, and the TypeError that follows from it is asserted on CPython only. * 'The reference dies with the object' assumed reference counting. PyPy collects generationally, so 'del' alone leaves the object alive; 'gc.collect()' makes it portable. The documentation now says the restriction being lifted is CPython's, so the keyword reads as what it is on PyPy: harmless, and still required for the CPython case. Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
bfa65b5 to
0e5d49b
Compare
…an't be annotated * 'weakref=True' on a mixin-class adds '__weakref__' to its mixin slots. The class it's mixed into lists it once, and not at all if a base-class already provides it. * Annotating '__weakref__' raises an ExtendedTypeError, with a note pointing to 'weakref=True'. * Annotating '__dict__' raises an ExtendedTypeError, with a note pointing to 'slots=False'. Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
* 'weakref=True' adds '__weakref__' to the fields the annotations declare, so the existing duplicate check and the mixin-slot aggregation handle it. The loops over base-classes and the special cases for mixin-classes are gone. * Asking again in a derived class is an AttributeError now, like any other duplicate slot. A mixin-class asking for it on a class that has it already fails with Python's TypeError. * 'weakref' is ordered after 'mixin' and before 'singleton', and '_computeSlots' has no default for it. * The '__dict__' todo is solved: annotating '__dict__' is rejected. Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
New Features
A slotted class can be referenced weakly, with the new
weakrefclass keyword argument — closes#49.
A class using
__slots__has no__dict__forweakrefto keep its bookkeeping in, and the slot that wouldhold it is not created unless it is asked for:
The typical reason to want it is a back-reference — a child pointing at its parent keeps the parent alive as
long as the child lives, and a weak reference is how that cycle is avoided.
__weakref__is requested like any other slot.weakref=Trueadds it to the fields the annotationsdeclare, so the existing duplicate check handles it: a derived class inherits the capability, and asking again
is an
AttributeError, like any other duplicate slot:A mixin-class can ask for it too.
__weakref__becomes one of its mixin slots and is added to the class itis mixed into. If that class has it already, Python rejects the second one with
TypeError: __weakref__ slot disallowed.__weakref__and__dict__can't be annotated. Both raise anExtendedTypeError, whose note points to theintended spelling:
weakref=Truefor__weakref__,slots=Falsefor__dict__. Annotating__dict__in aslotted class used to work silently, and it gave up what slots save.
Changes
ExtendedType's.. todo::on__dict__and__weakref__is removed:__weakref__isweakref=True, and__dict__in a slotted class is rejected.weakrefis ordered aftermixinand beforesingleton.Documentation
doc/MetaClasses.rst— a new Weak References section: why a slotted class is not weak-referenceable, theexample, the once-per-hierarchy rule, mixin-classes, the rejected annotations, and the note that a class without
slots already is.
Unit Tests
tests/unit/MetaClasses/WeakReferences.py— 17 testcases:WeakReferences(12): a slotted class is not weak-referenceable by default, the keyword adds the slot, aninstance can be referenced, the reference dies with the object, the data fields still work, a derived class
inherits it without repeating it, asking twice is an error, a mixin's fields are unaffected, a mixin-class
contributes it and passes it through a mixin-class of mixin-classes, and asking in a mixin-class for a class
that has it already is an error.
AnnotatedWeakref(3),AnnotatedDict(2): the annotation is rejected in a slotted class, a mixin-class and,for
__weakref__, an unslotted class.Related Issues and Pull-Requests
__weakref__in types withslots", open since 2022-05-14. Its other half,
__dict__in slotted types, was struck out in the descriptionand is not addressed here.