-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Can no longer patch flask.g #84982
Comments
Whenever I try to patch flask.g, it appears to do nothing. This happened when I upgraded mock from 3.x to 4.x. I reported it on the mock github page testing-cabal/mock#490 and was asked to report it here. The folllowing code run with pytest works fine in mock 3.0.5, but fails to patch in 4.0.0 and up. from mock import patch
import flask
def some_function():
flask.g.somevariable = True
return flask.g.somevariable @patch('flask.g') |
Sorry, but I believe you were misdirected*. mock, as opposed to unittest.mock, pytest, flask, and werkzeug are 3rd party modules. The error report seems to be missing part of the stacktrace at both ends. What line is your file resulted in the error? (This time we can guess == @patch. @what called _lookup_app_object in flask? (I have no idea.) However, once contextlib._GeneratorContextManager calls next(self.gen), the rest of the trace is outside the stdlib. At the end, the RuntimeError is raised by flask, not by python, because the flask _app_ctx_stack.top does not exist. If you do not understand their error message, ask flask people.
|
The test was supposed to patch the flask component during the unit test, On Fri, May 29, 2020 at 11:02 PM Terry J. Reedy <[email protected]>
|
Terry, mock is now a rolling backport of unittest.mock with all development taking place in cpython's repo. If issues are reported there, they need to be triaged here first, as it's most likely a bug in unittest.mock that needs fixing here. Rob, please can you see if this is reproducible on Python 3.8 or 3.9, replacing |
Rob, you're welcome to dump |
I have confirmed that using unittest.mock instead of the 3rd party mock library in python 3.8.3 and 3.9-dev fails to patch flask.g. 3.7.7 works correctly. |
Chris, I see no evidence in the *incomplete* traceback not posted here that there is a bug in mock. Mock calls "hasattr(obj, '__func__') and apparently flask, called via werkzeug, answers 'RuntimeError'. The overt bug is that calling werkzeug.local.(type(obj)?).__getattr__ fails by doing something other than "return the (computed) attribute value or raise an AttributeError exception". |
I updated the test with 2 cases and the traceback is different for each when I expected them to be the same if this was purely a mock issue, the line throwing the error is the same. I can post this over with flask and see what they think. from unittest.mock import patch
import flask
def some_function():
flask.g.somevariable = True
return flask.g.somevariable @patch('flask.g') def test_no_patch():
assert some_function() $ pytest -vv temp_test.py
============================================================================================================ test session starts ============================================================================================================= temp_test.py::test_patch FAILED [ 50%] ================================================================================================================== FAILURES ================================================================================================================== args = (), keywargs = {}
@wraps(func)
def patched(*args, **keywargs):
> with self.decoration_helper(patched,
args,
keywargs) as (newargs, newkeywargs): ../../../.pyenv/versions/3.8.3/lib/python3.8/unittest/mock.py:1322: ../../../.pyenv/versions/3.8.3/lib/python3.8/contextlib.py:113: in __enter__ name = 'g'
def _lookup_app_object(name):
top = _app_ctx_stack.top
if top is None:
> raise RuntimeError(_app_ctx_err_msg)
E RuntimeError: Working outside of application context.
E
E This typically means that you attempted to use functionality that needed
E to interface with the current application object in some way. To solve
E this, set up an application context with app.app_context(). See the
E documentation for more information. pyvenv/lib/python3.8/site-packages/flask/globals.py:45: RuntimeError def test_no_patch():
> assert some_function() temp_test.py:14: temp_test.py:6: in some_function name = 'g'
def _lookup_app_object(name):
top = _app_ctx_stack.top
if top is None:
> raise RuntimeError(_app_ctx_err_msg)
E RuntimeError: Working outside of application context.
E
E This typically means that you attempted to use functionality that needed
E to interface with the current application object in some way. To solve
E this, set up an application context with app.app_context(). See the
E documentation for more information. pyvenv/lib/python3.8/site-packages/flask/globals.py:45: RuntimeError |
I've worked around the issue and accept that this will not work in the future. |
In order to patch flask.g e.g. as in python#84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
…new_callable is set In order to patch flask.g e.g. as in python#84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
…new_callable is set In order to patch flask.g e.g. as in python#84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
…new_callable is set In order to patch flask.g e.g. as in python#84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
…le is set (#119601) In order to patch flask.g e.g. as in #84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
…callable is set (pythonGH-119601) In order to patch flask.g e.g. as in pythonGH-84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe. (cherry picked from commit 422c4fc) Co-authored-by: Robert Collins <[email protected]>
…callable is set (pythonGH-119601) In order to patch flask.g e.g. as in pythonGH-84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe. (cherry picked from commit 422c4fc) Co-authored-by: Robert Collins <[email protected]>
…_callable is set (GH-119601) (#120335) gh-119600: mock: do not access attributes of original when new_callable is set (GH-119601) In order to patch flask.g e.g. as in GH-84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe. (cherry picked from commit 422c4fc) Co-authored-by: Robert Collins <[email protected]>
…_callable is set (GH-119601) (#120334) gh-119600: mock: do not access attributes of original when new_callable is set (GH-119601) In order to patch flask.g e.g. as in GH-84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe. (cherry picked from commit 422c4fc) Co-authored-by: Robert Collins <[email protected]>
…callable is set (python#119601) In order to patch flask.g e.g. as in python#84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
…callable is set (python#119601) In order to patch flask.g e.g. as in python#84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
…callable is set (python#119601) In order to patch flask.g e.g. as in python#84982, that proxies getattr must not be invoked. For that, mock must not try to read from the original object. In some cases that is unavoidable, e.g. when doing autospec. However, patch("flask.g", new_callable=MagicMock) should be entirely safe.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: