8000 Flask app Protocol · Issue #2789 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content
Flask app Protocol #2789
Closed
Closed
@chrisphilip322

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?

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

      0