Flask-restful: Type checking warnings (PyCharm)

Created on 7 Nov 2017  Â·  4Comments  Â·  Source: flask-restful/flask-restful

I'm using PyCharm to setup a basic project, and immediately there are 2 warnings from the type checker:

1) The Flask app instance is not correct, the Api expects a Blueprint?
image

2) A Resource subclass does not get recognized by the add_resource method.
image

I am not sure if this is because of some flask_restful things, or if I should report this to JetBrains.
Thanks.

Most helpful comment

@joshfriend thanks for the fix.
Please note that the ‘API’ constructor still expects ‘Blueprint’ and the type checker will complain, if you give ‘Flask’.

All 4 comments

Concerning the 2. issue (Resource not the expected type): This is due to the :type comment in the add_resource method of the Api class:

```lang=python
def add_resource(self, resource, urls, *kwargs):
"""Adds a resource to the api.

    :param resource: the class name of your resource
    :type resource: :class:`Resource`
[...]

```
Link to Source

This specifies to PyCharm that the resource parameter should be something that has the type Resource. However, this is not entirely right because a flask_restful.Resource

Represents an abstract RESTful resource.

So to actually pass it to the add_resource method it needs to be subclassed. The type hint specifies that something of the type Resource is expected and not something that is a subclass of Resource (classes are not subclasses of themselves). However a Resource subclass would be actually correct. Those two things are not the same, hence the error.

The fix for this to either remove the type hint or to change it to specify something like Type[Resource] (covariant, using syntax from the typing module, not the syntax that flask_restful uses!).

@joshfriend thanks for the fix.
Please note that the ‘API’ constructor still expects ‘Blueprint’ and the type checker will complain, if you give ‘Flask’.

@paulkernstock the Flask/Blueprint has been fixed in #695 - although afaik it's not yet in any release.

@voneiden Cool thing, thanks for the hint.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lra picture lra  Â·  4Comments

codebykyle picture codebykyle  Â·  3Comments

trnc-ck picture trnc-ck  Â·  3Comments

RealJTG picture RealJTG  Â·  5Comments

SubhabrataBanerji picture SubhabrataBanerji  Â·  5Comments