Better type for click 'type' argument (#1903)

The call accepts tuples of types and callback methods
This commit is contained in:
David Euresti
2018-03-06 21:41:21 -08:00
committed by Jelle Zijlstra
parent d702d3621c
commit df65f6ea15
4 changed files with 18 additions and 16 deletions

View File

@@ -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

View File

@@ -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] = ...,

View File

@@ -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 = ...,

View File

@@ -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