Message405981
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' |
|
Date |
User |
Action |
Args |
2021-11-08 19:30:49 | kjamieson | set | recipients:
+ kjamieson |
2021-11-08 19:30:49 | kjamieson | set | messageid: <1636399849.8.0.841580145929.issue45755@roundup.psfhosted.org> |
2021-11-08 19:30:49 | kjamieson | link | issue45755 messages |
2021-11-08 19:30:49 | kjamieson | create | |
|