<p dir="auto">I override <code class="notranslate">Response.force_type</code> to handle my own custom responses (and some extra builtin types like lists). I recently tried to upgrade to Flask==1.1.x but it no longer calls my handler and throws an exception instead.</p>
<h3 dir="auto">Reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pytest
from flask import Flask, Response, jsonify


class MyCustomType:
    def to_json(self):
        return {'ok': 1}


class MyResponse(Response):
    @classmethod
    def force_type(cls, response, environ=None):
        if isinstance(response, list):
            return jsonify(response)
        if isinstance(response, MyCustomType):
            return jsonify(response.to_json())
        return super(MyResponse, cls).force_type(response, environ=environ)


class MyApp(Flask):
    response_class = MyResponse


my_app = MyApp(__name__)


@my_app.route('/list')
def get_list():
    return ['my_element']


@my_app.route('/my')
def get_my():
    return MyCustomType()


@pytest.fixture
def client():
    with my_app.test_client() as client:
        yield client


def test_force_type_list(client):
    assert client.get('/list').json == ['my_element']


def test_force_type_my(client):
    assert client.get('/my').json == {'ok': 1}
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pytest</span>
<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-v">Response</span>, <span class="pl-s1">jsonify</span>


<span class="pl-k">class</span> <span class="pl-v">MyCustomType</span>:
    <span class="pl-k">def</span> <span class="pl-en">to_json</span>(<span class="pl-s1">self</span>):
        <span class="pl-k">return</span> {<span class="pl-s">'ok'</span>: <span class="pl-c1">1</span>}


<span class="pl-k">class</span> <span class="pl-v">MyResponse</span>(<span class="pl-v">Response</span>):
    <span class="pl-en">@<span class="pl-s1">classmethod</span></span>
    <span class="pl-k">def</span> <span class="pl-en">force_type</span>(<span class="pl-s1">cls</span>, <span class="pl-s1">response</span>, <span class="pl-s1">environ</span><span class="pl-c1">=</span><span class="pl-c1">None</span>):
        <span class="pl-k">if</span> <span class="pl-en">isinstance</span>(<span class="pl-s1">response</span>, <span class="pl-s1">list</span>):
            <span class="pl-k">return</span> <span class="pl-en">jsonify</span>(<span class="pl-s1">response</span>)
        <span class="pl-k">if</span> <span class="pl-en">isinstance</span>(<span class="pl-s1">response</span>, <span class="pl-v">MyCustomType</span>):
            <span class="pl-k">return</span> <span class="pl-en">jsonify</span>(<span class="pl-s1">response</span>.<span class="pl-en">to_json</span>())
        <span class="pl-k">return</span> <span class="pl-en">super</span>(<span class="pl-v">MyResponse</span>, <span class="pl-s1">cls</span>).<span class="pl-en">force_type</span>(<span class="pl-s1">response</span>, <span class="pl-s1">environ</span><span class="pl-c1">=</span><span class="pl-s1">environ</span>)


<span class="pl-k">class</span> <span class="pl-v">MyApp</span>(<span class="pl-v">Flask</span>):
    <span class="pl-s1">response_class</span> <span class="pl-c1">=</span> <span class="pl-v">MyResponse</span>


<span class="pl-s1">my_app</span> <span class="pl-c1">=</span> <span class="pl-v">MyApp</span>(<span class="pl-s1">__name__</span>)


<span class="pl-en">@<span class="pl-s1">my_app</span>.<span class="pl-en">route</span>(<span class="pl-s">'/list'</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">get_list</span>():
    <span class="pl-k">return</span> [<span class="pl-s">'my_element'</span>]


<span class="pl-en">@<span class="pl-s1">my_app</span>.<span class="pl-en">route</span>(<span class="pl-s">'/my'</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">get_my</span>():
    <span class="pl-k">return</span> <span class="pl-v">MyCustomType</span>()


<span class="pl-en">@<span class="pl-s1">pytest</span>.<span class="pl-s1">fixture</span></span>
<span class="pl-k">def</span> <span class="pl-en">client</span>():
    <span class="pl-k">with</span> <span class="pl-s1">my_app</span>.<span class="pl-en">test_client</span>() <span class="pl-k">as</span> <span class="pl-s1">client</span>:
        <span class="pl-k">yield</span> <span class="pl-s1">client</span>


<span class="pl-k">def</span> <span class="pl-en">test_force_type_list</span>(<span class="pl-s1">client</span>):
    <span class="pl-k">assert</span> <span class="pl-s1">client</span>.<span class="pl-en">get</span>(<span class="pl-s">'/list'</span>).<span class="pl-s1">json</span> <span class="pl-c1">==</span> [<span class="pl-s">'my_element'</span>]


<span class="pl-k">def</span> <span class="pl-en">test_force_type_my</span>(<span class="pl-s1">client</span>):
    <span class="pl-k">assert</span> <span class="pl-s1">client</span>.<span class="pl-en">get</span>(<span class="pl-s">'/my'</span>).<span class="pl-s1">json</span> <span class="pl-c1">==</span> {<span class="pl-s">'ok'</span>: <span class="pl-c1">1</span>}</pre></div>
<h3 dir="auto">Exception</h3>
<div class="highlight highlight-text-python-traceback notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;/home/closeio/venv/lib/python3.6/site-packages/flask/app.py&quot;, line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File &quot;/home/closeio/venv/lib/python3.6/site-packages/flask/app.py&quot;, line 1952, in full_dispatch_request
    return self.finalize_request(rv)
  File &quot;/home/closeio/venv/lib/python3.6/site-packages/flask/app.py&quot;, line 1967, in finalize_request
    response = self.make_response(rv)
  File &quot;/home/closeio/venv/lib/python3.6/site-packages/flask/app.py&quot;, line 2130, in make_response
    &quot; {rv.__class__.__name__}.&quot;.format(rv=rv)
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a MyCustomType."><pre class="notranslate">Traceback (most recent call last):
  File <span class="pl-s">"/home/closeio/venv/lib/python3.6/site-packages/flask/app.py"</span>, line <span class="pl-c1">2446</span>, in <span class="pl-en">wsgi_app</span>
    response <span class="pl-k">=</span> <span class="pl-c1">self</span>.full_dispatch_request()
  File <span class="pl-s">"/home/closeio/venv/lib/python3.6/site-packages/flask/app.py"</span>, line <span class="pl-c1">1952</span>, in <span class="pl-en">full_dispatch_request</span>
    <span class="pl-k">return</span> <span class="pl-c1">self</span>.finalize_request(rv)
  File <span class="pl-s">"/home/closeio/venv/lib/python3.6/site-packages/flask/app.py"</span>, line <span class="pl-c1">1967</span>, in <span class="pl-en">finalize_request</span>
    response <span class="pl-k">=</span> <span class="pl-c1">self</span>.make_response(rv)
  File <span class="pl-s">"/home/closeio/venv/lib/python3.6/site-packages/flask/app.py"</span>, line <span class="pl-c1">2130</span>, in <span class="pl-en">make_response</span>
    <span class="pl-s"><span class="pl-pds">"</span> <span class="pl-c1">{rv.__class__.__name__}</span>.<span class="pl-pds">"</span></span>.format(<span class="pl-v">rv</span><span class="pl-k">=</span>rv)
<span class="pl-en">TypeError</span>: <span class="pl-s">The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a MyCustomType.</span></pre></div>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Python version: 3.6.9</li>
<li>Flask version: 1.1.4 (it works on 1.0.4)</li>
<li>Werkzeug version: 0.15.6</li>
</ul>
<h3 dir="auto">PR that caused the issue</h3>
<p dir="auto"><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="450841307" data-permission-text="Title is private" data-url="https://github.com/pallets/flask/issues/3235" data-hovercard-type="pull_request" data-hovercard-url="/pallets/flask/pull/3235/hovercard" href="https://github.com/pallets/flask/pull/3235">#3235</a></p>
<p dir="auto">When using flask 2.0.2 and having an error in the import the flask cli does not output the traceback anymore.<br>
It seems to have stopped to work after <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1013550488" data-permission-text="Title is private" data-url="https://github.com/pallets/flask/issues/4283" data-hovercard-type="pull_request" data-hovercard-url="/pallets/flask/pull/4283/hovercard" href="https://github.com/pallets/flask/pull/4283">#4283</a> was merged.</p>
<p dir="auto">To reproduce: make an import error and run:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(.venv) PS D:\Workspace\private\notifications\python-api&gt; flask run --debugger --reload
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.

Error: While importing 'wsgi', an ImportError was raised."><pre class="notranslate"><code class="notranslate">(.venv) PS D:\Workspace\private\notifications\python-api&gt; flask run --debugger --reload
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.

Error: While importing 'wsgi', an ImportError was raised.
</code></pre></div>
<p dir="auto">Expected the following traceback:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; from gna import create_app
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;D:\Workspace\private\notifications\python-api\gna\__init__.py&quot;, line 4, in &lt;module&gt;
    from . import models, routes, services
  File &quot;D:\Workspace\private\notifications\python-api\gna\routes\__init__.py&quot;, line 4, in &lt;module&gt;
    from .api import api_bp
ImportError: cannot import name 'api_bp' from 'gna.routes.api' (D:\Workspace\private\notifications\python-api\gna\routes\api\__init__.py)
&gt;&gt;&gt;"><pre lang="(.venv)" class="notranslate"><code class="notranslate">Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from gna import create_app
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "D:\Workspace\private\notifications\python-api\gna\__init__.py", line 4, in &lt;module&gt;
    from . import models, routes, services
  File "D:\Workspace\private\notifications\python-api\gna\routes\__init__.py", line 4, in &lt;module&gt;
    from .api import api_bp
ImportError: cannot import name 'api_bp' from 'gna.routes.api' (D:\Workspace\private\notifications\python-api\gna\routes\api\__init__.py)
&gt;&gt;&gt;
</code></pre></div>
<p dir="auto">Environment:</p>
<ul dir="auto">
<li>Python version: 3.10.2</li>
<li>Flask version: 2.0.2</li>
</ul>