Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conflict with asynctest project #124

Closed
hyzhak opened this issue Jul 20, 2016 · 5 comments
Closed

conflict with asynctest project #124

hyzhak opened this issue Jul 20, 2016 · 5 comments

Comments

@hyzhak
Copy link

hyzhak commented Jul 20, 2016

Issue Type

It is Bug Report about conflict Sure lib and asynctest (https://github.com/Martiusweb/asynctest).

Versions & Configuration

Using:
sure=1.3.0
asynctest==0.7.1
Docker Python 3.5.1 (https://hub.docker.com/_/python/)

Steps to reproduce

I'm trying to use sure and asynctest in one test:

import asynctest
# I don't ever use it but import actually is enough
import sure

class Test_Story(asynctest.TestCase):
    @asynctest.mock.patch('story.async_function')
    async def test_async_story(self, mock):
        pass

and it fails with error:

   Traceback (most recent call last):
    /usr/local/lib/python3.5/site-packages/asynctest/case.py line 261 in run
      self._run_test_method(testMethod)
    /usr/local/lib/python3.5/site-packages/asynctest/case.py line 313 in _run_test_method
      self.loop.run_until_complete(result)
    /usr/local/lib/python3.5/site-packages/asynctest/case.py line 196 in wrapper
      return method(*args, **kwargs)
    /usr/local/lib/python3.5/asyncio/base_events.py line 337 in run_until_complete
      return future.result()
    /usr/local/lib/python3.5/asyncio/futures.py line 274 in result
      raise self._exception
    /usr/local/lib/python3.5/asyncio/tasks.py line 239 in _step
      result = coro.send(None)

    /usr/local/lib/python3.5/site-packages/asynctest/mock.py line 429 in patched_generator
      return (yield from patched(*args, **kwargs))
    /usr/local/lib/python3.5/site-packages/asynctest/mock.py line 395 in patched
      arg = patching.__enter__()
    /usr/local/lib/python3.5/unittest/mock.py line 1271 in __enter__
      new = Klass(**_kwargs)
    /usr/local/lib/python3.5/site-packages/asynctest/mock.py line 274 in __init__
      super().__init__(*args, **kwargs)
    /usr/local/lib/python3.5/unittest/mock.py line 902 in __init__
      _spec_state, _new_name, _new_parent, **kwargs
    /usr/local/lib/python3.5/unittest/mock.py line 397 in __init__
      self._mock_add_spec(spec, spec_set, _spec_as_instance, _eat_self)
    /usr/local/lib/python3.5/site-packages/asynctest/mock.py line 78 in _mock_add_spec
      if asyncio.iscoroutinefunction(getattr(spec, attr)):
   AttributeError: 'NoneType' object has no attribute '_spec_coroutines'

and if I will remove import sure we won't get error.
After little bit investigation I've found that maybe problem that Sure mock somehow __dict__. And seems is related to this caveat https://github.com/gabrielfalcao/sure/blob/master/docs/source/api-reference.rst#holy-guacamole-how-did-you-implement-that-feature-

PS:
related issue in asynctest project Martiusweb/asynctest#18

@hyzhak
Copy link
Author

hyzhak commented Jul 23, 2016

I tried to debug this case and found that it seems that dir(None) grab local variable because here https://github.com/Martiusweb/asynctest/blob/master/asynctest/mock.py#L76 before _spec_coroutines = [] we have:

['args', 'kwargs', 'self', 'spec']

but after:

['_spec_coroutines', 'args', 'kwargs', 'self', 'spec']

what is completely different from what we have here without sure:

['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

@timofurrer
Copy link
Collaborator

Thanks for analyzing the issue!
I'm on vacation right now so I can't analyze or fix it at the moment - but will asap.
I'd be more than happy to review a PR though!

Thanks 🍻

@timofurrer
Copy link
Collaborator

@hyzhak I tried to reproduce your issue with the code you provided but it didn't work out because of the mocking where I'm missing the story module. Could you please provide a test without dependencies?

And please update to sure version 1.4.0. As you can read in the release notes I've fixed a major bug with the builtin monkey patching sure does.

Thanks 🍰

@timofurrer
Copy link
Collaborator

I have some time now to tackle this issue. I've contacted Martin (maintainer of asynctest) - maybe there is a chance we can look at this together.

In the meanwhile you'd be able to disable the monkey patching by setting the SURE_DISABLE_NEW_SYNTAX to 1. Like: SURE_DISABLE_NEW_SYNTAX=1.

timofurrer added a commit that referenced this issue Feb 9, 2017
Fix patched built-in dir() method. Closes #124
@timofurrer
Copy link
Collaborator

I've just fixed it with #128 .

Have fun and thanks for the report 🍻

@timofurrer timofurrer added this to the 1.4.1 milestone Feb 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants