Add support for mypy --strict (#147)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
JAYATI SHRIVASTAVA 2020-08-16 20:09:14 +05:30 committed by GitHub
parent 4312ead23a
commit 3cc81b64c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 75 additions and 73 deletions

View file

@ -3,3 +3,4 @@ ignore_missing_imports = True
[mypy-typer.*]
disallow_untyped_defs = True
strict = True

View file

@ -1,40 +1,37 @@
"""Typer, build great CLIs. Easy to code. Based on Python type hints."""
__version__ = "0.3.1"
from click.exceptions import Abort as Abort
from click.exceptions import BadParameter as BadParameter
from click.exceptions import Exit as Exit
from click.termui import clear as clear
from click.termui import confirm as confirm
from click.termui import echo_via_pager as echo_via_pager
from click.termui import edit as edit
from click.termui import get_terminal_size as get_terminal_size
from click.termui import getchar as getchar
from click.termui import launch as launch
from click.termui import pause as pause
from click.termui import progressbar as progressbar
from click.termui import prompt as prompt
from click.termui import secho as secho
from click.termui import style as style
from click.termui import unstyle as unstyle
from click.utils import echo as echo
from click.utils import format_filename as format_filename
from click.utils import get_app_dir as get_app_dir
from click.utils import get_binary_stream as get_binary_stream
from click.utils import get_text_stream as get_text_stream
from click.utils import open_file as open_file
from click.exceptions import Abort, BadParameter, Exit
from click.termui import (
clear,
confirm,
echo_via_pager,
edit,
get_terminal_size,
getchar,
launch,
pause,
progressbar,
prompt,
secho,
style,
unstyle,
)
from click.utils import (
echo,
format_filename,
get_app_dir,
get_binary_stream,
get_text_stream,
open_file,
)
from . import colors
from .main import Typer, run
from .models import (
CallbackParam,
Context,
FileBinaryRead,
FileBinaryWrite,
FileText,
FileTextWrite,
)
from .params import Argument, Option
from . import colors as colors
from .main import Typer as Typer
from .main import run as run
from .models import CallbackParam as CallbackParam
from .models import Context as Context
from .models import FileBinaryRead as FileBinaryRead
from .models import FileBinaryWrite as FileBinaryWrite
from .models import FileText as FileText
from .models import FileTextWrite as FileTextWrite
from .params import Argument as Argument
from .params import Option as Option

View file

@ -13,13 +13,13 @@ class TyperArgument(click.core.Argument):
type: Optional[Any] = None,
required: Optional[bool] = None,
default: Optional[Any] = None,
callback: Optional[Callable] = None,
callback: Optional[Callable[..., Any]] = None,
nargs: Optional[int] = None,
metavar: Optional[str] = None,
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
autocompletion: Optional[Callable] = None,
autocompletion: Optional[Callable[..., Any]] = None,
# TyperArgument
show_default: Union[bool, str] = True,
show_choices: bool = True,

View file

@ -48,10 +48,10 @@ class Typer:
no_args_is_help: Optional[bool] = Default(None),
subcommand_metavar: Optional[str] = Default(None),
chain: bool = Default(False),
result_callback: Optional[Callable] = Default(None),
result_callback: Optional[Callable[..., Any]] = Default(None),
# Command
context_settings: Optional[Dict[Any, Any]] = Default(None),
callback: Optional[Callable] = Default(None),
callback: Optional[Callable[..., Any]] = Default(None),
help: Optional[str] = Default(None),
epilog: Optional[str] = Default(None),
short_help: Optional[str] = Default(None),
@ -93,7 +93,7 @@ class Typer:
no_args_is_help: Optional[bool] = Default(None),
subcommand_metavar: Optional[str] = Default(None),
chain: bool = Default(False),
result_callback: Optional[Callable] = Default(None),
result_callback: Optional[Callable[..., Any]] = Default(None),
# Command
context_settings: Optional[Dict[Any, Any]] = Default(None),
help: Optional[str] = Default(None),
@ -176,10 +176,10 @@ class Typer:
no_args_is_help: Optional[bool] = Default(None),
subcommand_metavar: Optional[str] = Default(None),
chain: bool = Default(False),
result_callback: Optional[Callable] = Default(None),
result_callback: Optional[Callable[..., Any]] = Default(None),
# Command
context_settings: Optional[Dict[Any, Any]] = Default(None),
callback: Optional[Callable] = Default(None),
callback: Optional[Callable[..., Any]] = Default(None),
help: Optional[str] = Default(None),
epilog: Optional[str] = Default(None),
short_help: Optional[str] = Default(None),
@ -450,7 +450,7 @@ def param_path_convertor(value: Optional[str] = None) -> Optional[Path]:
return None
def generate_enum_convertor(enum: Type[Enum]) -> Callable:
def generate_enum_convertor(enum: Type[Enum]) -> Callable[..., Any]:
lower_val_map = {str(val.value).lower(): val for val in enum}
def convertor(value: Any) -> Any:
@ -463,7 +463,7 @@ def generate_enum_convertor(enum: Type[Enum]) -> Callable:
return convertor
def generate_iter_convertor(convertor: Callable[[Any], Any]) -> Callable:
def generate_iter_convertor(convertor: Callable[[Any], Any]) -> Callable[..., Any]:
def internal_convertor(value: Any) -> List[Any]:
return [convertor(v) for v in value]
@ -472,11 +472,11 @@ def generate_iter_convertor(convertor: Callable[[Any], Any]) -> Callable:
def get_callback(
*,
callback: Optional[Callable] = None,
callback: Optional[Callable[..., Any]] = None,
params: Sequence[click.Parameter] = [],
convertors: Dict[str, Callable[[str], Any]] = {},
context_param_name: str = None,
) -> Optional[Callable]:
context_param_name: Optional[str] = None,
) -> Optional[Callable[..., Any]]:
if not callback:
return None
parameters = get_params_from_function(callback)
@ -629,7 +629,7 @@ def get_click_param(
if origin is Union:
types = []
for type_ in main_type.__args__:
if type_ is NoneType: # type: ignore
if type_ is NoneType:
continue
types.append(type_)
assert len(types) == 1, "Typer Currently doesn't support Union types"
@ -749,8 +749,10 @@ def get_click_param(
def get_param_callback(
*, callback: Optional[Callable] = None, convertor: Optional[Callable] = None
) -> Optional[Callable]:
*,
callback: Optional[Callable[..., Any]] = None,
convertor: Optional[Callable[..., Any]] = None,
) -> Optional[Callable[..., Any]]:
if not callback:
return None
parameters = get_params_from_function(callback)
@ -798,7 +800,9 @@ def get_param_callback(
return wrapper
def get_param_completion(callback: Optional[Callable] = None) -> Optional[Callable]:
def get_param_completion(
callback: Optional[Callable[..., Any]] = None
) -> Optional[Callable[..., Any]]:
if not callback:
return None
parameters = get_params_from_function(callback)
@ -849,7 +853,7 @@ def get_param_completion(callback: Optional[Callable] = None) -> Optional[Callab
return wrapper
def run(function: Callable) -> Any:
def run(function: Callable[..., Any]) -> Any:
app = Typer()
app.command()(function)
app()

View file

@ -87,7 +87,7 @@ class CommandInfo:
*,
cls: Optional[Type[click.Command]] = None,
context_settings: Optional[Dict[Any, Any]] = None,
callback: Optional[Callable] = None,
callback: Optional[Callable[..., Any]] = None,
help: Optional[str] = None,
epilog: Optional[str] = None,
short_help: Optional[str] = None,
@ -122,10 +122,10 @@ class TyperInfo:
no_args_is_help: Optional[bool] = Default(None),
subcommand_metavar: Optional[str] = Default(None),
chain: bool = Default(False),
result_callback: Optional[Callable] = Default(None),
result_callback: Optional[Callable[..., Any]] = Default(None),
# Command
context_settings: Optional[Dict[Any, Any]] = Default(None),
callback: Optional[Callable] = Default(None),
callback: Optional[Callable[..., Any]] = Default(None),
help: Optional[str] = Default(None),
epilog: Optional[str] = Default(None),
short_help: Optional[str] = Default(None),
@ -159,12 +159,12 @@ class ParameterInfo:
*,
default: Optional[Any] = None,
param_decls: Optional[Sequence[str]] = None,
callback: Optional[Callable] = None,
callback: Optional[Callable[..., Any]] = None,
metavar: Optional[str] = None,
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
autocompletion: Optional[Callable] = None,
autocompletion: Optional[Callable[..., Any]] = None,
# TyperArgument
show_default: Union[bool, str] = True,
show_choices: bool = True,
@ -180,7 +180,7 @@ class ParameterInfo:
# DateTime
formats: Optional[Union[List[str]]] = None,
# File
mode: str = None,
mode: Optional[str] = None,
encoding: Optional[str] = None,
errors: Optional[str] = "strict",
lazy: Optional[bool] = None,
@ -241,12 +241,12 @@ class OptionInfo(ParameterInfo):
# ParameterInfo
default: Optional[Any] = None,
param_decls: Optional[Sequence[str]] = None,
callback: Optional[Callable] = None,
callback: Optional[Callable[..., Any]] = None,
metavar: Optional[str] = None,
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
autocompletion: Optional[Callable] = None,
autocompletion: Optional[Callable[..., Any]] = None,
# Option
show_default: bool = True,
prompt: Union[bool, str] = False,
@ -269,7 +269,7 @@ class OptionInfo(ParameterInfo):
# DateTime
formats: Optional[Union[List[str]]] = None,
# File
mode: str = None,
mode: Optional[str] = None,
encoding: Optional[str] = None,
errors: Optional[str] = "strict",
lazy: Optional[bool] = None,
@ -339,12 +339,12 @@ class ArgumentInfo(ParameterInfo):
# ParameterInfo
default: Optional[Any] = None,
param_decls: Optional[Sequence[str]] = None,
callback: Optional[Callable] = None,
callback: Optional[Callable[..., Any]] = None,
metavar: Optional[str] = None,
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
autocompletion: Optional[Callable] = None,
autocompletion: Optional[Callable[..., Any]] = None,
# TyperArgument
show_default: Union[bool, str] = True,
show_choices: bool = True,
@ -360,7 +360,7 @@ class ArgumentInfo(ParameterInfo):
# DateTime
formats: Optional[Union[List[str]]] = None,
# File
mode: str = None,
mode: Optional[str] = None,
encoding: Optional[str] = None,
errors: Optional[str] = "strict",
lazy: Optional[bool] = None,

View file

@ -7,12 +7,12 @@ def Option(
# Parameter
default: Optional[Any],
*param_decls: str,
callback: Optional[Callable] = None,
callback: Optional[Callable[..., Any]] = None,
metavar: Optional[str] = None,
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
autocompletion: Optional[Callable] = None,
autocompletion: Optional[Callable[..., Any]] = None,
# Option
show_default: bool = True,
prompt: Union[bool, str] = False,
@ -35,7 +35,7 @@ def Option(
# DateTime
formats: Optional[Union[List[str]]] = None,
# File
mode: str = None,
mode: Optional[str] = None,
encoding: Optional[str] = None,
errors: Optional[str] = "strict",
lazy: Optional[bool] = None,
@ -103,12 +103,12 @@ def Argument(
# Parameter
default: Optional[Any],
*,
callback: Optional[Callable] = None,
callback: Optional[Callable[..., Any]] = None,
metavar: Optional[str] = None,
expose_value: bool = True,
is_eager: bool = False,
envvar: Optional[Union[str, List[str]]] = None,
autocompletion: Optional[Callable] = None,
autocompletion: Optional[Callable[..., Any]] = None,
# TyperArgument
show_default: Union[bool, str] = True,
show_choices: bool = True,
@ -124,7 +124,7 @@ def Argument(
# DateTime
formats: Optional[Union[List[str]]] = None,
# File
mode: str = None,
mode: Optional[str] = None,
encoding: Optional[str] = None,
errors: Optional[str] = "strict",
lazy: Optional[bool] = None,

View file

@ -1,10 +1,10 @@
import inspect
from typing import Callable, Dict, get_type_hints
from typing import Any, Callable, Dict, get_type_hints
from .models import ParamMeta
def get_params_from_function(func: Callable) -> Dict[str, ParamMeta]:
def get_params_from_function(func: Callable[..., Any]) -> Dict[str, ParamMeta]:
signature = inspect.signature(func)
type_hints = get_type_hints(func)
params = {}