python-restx/flask-restx

Suggestion: fix for `abort()` pylance typing error

Opened this issue · 0 comments

Code

In flask_restx/errors.py I recommend we declare a typehint to the
code parameter in the abort function like so:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from typing import Union

import flask

from werkzeug.exceptions import HTTPException

from ._http import HTTPStatus

__all__ = (
    "abort",
    "RestError",
    "ValidationError",
    "SpecsError",
)


def abort(
    code: Union[int, HTTPStatus] = HTTPStatus.INTERNAL_SERVER_ERROR,
    message=None,
    **kwargs
):
...

Repro Steps (if applicable)

  1. Install flask_restx to your project
  2. import from flask_restx import abort
  3. If you have pylance enabled, you will be getting the following error:
  "Literal[500]" is incompatible with "HTTPStatus"

Expected Behavior

You should be able to pass in an error code integer, i.e. 500 or an HTTPStatus variable without issue.

Actual Behavior

Pylance will complain:

  "Literal[500]" is incompatible with "HTTPStatus"

Error Messages/Stack Trace

Argument of type "Literal[500]" cannot be assigned to parameter "code" of type "HTTPStatus" in function "abort"
  "Literal[500]" is incompatible with "HTTPStatus"

Environment

  • Python version: 3.8
  • Flask version: 1.1.1
  • Flask-RESTX version: 0.5.1
  • Other installed Flask extensions: None

Additional Context

I believe this fix would help the increasingly popular usage of type hints in python. If i can become a contributor, I can work on getting flask-restx to become more typehint friendly. Thanks!