Comments (2)
This package is a rolling backport of unittest.mock
.
As such, any problems you encounter most likely need to be fixed upstream.
Please can you try and reproduce the problem on the latest release of Python 3, including alphas, and replace any import from mock
with ones from unittest.mock
.
If the issue still occurs, then please report upstream through https://github.com/python/cpython/issues as it will need to be fixed there so that it can be backported here and released to you.
If not, reply with what you find out and we can re-open this issue.
from mock.
I can confirm that the issue exists in master branch of CPython. The issue is that using spec
only checks for invalid attribute access and not signatures. So MagicMock(AClass)
uses AClass
as spec and checks for only attribute access on the mock allowing invalid attributes but assert_
helpers check for signature since Python 3.4. There was a proposal to make spec also do signature checking but it will break backwards compatibility due to strict checks and auto spec is also expensive since it would also apply it for methods. I would suggest using create_autospec
since that will check for signature too catching these issues earlier.
from unittest.mock import MagicMock, create_autospec
class AClass:
def __init__(self, a, b=2):
pass
def test_mock(kwargs):
mock_a_class = create_autospec(AClass)
mock_a_class(**kwargs)
mock_a_class.assert_called_once_with(**kwargs)
test_mock({})
test_mock({'c': 1, 'a': 2})
python /tmp/foo.py
Traceback (most recent call last):
File "/tmp/foo.py", line 14, in <module>
test_mock({})
File "/tmp/foo.py", line 11, in test_mock
mock_a_class(**kwargs)
File "/usr/lib/python3.10/unittest/mock.py", line 1102, in __call__
self._mock_check_sig(*args, **kwargs)
File "/usr/lib/python3.10/unittest/mock.py", line 123, in checksig
sig.bind(*args, **kwargs)
File "/usr/lib/python3.10/inspect.py", line 3179, in bind
return self._bind(args, kwargs)
File "/usr/lib/python3.10/inspect.py", line 3094, in _bind
raise TypeError(msg) from None
TypeError: missing a required argument: 'a'
from mock.
Related Issues (20)
- should we mark as deprecated? HOT 3
- Add assert_not_called_with HOT 1
- Version 4.0 release HOT 3
- Syntax Error in 4.0.0 HOT 3
- Mock 4.0.0 "missing X positional argument" on instantiation of objects inherited from mock.MagicMock HOT 7
- Please add contribution guidelines HOT 1
- Should the backports follow upstream releases? instead of alpha/beta/rcs. HOT 3
- Remove universal wheel tag and hide 4.0.0b1 and 4.0.0 releases to stop tools finding them. HOT 14
- egg_info for package mock produced metadata for project name unknown HOT 5
- 4.0.x: 'ValueError: Sentinels must not start with _' and others HOT 7
- Can we get mock supported in python 3.8 HOT 2
- Can no longer patch flask.g HOT 9
- spec_set/autospec/spec seems to not be reading attributes defined in class body HOT 2
- mock throws ValueError when spec_arg is an ndarray HOT 4
- Question on release 4.0.3 HOT 2
- Test test_bool_not_called_when_passing_spec_arg() fails (mixup unittest.mock vs mock?) HOT 4
- 4.0.3: pytest is failing HOT 5
- AsyncMock stubs for mypy in Python 3.7 HOT 5
- Error when inspecting the signature of AsyncMock() HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mock.