attrs icon indicating copy to clipboard operation
attrs copied to clipboard

unittest mock missed attrs class when slots=False

Open noklam opened this issue 3 years ago • 1 comments

Sorry for opening the same thread in two different repositories, I did my research but couldn't find a working solution. I think this should be a common use case and I don't believe I am the first one to hit this issue.

When slots=False, Mock doesn't work. I

image

from unittest.mock import create_autospec
from attrs import frozen, field, fields, define

@define(slots=True)
class SlottedClass:
    a: int
    b: int
        
tmp = SlottedClass(1,1)

@define(slots=False)
class NonSlottedClass:
    a: int
    b: int
        
tmp = SlottedClass(1,1)

slotted = create_autospec(slotted)
non_slotted = create_autospec(NonSlottedClass)

slotted.a, slotted.b

non_slotted.a, non_slotted.b

Original Thread: https://github.com/python/cpython/issues/93133

noklam avatar May 23 '22 16:05 noklam

As you've found out in the CPython bug, it's not an attrs issue but apparently a slots issue…let's hope they fix it in a general way and not special-case DCs. :-/

hynek avatar May 25 '22 06:05 hynek

Looks like Python core doesn't care and I don't think we can do anything about this, since mock probably inspects __slots__ and __dict__?

hynek avatar Aug 11 '22 08:08 hynek