[New-bugs-announce] [issue45755] Mock spec with a specialized generic class does not mock class attributes

Kevin Jamieson report at bugs.python.org
Mon Nov 8 14:30:49 EST 2021


New submission from Kevin Jamieson <kjamieson at qumulo.com>:

This worked in Python 3.6, but in Python 3.7 and later creating a mock with a spec specifying a subscripted generic class does not mock any of the attributes of the class, because those attributes are not returned by dir().

For example:

# cat test.py
from typing import Generic, TypeVar
from unittest import mock

T = TypeVar('T')

class Foo(Generic[T]):
    def bar(self) -> None:
        pass

m = mock.MagicMock(spec=Foo[int])
m.bar()


# python3.11 test.py
Traceback (most recent call last):
  File "/root/test.py", line 11, in <module>
    m.bar()
    ^^^^^^^
  File "/usr/lib/python3.11/unittest/mock.py", line 635, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Mock object has no attribute 'bar'

----------
components: Library (Lib)
messages: 405981
nosy: kjamieson
priority: normal
severity: normal
status: open
title: Mock spec with a specialized generic class does not mock class attributes
type: behavior
versions: Python 3.10, Python 3.11, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45755>
_______________________________________


More information about the New-bugs-announce mailing list