I have come across this problem when making my unit tests.
from unittest.mock import AsyncMock, patch
import pydantic
# class Parent(): # Using non-pydantic class works
class Parent(pydantic.BaseModel): # Using pydantic.BaseModel makes this test fail.
async def method(self):
raise NotImplementedError
class Child(Parent):
async def method(self):
# Overriding the parent method
return False
async def test_patching():
child_instance = Child()
with patch.object(child_instance, 'method', new_callable=AsyncMock) as mock_method:
mock_method.return_value = True
assert await child_instance.method() is True
This results in the error:
AttributeError: 'Child' object has no attribute 'method'
If I do not use the pydantic.BaseModel, I do not get this error.
What can I do to fix this?