From 04e1e2a1c128b40c33461bdd9374adb6dd1eee34 Mon Sep 17 00:00:00 2001 From: Savo Kovacevic Date: Fri, 17 May 2019 16:31:31 +0200 Subject: [PATCH 1/5] Fix flask render_template and render_template_string stubs --- third_party/2and3/flask/templating.pyi | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/third_party/2and3/flask/templating.pyi b/third_party/2and3/flask/templating.pyi index 1da102d2bcd4..a8680794be3c 100644 --- a/third_party/2and3/flask/templating.pyi +++ b/third_party/2and3/flask/templating.pyi @@ -5,7 +5,7 @@ from .globals import _app_ctx_stack, _request_ctx_stack from .signals import before_render_template, template_rendered from jinja2 import BaseLoader, Environment as BaseEnvironment -from typing import Any +from typing import Any, Text, Iterable, Union class Environment(BaseEnvironment): app: Any = ... @@ -17,5 +17,8 @@ class DispatchingJinjaLoader(BaseLoader): def get_source(self, environment: Any, template: Any): ... def list_templates(self): ... -def render_template(template_name_or_list: Any, **context: Any): ... -def render_template_string(source: Any, **context: Any): ... +def render_template( + template_name_or_list: Union[str, Iterable[str]], + **context: Any +) -> Text: ... +def render_template_string(source: str, **context: Any) -> Text: ... From bc9fb87fe7985bbfdd5d4b241656a357727bdbe6 Mon Sep 17 00:00:00 2001 From: Savo Kovacevic Date: Mon, 20 May 2019 01:02:13 +0200 Subject: [PATCH 2/5] Add types for flask view function --- third_party/2and3/flask/app.pyi | 43 ++++++++++++++++++++++++-- third_party/2and3/flask/blueprints.pyi | 7 +++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/third_party/2and3/flask/app.pyi b/third_party/2and3/flask/app.pyi index 5be3d6d5931b..be8019917fd3 100644 --- a/third_party/2and3/flask/app.pyi +++ b/third_party/2and3/flask/app.pyi @@ -13,13 +13,50 @@ from .signals import appcontext_tearing_down, got_request_exception, request_fin from .templating import DispatchingJinjaLoader, Environment from .wrappers import Request, Response from .testing import FlaskClient -from typing import Any, Callable, ContextManager, Dict, List, Optional, Type, TypeVar, Union, Text +from typing import ( + Any, + Callable, + ContextManager, + Dict, + List, + Optional, + Type, + TypeVar, + Union, + Text, + Tuple, + TracebackType, + NoReturn, + Iterable, + ByteString +) from datetime import timedelta def setupmethod(f: Any): ... _T = TypeVar('_T') + +_ExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] +_StartResponse = Callable[[str, List[Tuple[str, str]], Optional[_ExcInfo]], Callable[[bytes], Any]] +_WSGICallable = Callable[[Dict[Text, Any], _StartResponse], Iterable[bytes]] + +_Status = Union[str, int] +_Headers = Union[Dict[Any, Any], List[Tuple[Any, Any]]] +_Body = Union[Text, ByteString, Response, _WSGICallable] + +_ViewFunc = Union[ + Callable[..., Text], + Callable[..., ByteString], + Callable[..., NoReturn], + Callable[..., Response], + Callable[..., _WSGICallable], + Callable[..., Tuple[_Body, _Status, _Headers]], + Callable[..., Tuple[_Body, _Status]], + Callable[..., Tuple[_Body, _Headers]] +] + + class Flask(_PackageBoundObject): request_class: type = ... response_class: type = ... @@ -96,8 +133,8 @@ class Flask(_PackageBoundObject): def make_null_session(self): ... def register_blueprint(self, blueprint: Blueprint, **options: Any) -> None: ... def iter_blueprints(self): ... - def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: Callable[..., Any] = ..., provide_automatic_options: Optional[bool] = ..., **options: Any) -> None: ... - def route(self, rule: str, **options: Any) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ... + def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: _ViewFunc = ..., provide_automatic_options: Optional[bool] = ..., **options: Any) -> None: ... + def route(self, rule: str, **options: Any) -> Callable[[_ViewFunc], _ViewFunc]: ... def endpoint(self, endpoint: str) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ... def errorhandler(self, code_or_exception: Union[int, Type[Exception]]) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ... def register_error_handler(self, code_or_exception: Union[int, Type[Exception]], f: Callable[..., Any]) -> None: ... diff --git a/third_party/2and3/flask/blueprints.pyi b/third_party/2and3/flask/blueprints.pyi index d43f8e4ff612..83a263315259 100644 --- a/third_party/2and3/flask/blueprints.pyi +++ b/third_party/2and3/flask/blueprints.pyi @@ -3,6 +3,7 @@ # NOTE: This dynamically typed stub was automatically generated by stubgen. from .helpers import _PackageBoundObject +from .app import _ViewFunc from typing import Any, Callable, Optional, Type, TypeVar, Union _T = TypeVar('_T') @@ -16,7 +17,7 @@ class BlueprintSetupState: url_prefix: Any = ... url_defaults: Any = ... def __init__(self, blueprint: Any, app: Any, options: Any, first_registration: Any) -> None: ... - 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: _ViewFunc = ..., **options: Any) -> None: ... class Blueprint(_PackageBoundObject): warn_on_modifications: bool = ... @@ -37,8 +38,8 @@ class Blueprint(_PackageBoundObject): def record_once(self, func: Any): ... def make_setup_state(self, app: Any, options: Any, first_registration: bool = ...): ... def register(self, app: Any, options: Any, first_registration: bool = ...) -> None: ... - def route(self, rule: str, **options: Any) -> Callable[[Callable[..., Any]], Callable[..., Any]]: ... - def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: Callable[..., Any] = ..., **options: Any) -> None: ... + def route(self, rule: str, **options: Any) -> Callable[[_ViewFunc], _ViewFunc]: ... + def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: _ViewFunc = ..., **options: Any) -> None: ... def endpoint(self, endpoint: str) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ... def app_template_filter(self, name: Optional[Any] = ...): ... def add_app_template_filter(self, f: Any, name: Optional[Any] = ...) -> None: ... From a73d5a33d52b3ff0b32125c51fa900cd28f60ac8 Mon Sep 17 00:00:00 2001 From: Savo Kovacevic Date: Tue, 21 May 2019 22:14:07 +0200 Subject: [PATCH 3/5] Import TracebackType from the right location --- third_party/2and3/flask/app.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/2and3/flask/app.pyi b/third_party/2and3/flask/app.pyi index be8019917fd3..ff8b1545fa02 100644 --- a/third_party/2and3/flask/app.pyi +++ b/third_party/2and3/flask/app.pyi @@ -13,6 +13,7 @@ from .signals import appcontext_tearing_down, got_request_exception, request_fin from .templating import DispatchingJinjaLoader, Environment from .wrappers import Request, Response from .testing import FlaskClient +from types import TracebackType from typing import ( Any, Callable, @@ -25,7 +26,6 @@ from typing import ( Union, Text, Tuple, - TracebackType, NoReturn, Iterable, ByteString From 9c6d7c523e0c00efd9a80cc274b0990579b60f77 Mon Sep 17 00:00:00 2001 From: Savo Kovacevic Date: Sat, 25 May 2019 01:10:02 +0200 Subject: [PATCH 4/5] Switch to bound typevar in route decorator stub --- third_party/2and3/flask/app.pyi | 3 ++- third_party/2and3/flask/blueprints.pyi | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/third_party/2and3/flask/app.pyi b/third_party/2and3/flask/app.pyi index ff8b1545fa02..e18fded513d1 100644 --- a/third_party/2and3/flask/app.pyi +++ b/third_party/2and3/flask/app.pyi @@ -55,6 +55,7 @@ _ViewFunc = Union[ Callable[..., Tuple[_Body, _Status]], Callable[..., Tuple[_Body, _Headers]] ] +_VT = TypeVar('_VT', bound=_ViewFunc) class Flask(_PackageBoundObject): @@ -134,7 +135,7 @@ class Flask(_PackageBoundObject): def register_blueprint(self, blueprint: Blueprint, **options: Any) -> None: ... def iter_blueprints(self): ... def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: _ViewFunc = ..., provide_automatic_options: Optional[bool] = ..., **options: Any) -> None: ... - def route(self, rule: str, **options: Any) -> Callable[[_ViewFunc], _ViewFunc]: ... + def route(self, rule: str, **options: Any) -> Callable[[_VT], _VT]: ... def endpoint(self, endpoint: str) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ... def errorhandler(self, code_or_exception: Union[int, Type[Exception]]) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ... def register_error_handler(self, code_or_exception: Union[int, Type[Exception]], f: Callable[..., Any]) -> None: ... diff --git a/third_party/2and3/flask/blueprints.pyi b/third_party/2and3/flask/blueprints.pyi index 83a263315259..d534ac8a14b3 100644 --- a/third_party/2and3/flask/blueprints.pyi +++ b/third_party/2and3/flask/blueprints.pyi @@ -7,6 +7,7 @@ from .app import _ViewFunc from typing import Any, Callable, Optional, Type, TypeVar, Union _T = TypeVar('_T') +_VT = TypeVar('_VT', bound=_ViewFunc) class BlueprintSetupState: app: Any = ... @@ -38,7 +39,7 @@ class Blueprint(_PackageBoundObject): def record_once(self, func: Any): ... def make_setup_state(self, app: Any, options: Any, first_registration: bool = ...): ... def register(self, app: Any, options: Any, first_registration: bool = ...) -> None: ... - def route(self, rule: str, **options: Any) -> Callable[[_ViewFunc], _ViewFunc]: ... + def route(self, rule: str, **options: Any) -> Callable[[_VT], _VT]: ... def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: _ViewFunc = ..., **options: Any) -> None: ... def endpoint(self, endpoint: str) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ... def app_template_filter(self, name: Optional[Any] = ...): ... From ab780c991048d3bfc7d5e63493e0bfc5bb07a15e Mon Sep 17 00:00:00 2001 From: Savo Kovacevic Date: Sat, 25 May 2019 01:23:12 +0200 Subject: [PATCH 5/5] Change render_template and render_template_string parameters to Text --- third_party/2and3/flask/templating.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/third_party/2and3/flask/templating.pyi b/third_party/2and3/flask/templating.pyi index a8680794be3c..87d77152740b 100644 --- a/third_party/2and3/flask/templating.pyi +++ b/third_party/2and3/flask/templating.pyi @@ -18,7 +18,7 @@ class DispatchingJinjaLoader(BaseLoader): def list_templates(self): ... def render_template( - template_name_or_list: Union[str, Iterable[str]], + template_name_or_list: Union[Text, Iterable[Text]], **context: Any ) -> Text: ... -def render_template_string(source: str, **context: Any) -> Text: ... +def render_template_string(source: Text, **context: Any) -> Text: ...