Fix mangled __slots__ attribute lost for subclass instances (#506)#609
Open
uttam12331 wants to merge 1 commit into
Open
Fix mangled __slots__ attribute lost for subclass instances (#506)#609uttam12331 wants to merge 1 commit into
uttam12331 wants to merge 1 commit into
Conversation
`_dict_from_slots` unmangled each slot name using the concrete instance type (`type(object).__name__`). A mangled slot such as `__id` is stored under the name of the class that *declared* it (e.g. `_Parent__id`), so for a subclass instance the lookup used the wrong name (`_Child__id`), `hasattr` returned False, and the parent-defined slot was silently dropped. Pair each slot with the class in the MRO that declared it and unmangle using that class's name. Closes qlustered#506
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #506.
_dict_from_slotsunmangled each slot name using the concrete instance type:A mangled slot such as
__idis stored under the name of the class that declared it (e.g._Parent__id). For a subclass instance,type(object).__name__is the subclass, so the lookup used the wrong name (_Child__id),hasattr(...)returnedFalse, and the parent-defined slot was silently dropped:This is the root cause behind the
unprocessed/ dropped-attribute behavior reported when comparing an instance of a class with an instance of its subclass where a slot uses a mangled (dunder-prefixed) attribute — e.g.bson.ObjectIdvs a subclass of it.Fix
Pair each slot with the class in the MRO that declared it, and unmangle using that class's name. Non-mangled slots are unaffected.
Tests
Added
test_dict_from_slots_of_subclass_with_mangled_attribute, asserting the parent-declared mangled slot is collected for both the base and the subclass instance. It fails onmaster({}for the subclass) and passes with this change. The existing slots tests still pass;flake8introduces no new warnings; added a CHANGELOG entry.