From df65f6ea15c7fb3da528ad4445e2d5b620d04247 Mon Sep 17 00:00:00 2001 From: David Euresti Date: Tue, 6 Mar 2018 21:41:21 -0800 Subject: [PATCH] Better type for click 'type' argument (#1903) The call accepts tuples of types and callback methods --- third_party/2and3/click/core.pyi | 10 ++++------ third_party/2and3/click/decorators.pyi | 14 +++++++------- third_party/2and3/click/termui.pyi | 4 +++- third_party/2and3/click/types.pyi | 6 ++++-- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/third_party/2and3/click/core.pyi b/third_party/2and3/click/core.pyi index 417a98609..b8bb86530 100644 --- a/third_party/2and3/click/core.pyi +++ b/third_party/2and3/click/core.pyi @@ -17,6 +17,7 @@ from typing import ( from click.formatting import HelpFormatter from click.parser import OptionParser +from click.types import ParamType, _ConvertibleType def invoke_param_callback( @@ -317,7 +318,7 @@ class Parameter: name: str opts: List[str] secondary_opts: List[str] - type: 'ParamType' + type: ParamType required: bool callback: Optional[Callable[[Context, 'Parameter', str], Any]] nargs: int @@ -333,7 +334,7 @@ class Parameter: def __init__( self, param_decls: Optional[List[str]] = ..., - type: Optional[Union[type, 'ParamType']] = ..., + type: Optional[_ConvertibleType] = ..., required: bool = ..., default: Optional[Any] = ..., callback: Optional[Callable[[Context, 'Parameter', str], Any]] = ..., @@ -412,7 +413,7 @@ class Option(Parameter): multiple: bool = ..., count: bool = ..., allow_from_autoenv: bool = ..., - type: Optional[Union[type, 'ParamType']] = ..., + type: Optional[_ConvertibleType] = ..., help: Optional[str] = ..., **attrs ) -> None: @@ -430,6 +431,3 @@ class Argument(Parameter): **attrs ) -> None: ... - -# cyclic dependency -from click.types import ParamType # noqa: E402 diff --git a/third_party/2and3/click/decorators.pyi b/third_party/2and3/click/decorators.pyi index c587a7f6d..46f19181c 100644 --- a/third_party/2and3/click/decorators.pyi +++ b/third_party/2and3/click/decorators.pyi @@ -2,7 +2,7 @@ from distutils.version import Version from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union, Text from click.core import Command, Group, Argument, Option, Parameter, Context -from click.types import ParamType +from click.types import _ConvertibleType _T = TypeVar('_T') _Decorator = Callable[[_T], _T] @@ -74,7 +74,7 @@ def argument( # Argument required: Optional[bool] = ..., # Parameter - type: Optional[Union[type, ParamType]] = ..., + type: Optional[_ConvertibleType] = ..., default: Optional[Any] = ..., callback: Optional[_Callback] = ..., nargs: Optional[int] = ..., @@ -99,7 +99,7 @@ def option( multiple: bool = ..., count: bool = ..., allow_from_autoenv: bool = ..., - type: Optional[Union[type, ParamType]] = ..., + type: Optional[_ConvertibleType] = ..., help: Optional[str] = ..., # Parameter default: Optional[Any] = ..., @@ -127,7 +127,7 @@ def confirmation_option( multiple: bool = ..., count: bool = ..., allow_from_autoenv: bool = ..., - type: Optional[Union[type, ParamType]] = ..., + type: Optional[_ConvertibleType] = ..., help: str = ..., # Parameter default: Optional[Any] = ..., @@ -154,7 +154,7 @@ def password_option( multiple: bool = ..., count: bool = ..., allow_from_autoenv: bool = ..., - type: Optional[Union[type, ParamType]] = ..., + type: Optional[_ConvertibleType] = ..., help: Optional[str] = ..., # Parameter default: Optional[Any] = ..., @@ -184,7 +184,7 @@ def version_option( multiple: bool = ..., count: bool = ..., allow_from_autoenv: bool = ..., - type: Optional[Union[type, ParamType]] = ..., + type: Optional[_ConvertibleType] = ..., help: str = ..., # Parameter default: Optional[Any] = ..., @@ -211,7 +211,7 @@ def help_option( multiple: bool = ..., count: bool = ..., allow_from_autoenv: bool = ..., - type: Optional[Union[type, ParamType]] = ..., + type: Optional[_ConvertibleType] = ..., help: str = ..., # Parameter default: Optional[Any] = ..., diff --git a/third_party/2and3/click/termui.pyi b/third_party/2and3/click/termui.pyi index c99a68580..eafa09da2 100644 --- a/third_party/2and3/click/termui.pyi +++ b/third_party/2and3/click/termui.pyi @@ -11,6 +11,8 @@ from typing import ( TypeVar, ) +from click.types import _ConvertibleType + def hidden_prompt_func(prompt: str) -> str: ... @@ -30,7 +32,7 @@ def prompt( default: Optional[str] = ..., hide_input: bool = ..., confirmation_prompt: bool = ..., - type: Optional[Any] = ..., + type: Optional[_ConvertibleType] = ..., value_proc: Optional[Callable[[Optional[str]], Any]] = ..., prompt_suffix: str = ..., show_default: bool = ..., diff --git a/third_party/2and3/click/types.pyi b/third_party/2and3/click/types.pyi index 525e2f33e..80642d8f6 100644 --- a/third_party/2and3/click/types.pyi +++ b/third_party/2and3/click/types.pyi @@ -1,4 +1,4 @@ -from typing import Any, Callable, IO, Iterable, List, Optional, TypeVar, Union +from typing import Any, Callable, IO, Iterable, List, Optional, TypeVar, Union, Tuple as _PyTuple import uuid from click.core import Context, Parameter @@ -270,7 +270,9 @@ class UUIDParameterType(ParamType): ... -def convert_type(ty: Any, default: Optional[Any] = ...) -> ParamType: +_ConvertibleType = Union[type, ParamType, _PyTuple[type, ...], Callable[[str], Any], Callable[[Optional[str]], Any]] + +def convert_type(ty: Optional[_ConvertibleType], default: Optional[Any] = ...) -> ParamType: ... # parameter type shortcuts