Giter Club home page Giter Club logo

Comments (2)

cjw296 avatar cjw296 commented on September 4, 2024

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.

tirkarthi avatar tirkarthi commented on September 4, 2024

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'

python/cpython#74772

from mock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.