Repository navigation
Description
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: ... |
typeshed/third_party/2and3/flask/app.pyi
Line 98 in ca62cec
| 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
srittau commentedon Feb 11, 2019
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).