Skip to content

Fix typing on ctl.utils #92

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

Merged
merged 1 commit into from
Oct 17, 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
21 changes: 13 additions & 8 deletions infrahub_sdk/ctl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import traceback
from functools import wraps
from pathlib import Path
from typing import Any, Callable, Optional, TypeVar, Union
from typing import Any, Callable, Coroutine, NoReturn, Optional, TypeVar, Union

import pendulum
import typer
Expand All @@ -29,6 +29,7 @@
from .client import initialize_client_sync

YamlFileVar = TypeVar("YamlFileVar", bound=YamlFile)
T = TypeVar("T")


def init_logging(debug: bool = False) -> None:
Expand All @@ -42,7 +43,7 @@
logging.getLogger("infrahubctl")


def handle_exception(exc: Exception, console: Console, exit_code: int):
def handle_exception(exc: Exception, console: Console, exit_code: int) -> NoReturn:
"""Handle exeception in a different fashion based on its type."""
if isinstance(exc, Exit):
raise typer.Exit(code=exc.exit_code)
Expand All @@ -67,16 +68,18 @@
raise typer.Exit(code=exit_code)


def catch_exception(console: Optional[Console] = None, exit_code: int = 1):
def catch_exception(
console: Optional[Console] = None, exit_code: int = 1
) -> Callable[[Callable[..., T]], Callable[..., Union[T, Coroutine[Any, Any, T], NoReturn]]]:
"""Decorator to handle exception for commands."""
if not console:
console = Console()

def decorator(func: Callable):
def decorator(func: Callable[..., T]) -> Callable[..., Union[T, Coroutine[Any, Any, T], NoReturn]]:
if asyncio.iscoroutinefunction(func):

@wraps(func)
async def async_wrapper(*args: Any, **kwargs: Any):
async def async_wrapper(*args: Any, **kwargs: Any) -> Union[T, NoReturn]:
try:
return await func(*args, **kwargs)
except (Error, Exception) as exc: # pylint: disable=broad-exception-caught
Expand All @@ -85,7 +88,7 @@
return async_wrapper

@wraps(func)
def wrapper(*args: Any, **kwargs: Any):
def wrapper(*args: Any, **kwargs: Any) -> Union[T, NoReturn]:
try:
return func(*args, **kwargs)
except (Error, Exception) as exc: # pylint: disable=broad-exception-caught
Expand Down Expand Up @@ -116,8 +119,10 @@
)

if debug:
message = ("-" * 40, f"Response for GraphQL Query {query}", response, "-" * 40)
console.print("\n".join(message))
console.print("-" * 40)
console.print(f"Response for GraphQL Query {query}")
console.print(response)
console.print("-" * 40)

Check warning on line 125 in infrahub_sdk/ctl/utils.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/ctl/utils.py#L122-L125

Added lines #L122 - L125 were not covered by tests

return response

Expand Down
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,6 @@ ignore_errors = true
module = "infrahub_sdk.ctl.schema"
ignore_errors = true

[[tool.mypy.overrides]]
module = "infrahub_sdk.ctl.utils"
ignore_errors = true

[[tool.mypy.overrides]]
module = "infrahub_sdk.utils"
ignore_errors = true
Expand Down