microsoft / pyright

Static Type Checker for Python
Other
13.47k stars 1.48k forks source link

Pyright doesn't full analyise flasks "abort()" when inside a function #9483

Closed TimmBmn closed 2 days ago

TimmBmn commented 2 days ago

The bug

I am currently writing a Flask application and when i encounter an error i use this snippet so i instantly escape any code from within any function:

abort(make_response({"error": message}, 404))

I've now i wanted to move them to a separate file in wrapper functions so that i can have custom messages and status codes. Now i have encounter this bug where Flask doesn't realize that my wrapper function causes the code to stop executing which can result in it showing errors that are not there.

Minimal Setup:

from flask import Flask, abort

app = Flask(__name__)

def function_that_causes_404():
    abort(404)

@app.route("/pyrightbug")
def index():
    function_that_causes_404()
    return "Hello World" # it should say "Type analysis indicates code is unreachable" (see screenshot)

if __name__ == "__main__":
    app.run()

Whats happening:

Image

What should happen:

Image

How i stumbled upon this bug:

(because Pyright doesn't understand that errors.no_JSON_provided() calls abort() it keeps analyzing where it shouldn't Image

Pyright version and environment

I am running Pyright version 1.1.389 inside of Neovim (my config can be found under https://github.com//TimmBmn/nvim-config)

erictraut commented 2 days ago

If you want function_that_causes_404 to act like abort, you need to provide an explicit return type of NoReturn.

def function_that_causes_404() -> NoReturn:
    abort(404)