10

I find that the autocomplete for Flask is somewhat lacking. This is because internally, context-specific objects such as current_app, request, and logger are actually LocalProxy objects. Thus PyCharm reasonably has no idea what to do with this type.

One solution would be to apply type hints to the imported modules. Except you can't do that! As of Python 3.7 there appears to be no such syntax to facilitate this.

So the next-obvious solution would be to make local copies of each context-specific module with the type explicitly set like so:

from logging import Logger
from flask import Flask, Request, Blueprint, request, current_app as app
    
app: Flask = app
logger: Logger = app.logger
request: Request = request

This works until you actually attempt to start the server, in which case the application crashes because of a RuntimeError: Working outside of application context.

It turns out that we can actually encapsulate the relevant type hints inside of a class or other scope inside of the application context.

@foo_blueprint.route('/foo', methods=['GET'])
def foo(cls):
    _app: Flask = app
    _logger: Logger = app.logger
    _request: Request = request
    # ...

This works but is incredibly awkward in every imaginable sense.

Is there a reasonable solution for getting proper type hints inside of an application context in Flask?

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
I'll Eat My Hat
  • 1,150
  • 11
  • 20
  • [maybe it will be useful](https://stackoverflow.com/questions/40550805/python-hint-for-variable) – Danila Ganchar Nov 09 '18 at 08:42
  • 1
    Flask is only supported in the professional edition of Pycharm https://www.jetbrains.com/help/pycharm/flask.html Which version of Pycharm are you using: Community Edition or Professional? – Eskapp Nov 09 '18 at 16:01
  • 1
    I use the Community Edition (was not aware of dedicated support of particular libraries), but this question was written in general for those awful libraries that smash together types at runtime (such as Boto3). – I'll Eat My Hat Nov 10 '18 at 18:13
  • cont'd: Flask as an example was merely the latest, most widely used, and has what is probably the simplest problems and workarounds. – I'll Eat My Hat Nov 10 '18 at 18:21
  • For those looking for an alternative to Flask, I suggest using [FastAPI](https://fastapi.tiangolo.com/), which is fully type annotated. – I'll Eat My Hat May 28 '20 at 17:56

2 Answers2

3

Update 2022

Starting in Flask version 2.0.0, type hints are now built-in to the library. (See pull request #3973)

Version of Flask prior to 2.0.0

For older version of Flask, one workaround is to use a type cast as follows:

from typing import cast

from flask import request
from flask import Request
from flask import Response


@app.route('/')
def index() -> Response:
    # Can't use `assert isinstance(request, Request)`, since `request` is `LocalProxy` object.
    global request
    request = cast(Request, request)

    token = request.headers.get('authorization')
    ... 
    return 'API is working', 200
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
0

Have you tried this yet? I know that it at least works when importing normal variables from other modules.

# in module_a.py
my_str = 23


# in module_b.py
from module_a import my_str

my_str: str

# The IDE should now suggest string methods when trying to access
# properties of `my_str`
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53