<p dir="auto">Defining <code class="notranslate">flask.test_client()</code> instances using <code class="notranslate">with</code> clause causes them to share context/content of <code class="notranslate">flask.g</code> variable.<br>
This wasn't the case in previous versions of flask.</p>
<h3 dir="auto">How to replicate the bug</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import flask

app = flask.Flask(&quot;app&quot;)

@app.route(&quot;/index&quot;)
def index():

    if &quot;my_special_data&quot; in flask.g:
        print(&quot;special data found&quot;)
    else:
        print(&quot;no special data&quot;)

    flask.g.my_special_data = True

    return &quot;OK&quot;, 200

with app.test_client() as first_client, app.test_client() as second_client:

    first_client.get(&quot;/index&quot;)
    second_client.get(&quot;/index&quot;)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">flask</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-s1">flask</span>.<span class="pl-v">Flask</span>(<span class="pl-s">"app"</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">"/index"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">index</span>():

    <span class="pl-k">if</span> <span class="pl-s">"my_special_data"</span> <span class="pl-c1">in</span> <span class="pl-s1">flask</span>.<span class="pl-s1">g</span>:
        <span class="pl-en">print</span>(<span class="pl-s">"special data found"</span>)
    <span class="pl-k">else</span>:
        <span class="pl-en">print</span>(<span class="pl-s">"no special data"</span>)

    <span class="pl-s1">flask</span>.<span class="pl-s1">g</span>.<span class="pl-s1">my_special_data</span> <span class="pl-c1">=</span> <span class="pl-c1">True</span>

    <span class="pl-k">return</span> <span class="pl-s">"OK"</span>, <span class="pl-c1">200</span>

<span class="pl-k">with</span> <span class="pl-s1">app</span>.<span class="pl-en">test_client</span>() <span class="pl-k">as</span> <span class="pl-s1">first_client</span>, <span class="pl-s1">app</span>.<span class="pl-en">test_client</span>() <span class="pl-k">as</span> <span class="pl-s1">second_client</span>:

    <span class="pl-s1">first_client</span>.<span class="pl-en">get</span>(<span class="pl-s">"/index"</span>)
    <span class="pl-s1">second_client</span>.<span class="pl-en">get</span>(<span class="pl-s">"/index"</span>)</pre></div>
<p dir="auto">In flask==2.2.x running this script prints</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="no special data
special data found"><pre class="notranslate"><code class="notranslate">no special data
special data found
</code></pre></div>
<p dir="auto">showing that second_client shares <code class="notranslate">flask.g</code> with first client.</p>
<h3 dir="auto">Expected behaviour</h3>
<p dir="auto">In flask==2.1.3 this code prints</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="no special data
no special data"><pre class="notranslate"><code class="notranslate">no special data
no special data
</code></pre></div>
<p dir="auto">Additionally in both previous and current (2.2.x) versions of flask, running test clients defined outside of with clause, that is writing</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="first_client = app.test_client()
second_client = app.test_client()

first_client.get(&quot;/index&quot;)
second_client.get(&quot;/index&quot;)"><pre class="notranslate"><span class="pl-s1">first_client</span> <span class="pl-c1">=</span> <span class="pl-s1">app</span>.<span class="pl-en">test_client</span>()
<span class="pl-s1">second_client</span> <span class="pl-c1">=</span> <span class="pl-s1">app</span>.<span class="pl-en">test_client</span>()

<span class="pl-s1">first_client</span>.<span class="pl-en">get</span>(<span class="pl-s">"/index"</span>)
<span class="pl-s1">second_client</span>.<span class="pl-en">get</span>(<span class="pl-s">"/index"</span>)</pre></div>
<p dir="auto">produces</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="no special data
no special data"><pre class="notranslate"><code class="notranslate">no special data
no special data
</code></pre></div>

<p dir="auto">Environment:</p>
<ul dir="auto">
<li>Python version: 3.8.13</li>
<li>Flask version: 2.2.2</li>
</ul>
<p dir="auto">I believe this behavior should be considered a bug, and I noticed this behavior breaking some libraries based on flask.</p>
<p dir="auto">When loading my flask module <code class="notranslate">python -m project.modules.flaskhandler.py</code> everything works nicely.  Unfortunately, if I enable debug mode and <strong>use_reloader</strong>, the module is loaded, but when reloader restarts the application, it changes my sys.path.</p>
<p dir="auto">sys.path's first value changes from <strong>''</strong> to <strong>'project/modules/'</strong> which prevents absolute imports from other modules in my project.</p>
<p dir="auto">I was able to work around this problem with the following code at the top of my flaskhandler.py file:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import sys
import os

# Flask reloader hack
sys.path.append(os.getcwd())"><pre class="notranslate"><code class="notranslate">import sys
import os

# Flask reloader hack
sys.path.append(os.getcwd())
</code></pre></div>