I’m developing a Flask application to manage a school system, using Visual Studio Code with Pylance as my language server. I have defined multiple route functions in my routes.py
file, but I keep getting warnings from Pylance indicating that these functions are ‘not accessed’. I’m unsure why this is happening, and I’m looking for some guidance.
Even though I am importing the routes correctly and registering them with the app instance, the warnings persist. Here’s a snippet of my routes.py
:
# routes.py
from flask import Blueprint, render_template, redirect, url_for, request, session
from school_hub import db, User # Importing the database and User model
# Defining the Blueprint
main = Blueprint('main', __name__)
# Registering routes
@main.route('/login', methods=['GET', 'POST'])
def login():
# Implementation for login
if request.method == 'POST':
# Process login
pass
return render_template('login.html')
@main.route('/logout')
def logout():
# Implementation for logout
pass
@main.route('/')
def index():
return render_template('index.html')
In my app.py
, I import and register the routes as follows:
# app.py
from flask import Flask
from school_hub import create_app
from school_hub.routes import main
app = create_app()
app.register_blueprint(main) # Registering the Blueprint
if __name__ == '__main__':
app.run(debug=True)
Environment Details
- Python Version: 3.12
- Flask Version: 2.3
- Pylance Version: 2024.5.0
- Operating System: Windows 10
Additional Information
I’ve tried reloading the Pylance extension and refreshing the project, but the warnings persist. I’ve also looked through the Pylance settings for any configuration that could affect how it analyzes the code.
What Did You Try?
I have structured my Flask application with the following files:
app.py
: Where the app instance is created.__init__.py
: Initializes the app and SQLAlchemy.routes.py
: Contains route functions for the application.
Expected Outcome
I expected Pylance to recognize that these functions (login
, logout
, and index
) are being used as Flask routes and not show them as ‘not accessed’. I anticipated that once I registered the routes with the app instance, the warnings would disappear.
2
Answers
I don’t see there’s anything wrong with your code. It’s just a warning from Pylance that the functions in
routes.py
are not being directly referenced anywhere due to it being Flask’s route handler function. I think Pylance expect something along the lines ofroutes.logout()
or similar functions being called anywhere in your code for them to be considered "accessed"This is by design. To quote (code snippets faithfully adapted):
Alternatively, you can prefix the function names with underscores: