diff options
author | Nejc Habjan <nejc.habjan@siemens.com> | 2022-05-01 20:55:56 +0200 |
---|---|---|
committer | Nejc Habjan <nejc.habjan@siemens.com> | 2022-06-06 10:41:26 +0200 |
commit | 56280040e415b39ca0e9d032a927f0a39e734b9b (patch) | |
tree | c3e1b6d2fb077ef41b1347d49d37636e79d9c1f5 /gitlab/cli.py | |
parent | 0e3c461a2ad6ade9819db864261a82b357ce5808 (diff) | |
download | gitlab-refactor/decouple-cli-from-custom-args.tar.gz |
refactor: decouple CLI from custom method argumentsrefactor/decouple-cli-from-custom-args
Diffstat (limited to 'gitlab/cli.py')
-rw-r--r-- | gitlab/cli.py | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py index 9793964..5758555 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -23,12 +23,13 @@ import os import re import sys from types import ModuleType -from typing import Any, Callable, cast, Dict, Optional, Tuple, Type, TypeVar, Union +from typing import Any, Callable, cast, Dict, Optional, Tuple, Type, Union from requests.structures import CaseInsensitiveDict import gitlab.config from gitlab.base import RESTObject +from gitlab.types import F, RequiredOptional # This regex is based on: # https://github.com/jpvanhal/inflection/blob/master/inflection/__init__.py @@ -43,24 +44,18 @@ camel_lowerupper_regex = re.compile(r"([a-z\d])([A-Z])") custom_actions: Dict[str, Dict[str, Tuple[Tuple[str, ...], Tuple[str, ...], bool]]] = {} -# For an explanation of how these type-hints work see: -# https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators -# -# The goal here is that functions which get decorated will retain their types. -__F = TypeVar("__F", bound=Callable[..., Any]) - - def register_custom_action( cls_names: Union[str, Tuple[str, ...]], - mandatory: Tuple[str, ...] = (), - optional: Tuple[str, ...] = (), custom_action: Optional[str] = None, -) -> Callable[[__F], __F]: - def wrap(f: __F) -> __F: +) -> Callable[[F], F]: + def wrap(f: F) -> F: @functools.wraps(f) def wrapped_f(*args: Any, **kwargs: Any) -> Any: return f(*args, **kwargs) + action = custom_action or f.__name__.replace("_", "-") + custom_attrs = getattr(f, "_custom_attrs", RequiredOptional()) + # in_obj defines whether the method belongs to the obj or the manager in_obj = True if isinstance(cls_names, tuple): @@ -76,10 +71,13 @@ def register_custom_action( if final_name not in custom_actions: custom_actions[final_name] = {} - action = custom_action or f.__name__.replace("_", "-") - custom_actions[final_name][action] = (mandatory, optional, in_obj) + custom_actions[final_name][action] = ( + custom_attrs.required, + custom_attrs.optional, + in_obj, + ) - return cast(__F, wrapped_f) + return cast(F, wrapped_f) return wrap |