mindflayer / python-mocket

a socket mock framework - for all kinds of socket animals, web-clients included
BSD 3-Clause "New" or "Revised" License
280 stars 42 forks source link

Issue with decorator and patches used together #148

Closed WisdomPill closed 3 years ago

WisdomPill commented 3 years ago

Describe the bug In my project when I upgraded mocket I noticed an issue on all the tests that had @mocketize decorator and @patches (from unittest.mock package). I tried different versions and it seems to me that from 3.9.4 there is the issue. I am using django's TestCase I am sure the issue is showing up regardless of the content of the test and without any setUp or tearDown methods. I also tried with python's plain unittest.TestCase with the following code and the issue still happens.

class TestRandom(TestCase):
    @mocketize
    @patch("path.to.function")
    def test_random(
        self,
        method_patch,
    ):
        pass

The error is the following, it seems to me that the function does not get the patches as arguments.

Error
Traceback (most recent call last):
  File "/Users/anas/Projects/project_name/.venv/lib/python3.7/site-packages/decorator.py", line 231, in fun
    args, kw = fix(args, kw, sig)
  File "/Users/anas/Projects/project_name/.venv/lib/python3.7/site-packages/decorator.py", line 203, in fix
    ba = sig.bind(*args, **kwargs)
  File "/Users/anas/.pyenv/versions/3.7.9/lib/python3.7/inspect.py", line 3015, in bind
    return args[0]._bind(args[1:], kwargs)
  File "/Users/anas/.pyenv/versions/3.7.9/lib/python3.7/inspect.py", line 2930, in _bind
    raise TypeError(msg) from None
TypeError: missing a required argument: 'method_patch'

To Reproduce This is complicated, I am sure the only change that makes the tests fail is to upgrade mocket after 3.9.4 included, with 3.9.3 it works fine. I am trying to setup a dummy repo but the issue does not show up.

Expected behavior The patches should be passed as arguments as usual.

I tested only with python 3.7 regardless of the environment, on docker with python3.7-alpine and on my mac using pyenv.

Additional context I am sorry I did not provide further details but I can't share the code which is having the issue, I will try to create a dummy project with the same dependencies as mine and work my way down removing dependencies. It will take me some time but in the meantime I am opening the issue hoping that maybe you can spot the problem before that.

For the moment my workaround is to use Mocketizer as context manager

mindflayer commented 3 years ago

Hi @WisdomPill, looking at what changed between 3.9.3 and 3.9.4 I'd say it's probably something related to decorator version pinning. I need to test and probably pin the correct version, in case it's related to that. If you'd like to spend more time on it, I'd suggest to test different versions of decorator and figure out which one fails. Thanks for opening this issue.

mindflayer commented 3 years ago

Sorry for the delay. I've just opened a bug here.

The problem is related to using decorator>=5.

mindflayer commented 3 years ago

All good, you can pip install mocket==3.9.41 which excludes decorator>=5.

https://pypi.org/project/mocket/3.9.41/

WisdomPill commented 3 years ago

Thanks!