-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(commons): avoid silent add of __slots__
attrs to __dict__
#11
Merged
Conversation
This file contains 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
rmlibre
added
bug
Something isn't working
notice
attention is advised, discussion is welcome
labels
Jun 28, 2024
rmlibre
commented
Jun 29, 2024
rmlibre
changed the title
[DRAFT] v0.23.9: fix(commons): avoid silent add of
[DRAFT] fix(commons): avoid silent add of Jun 29, 2024
__slots__
attrs to __dict__
__slots__
attrs to __dict__
rmlibre
commented
Jun 29, 2024
rmlibre
changed the title
[DRAFT] fix(commons): avoid silent add of
fix(commons): avoid silent add of Jun 29, 2024
__slots__
attrs to __dict__
__slots__
attrs to __dict__
Suggested in review: #11 (comment) Partial resolve [#11]
…11] Suggested in review: #11 (comment) Partial resolve [#11]
rmlibre
added a commit
that referenced
this pull request
Jul 1, 2024
3 tasks
rmlibre
added a commit
that referenced
this pull request
Jul 5, 2024
…#14] The ``__contains__`` logic also needed to be fixed to allow the simplification in this commit. Checking ``hasattr`` wasn't a limiting enough condition, as anything in the class dictionary would also return ``True``. This led to errors when using namespaces as modules, because they'd take in attribute names like ``__doc__`` which would proc a ``PermissionError`` in frozen classes. Resolution related to (#9, #11, #14)
2 tasks
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.
Description
There's a bug in the
__init__
of theNamespace
class from thecommons
subpackage. The bug causes the silent addition of attributes that are declared in__slots__
to be added into an instance's__dict__
.This is the offending piece of code:
aiootp/aiootp/commons/namespaces.py
Lines 36 to 46 in 03b7753
Here's a demonstration of the impact:
This is bizarre behavior. However, this trickiness is expected when mixing
__slots__
&__dict__
.Expected behavior
If an attribute is defined in its or its subclass'
__slots__
, then the__init__
, & other public interfaces for setting attributes, won't add it to the instance's__dict__
. Direct usage of__dict__
should be highly discouraged, as it can lead to this unstable behavior.This is what the results should have looked like:
Remediations
__init__
from theSlots
superclass, which correctly sets attributes by using the__getitem__
interface.Slots.__init__(...):
aiootp/aiootp/commons/slots.py
Lines 74 to 75 in 03b7753
The init defers to getitem...
Slots.__getitem__(...):
aiootp/aiootp/commons/slots.py
Lines 133 to 136 in 03b7753
While getitem only sets directly to the dictionary when the name is not a
str
, which ensures it cannot have been defined in the slots.packaging
module which needs to set values directly into the__dict__
of remade modules.The code the was able to remain the same, & avoid using discouraged interfaces, in lieu of improved logic in the
Slots
initializer:aiootp/aiootp/commons/slots.py
Lines 78 to 83 in 494dc82
This was able to detect the bug in a spread of classes:
aiootp/tests/test_namespace_classes.py
Lines 217 to 244 in 494dc82
I think there should be a better warning against slot / dict misuse. Just unsure how / where
aiootp/aiootp/commons/slots.py
Lines 72 to 76 in 494dc82
Validity of assignment styles:
Commentary
These remediations may break code which depends on the bug. However, a fix is required. Let this PR serve as documentation & place of discussion around alternative or additional remediations.