Skip to content
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

unittest mock miss attributes with AttributeError with dataclasses and attrs class when slots=False? #93133

Open
noklam opened this issue May 23, 2022 · 5 comments
Labels
tests type-bug

Comments

@noklam
Copy link

@noklam noklam commented May 23, 2022

Bug report

A clear and concise description of what the bug is.
Include a minimal, reproducible example (https://stackoverflow.com/help/minimal-reproducible-example), if possible.
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

Your environment

  • CPython versions tested on:
  • Operating system and architecture: MacOS
  • attrs: '21.4.0'
  • python: 3.8.3
@AA-Turner
Copy link
Member

@AA-Turner AA-Turner commented May 23, 2022

Please confirm that this isn't an issue with attrs -- there's a lot of magic behaviour there.

A

@erlend-aasland erlend-aasland added the tests label May 23, 2022
@ericvsmith
Copy link
Member

@ericvsmith ericvsmith commented May 24, 2022

It would also be good to try and reproduce it with dataclasses, since they have a lot of the same magic. If it failed with dataclasses then we could look at the interaction between mock and dataclasses more easily than if it requires attrs.

@noklam
Copy link
Author

@noklam noklam commented May 24, 2022

It did fail with dataclasses too, I can supplement an example later, in commute now.

@noklam
Copy link
Author

@noklam noklam commented May 25, 2022

I have updated the title of the issues since it may not be an attrs only problem. @AA-Turner @ericvsmith, thank you.

A similar example with dataclasses below.

from dataclasses import dataclass
from unittest.mock import create_autospec

@dataclass
class DummyClass:
    a: int
    b: int
        
dummy_class = create_autospec(DummyClass)
dummy_class.a

     636         elif self._mock_methods is not None:
    637             if name not in self._mock_methods or name in _all_magics:
--> 638                 raise AttributeError("Mock object has no attribute %r" % name)
    639         elif _is_magic(name):
    640             raise AttributeError(name)

AttributeError: Mock object has no attribute 'a'

@noklam noklam changed the title unittest mock missed attrs class when slots=False? unittest mock miss attributes with AttributeError with dataclasses and attrs class when slots=False? May 25, 2022
@ericvsmith
Copy link
Member

@ericvsmith ericvsmith commented May 25, 2022

That's the same as:

from unittest.mock import create_autospec

class DummyClass:
    pass

dummy_class = create_autospec(DummyClass)
dummy_class.a

Which gives the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests type-bug
Projects
None yet
Development

No branches or pull requests

4 participants