New submission from Kevin Jamieson <kjamie...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue45755>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to