This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Rob Taft
Recipients Rob Taft, cjw296, rkm, terry.reedy, xtreak
Date 2020-06-03.16:23:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1591201395.86.0.0618622718303.issue40805@roundup.psfhosted.org>
In-reply-to
Content
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_patch(mock_flask_global):
    assert some_function()

def test_no_patch():
    assert some_function()


$ pytest -vv temp_test.py 
============================================================================================================ test session starts =============================================================================================================
platform linux -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- 
collected 2 items                                                                                                                                                                                                                            

temp_test.py::test_patch FAILED                                                                                                                                                                                                        [ 50%]
temp_test.py::test_no_patch FAILED                                                                                                                                                                                                     [100%]

================================================================================================================== FAILURES ==================================================================================================================
_________________________________________________________________________________________________________________ test_patch _________________________________________________________________________________________________________________

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__
    return next(self.gen)
../../../.pyenv/versions/3.8.3/lib/python3.8/unittest/mock.py:1304: in decoration_helper
    arg = exit_stack.enter_context(patching)
../../../.pyenv/versions/3.8.3/lib/python3.8/contextlib.py:425: in enter_context
    result = _cm_type.__enter__(cm)
../../../.pyenv/versions/3.8.3/lib/python3.8/unittest/mock.py:1416: in __enter__
    if spec is None and _is_async_obj(original):
../../../.pyenv/versions/3.8.3/lib/python3.8/unittest/mock.py:51: in _is_async_obj
    if hasattr(obj, '__func__'):
pyvenv/lib/python3.8/site-packages/werkzeug/local.py:347: in __getattr__
    return getattr(self._get_current_object(), name)
pyvenv/lib/python3.8/site-packages/werkzeug/local.py:306: in _get_current_object
    return self.__local()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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
_______________________________________________________________________________________________________________ test_no_patch ________________________________________________________________________________________________________________

    def test_no_patch():
>       assert some_function()

temp_test.py:14: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
temp_test.py:6: in some_function
    flask.g.somevariable = True
pyvenv/lib/python3.8/site-packages/werkzeug/local.py:364: in <lambda>
    __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)
pyvenv/lib/python3.8/site-packages/werkzeug/local.py:306: in _get_current_object
    return self.__local()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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
History
Date User Action Args
2020-06-03 16:23:15Rob Taftsetrecipients: + Rob Taft, terry.reedy, cjw296, xtreak, rkm
2020-06-03 16:23:15Rob Taftsetmessageid: <1591201395.86.0.0618622718303.issue40805@roundup.psfhosted.org>
2020-06-03 16:23:15Rob Taftlinkissue40805 messages
2020-06-03 16:23:15Rob Taftcreate