This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kjamieson
Recipients kjamieson
Date 2021-11-08.19:30:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1636399849.8.0.841580145929.issue45755@roundup.psfhosted.org>
In-reply-to
Content
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'
History
Date User Action Args
2021-11-08 19:30:49kjamiesonsetrecipients: + kjamieson
2021-11-08 19:30:49kjamiesonsetmessageid: <1636399849.8.0.841580145929.issue45755@roundup.psfhosted.org>
2021-11-08 19:30:49kjamiesonlinkissue45755 messages
2021-11-08 19:30:49kjamiesoncreate