Skip to content

Commit

Permalink
✨ Add support for mypy --strict (#147)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
  • Loading branch information
victorphoenix3 and tiangolo authored Aug 16, 2020
1 parent 4312ead commit 3cc81b6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 73 deletions.
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ ignore_missing_imports = True

[mypy-typer.*]
disallow_untyped_defs = True
strict = True
69 changes: 33 additions & 36 deletions typer/__init__.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def __init__(
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,
Expand Down
34 changes: 19 additions & 15 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def __init__(
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),
Expand Down Expand Up @@ -93,7 +93,7 @@ def callback(
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),
Expand Down Expand Up @@ -176,10 +176,10 @@ def add_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),
Expand Down Expand Up @@ -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:
Expand All @@ -463,7 +463,7 @@ def convertor(value: Any) -> Any:
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]

Expand All @@ -472,11 +472,11 @@ def internal_convertor(value: Any) -> List[Any]:

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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -798,7 +800,9 @@ def wrapper(ctx: click.Context, param: click.Parameter, value: Any) -> Any:
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)
Expand Down Expand Up @@ -849,7 +853,7 @@ def wrapper(ctx: click.Context, args: List[str], incomplete: Optional[str]) -> A
return wrapper


def run(function: Callable) -> Any:
def run(function: Callable[..., Any]) -> Any:
app = Typer()
app.command()(function)
app()
24 changes: 12 additions & 12 deletions typer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(
*,
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,
Expand Down Expand Up @@ -122,10 +122,10 @@ def __init__(
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),
Expand Down Expand Up @@ -159,12 +159,12 @@ def __init__(
*,
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,
Expand All @@ -180,7 +180,7 @@ def __init__(
# 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,
Expand Down Expand Up @@ -241,12 +241,12 @@ def __init__(
# 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,
Expand All @@ -269,7 +269,7 @@ def __init__(
# 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,
Expand Down Expand Up @@ -339,12 +339,12 @@ def __init__(
# 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,
Expand All @@ -360,7 +360,7 @@ def __init__(
# 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,
Expand Down
12 changes: 6 additions & 6 deletions typer/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions typer/utils.py
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down

1 comment on commit 3cc81b6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.