connexion icon indicating copy to clipboard operation
connexion copied to clipboard

Type inference for Flask app object wrong in VS Code

Open uniqueg opened this issue 2 years ago • 2 comments

Description

When creating a FlaskApp with connexion, the type information for the wrapped Flask app object is inferred wrong by VS Code with Pylance. While I do understand that type support has not been added to Connexion yet, it would be extremely helpful to at least be able if the editor would be able to infer the type of the wrapped app, so that one could make use of autosuggestion features and access config values without type checkers complaining.

I am not sure whether this is a problem with Connexion or with VS Code or Pylance, and whether it needs a fix or whether I am just doing something wrong (or there is, at least, a workaround).

Thanks for the great work on Connexion, btw, love it!

Expected behaviour

The type of the wrapped app should be inferred as flask.Flask.

Actual behaviour

The type of the wrapped app is inferred as None. See the following screenshot:

Screenshot from 2023-01-03 11-49-35

Screenshot from VS Code, type infernence presumably based on Pylance extension.

Note that the tooltip refers to the type of app.app, not app (whose type is correctly inferred as connexion.apps.FlaskApp).

Steps to reproduce

Install Connexion as per the instructions. Create a file with the following contents, based on the quickstart instructions.

import connexion

app = connexion.FlaskApp(__name__, specification_dir='openapi/')
app.app

Open in VS Code and hover over the second app in app.app.

Additional info:

Editor

Version: 1.74.2 Commit: e8a3071ea4344d9d48ef8a4df2c097372b0c5161 Date: 2022-12-20T10:27:19.253Z Electron: 19.1.8 Chromium: 102.0.5005.167 Node.js: 16.14.2 V8: 10.2.154.15-electron.0 OS: Linux x64 5.4.0-135-generic snap Sandboxed: No

Pylance

Name: Pylance Id: ms-python.vscode-pylance Description: A performant, feature-rich language server for Python in VS Code Version: 2022.12.20 Publisher: Microsoft VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance

Output of the commands:

  • python --version: Python 3.10.7 (tried other versions, from 3.7 to 3.10, same result)

  • pip show connexion | grep "^Version\:" Version: 2.14.1 (tried other versions, same result)

uniqueg avatar Jan 03 '23 12:01 uniqueg

Indeed, currently, typing is not properly supported in Connexion.

I believe we can fix this particular issue (as a small bandaid) by adding a typehint in the FlaskApp class for self.app in __init__ (in the form of self.app : flask.Flask):

image

In the meantime, I believe the following workaround should help you:

my_flask_app: flask.Flask = app.app

Of course, this is just for this one case. If there are other instances, these will not be solved :)

If this is the only case you'd like to get fixed, let me know and I'll add it (or open a PR to the v2 branch). As v3 is underway, we will not be working ourselves on any big changes on v2, but rather incorporate this in v3 directly.

Ruwann avatar Jan 19 '23 21:01 Ruwann

Thanks for the great response, @Ruwann. For our main use case, adding the type hint you suggest would indeed solve the problem, so I would certainly appreciate it being merged.

As for the workaround you suggest: This is indeed what we are doing now, but we will need to do that every time we create an app or import it, so it's a bit tedious.

uniqueg avatar Jan 19 '23 22:01 uniqueg