Skip to content

Flask app Protocol #2789

Closed
Closed

Description

@chrisphilip322

First off, big thanks to @pcorpet for adding Flask stubs in #2740. I was updating one of my packages with to new type information and noticed something that stuck out to me.

One of the patterns that my team and I like to use is to create a set of endpoints that are bound to a Flask duck-type; basically this factory can take a flask.Flask instance or a flask.Blueprint instance since both support add_url_rule.

def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: Callable[..., Any] = ..., **options: Any) -> None: ...

def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: Callable[..., Any] = ..., provide_automatic_options: Optional[bool] = ..., **options: Any) -> None: ...

I am not super familiar with typeshed practices or Flask internals but the solution I am using right now locally is to define FlaskBlueprintType = Union[flask.Flask, flask.Blueprint] and use that. A potentially more useful solution would be to define an ABC which Flask and Blueprint either explicitly implement or simply through https://docs.python.org/3/library/abc.html#abc.ABCMeta.register. Another option is to use https://www.python.org/dev/peps/pep-0544/ but Protocols are not fully finalized yet.

Does this make sense as type information to add or am I approaching this problem the wrong way?

Activity

  1. srittau commented on Feb 11, 2019

    @srittau
    Collaborator

    Protocols seem like a good fit for this solution, but a union is fine as well. I don't think there is anything in typeshed we could or should do about this, though (unless this is a common pattern).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions