Improving Flask stubs (#3003)

* Fix flask render_template and render_template_string stubs

* Add types for flask view function

* Import TracebackType from the right location

* Switch to bound typevar in route decorator stub

* Change render_template and render_template_string parameters to Text
This commit is contained in:
Savo Kovačević
2019-07-04 21:57:06 +02:00
committed by Jelle Zijlstra
parent 4af283e1ac
commit 84e6492d7e
3 changed files with 52 additions and 9 deletions

View File

@@ -13,13 +13,51 @@ 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 types import TracebackType
from typing import (
Any,
Callable,
ContextManager,
Dict,
List,
Optional,
Type,
TypeVar,
Union,
Text,
Tuple,
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]]
]
_VT = TypeVar('_VT', bound=_ViewFunc)
class Flask(_PackageBoundObject):
request_class: type = ...
response_class: type = ...
@@ -96,8 +134,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[[_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: ...

View File

@@ -3,9 +3,11 @@
# 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')
_VT = TypeVar('_VT', bound=_ViewFunc)
class BlueprintSetupState:
app: Any = ...
@@ -16,7 +18,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 +39,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[[_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] = ...): ...
def add_app_template_filter(self, f: Any, name: Optional[Any] = ...) -> None: ...

View File

@@ -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[Text, Iterable[Text]],
**context: Any
) -> Text: ...
def render_template_string(source: Text, **context: Any) -> Text: ...