__slots__ are ignored
See original GitHub issueHey there! First, thanks for this great library, I am really enjoying using it!
Sometimes I like to use __slots__
with dataclasses to prevent me from accidentally setting a value to a (typo-ed) new attribute instead of to an existing attribute.
However, if I use the DataClassDictMixin
from mashumaro, it seems like __slots__
are ignored. I.e. If I set a value to a non-existing attribute, I do not get AttributeError, but a new attribute is created and value is set to it, as if __slots__
would not exist.
Here’s a short pytest case to showcase the problem:
import pytest
from dataclasses import dataclass
from mashumaro import DataClassDictMixin
@dataclass
class Foo:
__slots__ = ["number"]
number: int
@dataclass
class Bar(DataClassDictMixin):
__slots__ = ["number"]
number: int
def test_slots():
foo = Foo(1)
with pytest.raises(AttributeError) as err:
foo.new_attribute = 2
assert str(err.value) == "'Foo' object has no attribute 'new_attribute'"
bar = Bar(1)
bar.new_attribute = 2
# -> should also fail with "'Bar' object has no attribute 'new_attribute'",
# but it doesn't
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
When and why does Python ignore __slots__? - Stack Overflow
But the answer I accepted, says that Usage of __slots__? has the answer I want inside it, which could be true. While coding...
Read more >Other slots are ignored in full page components #2881 - GitHub
With a full-page component, that header slot is going to be outside of the main Livewire component's root (inside of the default slot...
Read more >Equipment slots: once ignored, always ignored - Bug Report
I made some sets in the Equipment Manager. (I do use an addon to show me which set is equipped, and to change...
Read more >Some slot advice is worth ignoring | Gaming Guru
In more than 30 years as a player, I've received a lot of advice, some good, some bad, some indifferent.
Read more >Casinos Bet On Change After Younger Players Ignore 'Boring ...
When young people go to casinos, they aren't playing slot machines. Our Planet Money team talks to a man who thinks he can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Fixed in 2.2.
https://github.com/Fatal1ty/mashumaro/commit/543356e4add2e215f90d36e011efc489e0d2e936 is definitely needed.
I was not using the correct branch when I first tested (I thought I did) and now I re-checked and works as expected with the
slots
branch.I closed the issue by mistake. Getting late, I should go to bed.