<p dir="auto">Congrats on the release of Flask 2.2.0<g-emoji class="g-emoji" alias="tada" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png">🎉</g-emoji></p>
<p dir="auto">I noticed some weird behavior.</p>
<h2 dir="auto">Environment:</h2>
<ul dir="auto">
<li>Python version: 3.8.6</li>
<li>Flask version: 2.2.0</li>
<li>nose 1.3.7</li>
</ul>
<h2 dir="auto">Procedure:</h2>
<ol dir="auto">
<li>Create the following file as <code class="notranslate">example.py</code></li>
</ol>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from flask import request


def awesome_function():
    return &quot;spam&quot;"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-s1">request</span>


<span class="pl-k">def</span> <span class="pl-en">awesome_function</span>():
    <span class="pl-k">return</span> <span class="pl-s">"spam"</span></pre></div>
<ol start="2" dir="auto">
<li>Run <code class="notranslate">nosetests</code> with doctests</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ nosetests --with-doctest -v"><pre class="notranslate"><code class="notranslate">$ nosetests --with-doctest -v
</code></pre></div>
<h2 dir="auto">AS IS: <code class="notranslate">nosetests</code> fail</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Failure: RuntimeError (Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.) ... ERROR

======================================================================
ERROR: Failure: RuntimeError (Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.)
----------------------------------------------------------------------
Traceback (most recent call last):
  File &quot;/Users/.../venv/lib/python3.8/site-packages/nose/failure.py&quot;, line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File &quot;/Users/.../venv/lib/python3.8/site-packages/nose/plugins/manager.py&quot;, line 154, in generate
    for r in result:
  File &quot;/Users/.../venv/lib/python3.8/site-packages/nose/plugins/doctests.py&quot;, line 228, in loadTestsFromModule
    tests = self.finder.find(module)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/doctest.py&quot;, line 939, in find
    self._find(tests, obj, name, module, source_lines, globs, {})
  File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/doctest.py&quot;, line 998, in _find
    if ((inspect.isroutine(inspect.unwrap(val))
  File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py&quot;, line 520, in unwrap
    while _is_wrapper(func):
  File &quot;/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py&quot;, line 511, in _is_wrapper
    return hasattr(f, '__wrapped__')
  File &quot;/Users/.../venv/lib/python3.8/site-packages/werkzeug/local.py&quot;, line 316, in __get__
    obj = instance._get_current_object()  # type: ignore[misc]
  File &quot;/Users/.../venv/lib/python3.8/site-packages/werkzeug/local.py&quot;, line 509, in _get_current_object
    raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.

----------------------------------------------------------------------
Ran 1 test in 0.107s

FAILED (errors=1)"><pre class="notranslate"><code class="notranslate">Failure: RuntimeError (Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.) ... ERROR

======================================================================
ERROR: Failure: RuntimeError (Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/.../venv/lib/python3.8/site-packages/nose/failure.py", line 39, in runTest
    raise self.exc_val.with_traceback(self.tb)
  File "/Users/.../venv/lib/python3.8/site-packages/nose/plugins/manager.py", line 154, in generate
    for r in result:
  File "/Users/.../venv/lib/python3.8/site-packages/nose/plugins/doctests.py", line 228, in loadTestsFromModule
    tests = self.finder.find(module)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/doctest.py", line 939, in find
    self._find(tests, obj, name, module, source_lines, globs, {})
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/doctest.py", line 998, in _find
    if ((inspect.isroutine(inspect.unwrap(val))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 520, in unwrap
    while _is_wrapper(func):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 511, in _is_wrapper
    return hasattr(f, '__wrapped__')
  File "/Users/.../venv/lib/python3.8/site-packages/werkzeug/local.py", line 316, in __get__
    obj = instance._get_current_object()  # type: ignore[misc]
  File "/Users/.../venv/lib/python3.8/site-packages/werkzeug/local.py", line 509, in _get_current_object
    raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.

----------------------------------------------------------------------
Ran 1 test in 0.107s

FAILED (errors=1)
</code></pre></div>
<h2 dir="auto">TO BE: <code class="notranslate">nosetests</code> do NOT fail (0 tests run)</h2>
<ul dir="auto">
<li>In procedure 1, implementing some doctests do not change the result.</li>
</ul>
<h2 dir="auto">Workaround: <code class="notranslate">pip install -U 'flask&lt;2.2'</code> (installed flask 2.1.3)</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ nosetests --with-doctest -v

----------------------------------------------------------------------
Ran 0 tests in 0.096s

OK"><pre class="notranslate"><code class="notranslate">$ nosetests --with-doctest -v

----------------------------------------------------------------------
Ran 0 tests in 0.096s

OK
</code></pre></div>
<hr>
<p dir="auto">Flask 2.1.3 does not raise the error.<br>
<strong>Is this a bug embedded in the upgrade to 2.2.0?</strong></p>
<p dir="auto">I was wondering if it would be better to write it in the nose repository,<br>
but I think <code class="notranslate">from frask import request</code> is the cause, so I'll share it here.</p>
<p dir="auto">Migrating from Flask 2.1 to 2.2 failed a few unit tests of mine. It looks like we can no longer import <code class="notranslate">flask.request</code> top level in a test file (reproduced across Python 3.7, 3.8, 3.9, 3.10):</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from flask import Flask
from flask import request

def test_something():
    assert True"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-v">Flask</span>
<span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-s1">request</span>

<span class="pl-k">def</span> <span class="pl-en">test_something</span>():
    <span class="pl-k">assert</span> <span class="pl-c1">True</span></pre></div>
<p dir="auto">This gives a <code class="notranslate">RuntimeError</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="E   RuntimeError: Working outside of request context.
E
E   This typically means that you attempted to use functionality that needed
E   an active HTTP request. Consult the documentation on testing for
E   information about how to avoid this problem."><pre class="notranslate"><code class="notranslate">E   RuntimeError: Working outside of request context.
E
E   This typically means that you attempted to use functionality that needed
E   an active HTTP request. Consult the documentation on testing for
E   information about how to avoid this problem.
</code></pre></div>
<p dir="auto">On my side the fix was simple: importing <code class="notranslate">flask.request</code> outside of the global scope, just when I actually need it (my actual tests create small Flask apps to test a client).</p>
<p dir="auto">Edit: forgot to mention that this only occurs when pytest is run with <code class="notranslate">--doctest-modules</code></p>