question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

__slots__ are ignored

See original GitHub issue

Hey 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:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Fatal1tycommented, Apr 8, 2021

Fixed in 2.2.

0reactions
zupocommented, Apr 8, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found