3
3
import traceback
4
4
from functools import wraps
5
5
from pathlib import Path
6
- from typing import Any , Callable , Optional , TypeVar , Union
6
+ from typing import Any , Callable , Coroutine , NoReturn , Optional , TypeVar , Union
7
7
8
8
import pendulum
9
9
import typer
29
29
from .client import initialize_client_sync
30
30
31
31
YamlFileVar = TypeVar ("YamlFileVar" , bound = YamlFile )
32
+ T = TypeVar ("T" )
32
33
33
34
34
35
def init_logging (debug : bool = False ) -> None :
@@ -42,7 +43,7 @@ def init_logging(debug: bool = False) -> None:
42
43
logging .getLogger ("infrahubctl" )
43
44
44
45
45
- def handle_exception (exc : Exception , console : Console , exit_code : int ):
46
+ def handle_exception (exc : Exception , console : Console , exit_code : int ) -> NoReturn :
46
47
"""Handle exeception in a different fashion based on its type."""
47
48
if isinstance (exc , Exit ):
48
49
raise typer .Exit (code = exc .exit_code )
@@ -67,16 +68,18 @@ def handle_exception(exc: Exception, console: Console, exit_code: int):
67
68
raise typer .Exit (code = exit_code )
68
69
69
70
70
- def catch_exception (console : Optional [Console ] = None , exit_code : int = 1 ):
71
+ def catch_exception (
72
+ console : Optional [Console ] = None , exit_code : int = 1
73
+ ) -> Callable [[Callable [..., T ]], Callable [..., Union [T , Coroutine [Any , Any , T ], NoReturn ]]]:
71
74
"""Decorator to handle exception for commands."""
72
75
if not console :
73
76
console = Console ()
74
77
75
- def decorator (func : Callable ) :
78
+ def decorator (func : Callable [..., T ]) -> Callable [..., Union [ T , Coroutine [ Any , Any , T ], NoReturn ]] :
76
79
if asyncio .iscoroutinefunction (func ):
77
80
78
81
@wraps (func )
79
- async def async_wrapper (* args : Any , ** kwargs : Any ):
82
+ async def async_wrapper (* args : Any , ** kwargs : Any ) -> Union [ T , NoReturn ] :
80
83
try :
81
84
return await func (* args , ** kwargs )
82
85
except (Error , Exception ) as exc : # pylint: disable=broad-exception-caught
@@ -85,7 +88,7 @@ async def async_wrapper(*args: Any, **kwargs: Any):
85
88
return async_wrapper
86
89
87
90
@wraps (func )
88
- def wrapper (* args : Any , ** kwargs : Any ):
91
+ def wrapper (* args : Any , ** kwargs : Any ) -> Union [ T , NoReturn ] :
89
92
try :
90
93
return func (* args , ** kwargs )
91
94
except (Error , Exception ) as exc : # pylint: disable=broad-exception-caught
@@ -116,8 +119,10 @@ def execute_graphql_query(
116
119
)
117
120
118
121
if debug :
119
- message = ("-" * 40 , f"Response for GraphQL Query { query } " , response , "-" * 40 )
120
- console .print ("\n " .join (message ))
122
+ console .print ("-" * 40 )
123
+ console .print (f"Response for GraphQL Query { query } " )
124
+ console .print (response )
125
+ console .print ("-" * 40 )
121
126
122
127
return response
123
128
0 commit comments