Skip to content

Commit

Permalink
Run mypy in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Jan 25, 2024
1 parent 6ca696a commit 8c64241
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
58 changes: 28 additions & 30 deletions dtyper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ def more_stuff(big_calc):
from __future__ import annotations

import inspect
import typing as t
from dataclasses import field, make_dataclass
from functools import update_wrapper
from typing import TYPE_CHECKING

import typer
from typer import (
Expand Down Expand Up @@ -239,14 +239,14 @@ def more_stuff(big_calc):
utils,
)
from typer.core import TyperCommand
from typer.models import CommandFunctionType
from typing_extensions import ParamSpec

if TYPE_CHECKING:
from typing import Any, Callable, Dict, Optional, Type, TypeVar, Union
P = ParamSpec('P')
R = t.TypeVar('R')

from typing_extensions import ParamSpec

P = ParamSpec('P')
R = TypeVar('R')
Type = t.Type[t.Any]
Callable = t.Callable[..., t.Any]

__all__ = (
'Abort',
Expand Down Expand Up @@ -295,8 +295,8 @@ def more_stuff(big_calc):


def dataclass(
typer_command: Callable[P, R], **kwargs
) -> Callable[[Union[Type, Callable]], Type]:
typer_command: t.Callable[P, R], **kwargs: t.Any
) -> t.Callable[[t.Union[Type, Callable]], Type]:
"""Automatically construct a dataclass from a typer command.
One dataclass field is created for each parameter to the typer
Expand All @@ -308,7 +308,7 @@ def dataclass(
# is called twice on the same function.
typer_command = getattr(typer_command, '_dtyper_dec', typer_command)

def dataclass_maker(function_or_class: Union[Type, Callable]) -> Type:
def dataclass_maker(function_or_class: t.Union[Type, Callable]) -> Type:
assert callable(function_or_class)

ka = make_dataclass_args(typer_command, **kwargs)
Expand All @@ -327,8 +327,8 @@ def dataclass_maker(function_or_class: Union[Type, Callable]) -> Type:


def make_dataclass_args(
typer_command: Callable[P, R], **kwargs
) -> Dict[str, Any]:
typer_command: t.Callable[P, R], **kwargs: t.Any
) -> t.Dict[str, t.Any]:
"""Take a typer comamnd and return the arguments to be passed to
dataclasses.make_dataclass to construct a dataclass whose elements correspond
to the Arguments and Options to the typer command.
Expand All @@ -338,7 +338,7 @@ def make_dataclass_args(
# is called twice on the same function.
typer_command = getattr(typer_command, '_dtyper_dec', typer_command)

def param_to_field_desc(p):
def param_to_field_desc(p) -> t.Tuple[t.Any, ...]: # type: ignore[no-untyped-def]
if p.default is inspect.Parameter.empty:
return p.name, p.annotation
else:
Expand All @@ -347,17 +347,15 @@ def param_to_field_desc(p):
params = _fixed_signature(typer_command).parameters.values()
kwargs['fields'] = [param_to_field_desc(p) for p in params]
kwargs.setdefault('cls_name', typer_command.__name__)
kwargs.setdefault('namespace', {})['typer_command'] = staticmethod(
typer_command
)
kwargs.setdefault('namespace', {})['typer_command'] = staticmethod(typer_command)

return kwargs


update_wrapper(make_dataclass_args, make_dataclass)


def function(typer_command: Callable[P, R]) -> Callable[P, R]:
def function(typer_command: t.Callable[P, R]) -> t.Callable[P, R]:
"""
Decorate a typer.command to be called outside of a typer.Typer app context.
Expand Down Expand Up @@ -386,21 +384,21 @@ class Typer(typer.Typer):

def command(
self,
name: Optional[str] = None,
name: t.Optional[str] = None,
*,
cls: Optional[Type[TyperCommand]] = None,
context_settings: Optional[Dict[Any, Any]] = None,
help: Optional[str] = None,
epilog: Optional[str] = None,
short_help: Optional[str] = None,
cls: t.Optional[t.Type[TyperCommand]] = None,
context_settings: t.Optional[t.Dict[t.Any, t.Any]] = None,
help: t.Optional[str] = None,
epilog: t.Optional[str] = None,
short_help: t.Optional[str] = None,
options_metavar: str = '[OPTIONS]',
add_help_option: bool = True,
no_args_is_help: bool = False,
hidden: bool = False,
deprecated: bool = False,
# Rich settings
rich_help_panel: Union[str, None] = models.Default(None),
):
rich_help_panel: t.Union[str, None] = models.Default(None),
) -> t.Callable[[CommandFunctionType], CommandFunctionType]:
decorator = super().command(
name,
cls=cls,
Expand All @@ -416,11 +414,11 @@ def command(
rich_help_panel=rich_help_panel,
)

def wrapped(f):
def wrapped(f: CommandFunctionType) -> CommandFunctionType:
decorated = decorator(f)
func = function(decorated)
func._dtyper_dec = decorated
return func
func._dtyper_dec = decorated # type: ignore[attr-defined]
return t.cast(CommandFunctionType, func)

update_wrapper(wrapped, decorator)
return wrapped
Expand All @@ -429,15 +427,15 @@ def wrapped(f):
update_wrapper(Typer.command, typer.Typer.command)


def _fixed_signature(typer_command: Callable[P, R]) -> inspect.Signature:
def _fixed_signature(typer_command: t.Callable[P, R]) -> inspect.Signature:
"""
Return `inspect.Signature` with fixed default values for typer objects.
"""
sig = inspect.signature(typer_command)

def fix_param(p: inspect.Parameter) -> inspect.Parameter:
if isinstance(p.default, models.OptionInfo):
kind: Any = p.KEYWORD_ONLY
kind: t.Any = p.KEYWORD_ONLY
else:
kind = p.kind

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ line-length = 88

[tool.ruff.format]
quote-style = "single"

[tool.mypy]
strict = true
[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core"]
2 changes: 1 addition & 1 deletion test_dtyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_acommand():
assert ACommand('bukket')() == ('bukket-post', 'keys', None)
assert ACommand('bukket', 'kois', pid=3)() == ('bukket-post', 'kois', 3)

match = 'missing 1 required positional argument: \'bucket\''
match = "missing 1 required positional argument: 'bucket'"
with pytest.raises(TypeError, match=match):
ACommand()

Expand Down

0 comments on commit 8c64241

Please sign in to comment.