skip to Main Content

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


  1. 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 of routes.logout() or similar functions being called anywhere in your code for them to be considered "accessed"

    Login or Signup to reply.
  2. This is by design. To quote (code snippets faithfully adapted):

    When you apply a decorator to a function, it is effectively writing the result of the decorator back to the symbol.

    In other words, your code is doing the equivalent of the following:

    def _index():
        return render_template('index.html')
    
    index = main.route('/')(_index)
    

    The symbol index is not accessed here, so pyright is correct to flag it as unused.

    If your intent is to simply register a function, you should do this:

    def index():
        return render_template('index.html')
    
    main.route('/')(index)
    

    Comment | @microsoft/pyright#7908

    Alternatively, you can prefix the function names with underscores:

    @main.route('/')
    def _index():
        return render_template('index.html')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search