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

Error with objectproxy globals and current versions of mock #136

Closed
brondsem opened this issue May 16, 2023 · 0 comments · Fixed by #137
Closed

Error with objectproxy globals and current versions of mock #136

brondsem opened this issue May 16, 2023 · 0 comments · Fixed by #137

Comments

@brondsem
Copy link
Contributor

When using mock library version 4+, or unittest.mock on newer versions of Python, patching a TG global like tg.request etc will error out if you don't have any global context objects set up. This is unexpected since the goal of patching an object is so that it isn't used at all.

import unittest
import mock

class TestURLs(unittest.TestCase):
    @mock.patch('tg.request')
    def test_it(self, mock_req):
        print("TEST!")

causes this error:

Traceback (most recent call last):
  File "/tmp-venv/lib/python3.7/site-packages/mock/mock.py", line 1422, in patched
    keywargs) as (newargs, newkeywargs):
  File "/usr/local/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/tmp-venv/lib/python3.7/site-packages/mock/mock.py", line 1402, in decoration_helper
    arg = exit_stack.enter_context(patching)
  File "/usr/local/lib/python3.7/contextlib.py", line 427, in enter_context
    result = _cm_type.__enter__(cm)
  File "/tmp-venv/lib/python3.7/site-packages/mock/mock.py", line 1514, in __enter__
    if spec is None and _is_async_obj(original):
  File "/tmp-venv/lib/python3.7/site-packages/mock/mock.py", line 61, in _is_async_obj
    return iscoroutinefunction(obj) or inspect.isawaitable(obj)
  File "/tmp-venv/lib/python3.7/site-packages/mock/backports.py", line 34, in iscoroutinefunction
    getattr(obj, '_is_coroutine', None) is _is_coroutine
  File "/tmp-venv/lib/python3.7/site-packages/tg/support/objectproxy.py", line 26, in __getattr__
    return getattr(self._current_obj(), attr)
  File "/tmp-venv/lib/python3.7/site-packages/tg/request_local.py", line 235, in _current_obj
    return getattr(context, self.name)
  File "/tmp-venv/lib/python3.7/site-packages/tg/support/objectproxy.py", line 26, in __getattr__
    return getattr(self._current_obj(), attr)
  File "/tmp-venv/lib/python3.7/site-packages/tg/support/registry.py", line 72, in _current_obj
    'thread' % self.____name__)
TypeError: No object (name: context) has been registered for this thread

I'll submit a merge request with a potential fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant