Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use functools.update_wrapper instead of wraps #12

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions .coveragerc

This file was deleted.

20 changes: 13 additions & 7 deletions dtyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def more_stuff(big_calc):

import inspect
from dataclasses import field, make_dataclass
from functools import wraps
from functools import update_wrapper
from typing import TYPE_CHECKING

import typer
Expand Down Expand Up @@ -294,7 +294,6 @@ def more_stuff(big_calc):
)


@wraps(make_dataclass)
def dataclass(
typer_command: Callable[P, R], **kwargs
) -> Callable[[Union[Type, Callable]], Type]:
Expand All @@ -309,7 +308,6 @@ def dataclass(
# is called twice on the same function.
typer_command = getattr(typer_command, '_dtyper_dec', typer_command)

@wraps(typer_command)
def dataclass_maker(function_or_class: Union[Type, Callable]) -> Type:
assert callable(function_or_class)

Expand All @@ -321,10 +319,13 @@ def dataclass_maker(function_or_class: Union[Type, Callable]) -> Type:

return make_dataclass(**ka)

update_wrapper(dataclass_maker, typer_command)
return dataclass_maker


@wraps(make_dataclass)
update_wrapper(dataclass, make_dataclass)


def make_dataclass_args(
typer_command: Callable[P, R], **kwargs
) -> Dict[str, Any]:
Expand Down Expand Up @@ -353,6 +354,9 @@ def param_to_field_desc(p):
return kwargs


update_wrapper(make_dataclass_args, make_dataclass)


def function(typer_command: Callable[P, R]) -> Callable[P, R]:
"""
Decorate a typer.command to be called outside of a typer.Typer app context.
Expand All @@ -363,12 +367,12 @@ def function(typer_command: Callable[P, R]) -> Callable[P, R]:
"""
sig = _fixed_signature(typer_command)

@wraps(typer_command)
def wrapped(*args: P.args, **kwargs: P.kwargs) -> R:
bound = sig.bind(*args, **kwargs)
bound.apply_defaults()
return typer_command(*bound.args, **bound.kwargs)

update_wrapper(wrapped, typer_command)
wrapped.__signature__ = sig # type: ignore
return wrapped

Expand All @@ -380,7 +384,6 @@ class Typer(typer.Typer):
above so they can be called from regular Python code.
"""

@wraps(typer.Typer.command)
def command(
self,
name: Optional[str] = None,
Expand Down Expand Up @@ -413,16 +416,19 @@ def command(
rich_help_panel=rich_help_panel,
)

@wraps(decorator)
def wrapped(f):
decorated = decorator(f)
func = function(decorated)
func._dtyper_dec = decorated
return func

update_wrapper(wrapped, decorator)
return wrapped


update_wrapper(Typer.command, typer.Typer.command)


def _fixed_signature(typer_command: Callable[P, R]) -> inspect.Signature:
"""
Return `inspect.Signature` with fixed default values for typer objects.
Expand Down
18 changes: 12 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ python = ">=3.8"
typer = "*"

[tool.poetry.group.dev.dependencies]
coverage = ">=7.1.0"
black = ">=23.9.1"
isort = ">=5.12.0"
mypy = ">=1.5.1"
pytest = ">=7.4.2"
ruff = ">=0.0.292"
coverage = "*"
mypy = "*"
pytest = "*"
ruff = "*"

[tool.coverage.run]
branch = true
source = ["dtyper"]

[tool.coverage.report]
fail_under = 94
skip_covered = true
exclude_lines = ["pragma: no cover", "if False:", "if __name__ == .__main__.:", "raise NotImplementedError"]
[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core"]
9 changes: 0 additions & 9 deletions test.sh

This file was deleted.