Skip to content

Commit 1c35bd0

Browse files
committed
refactoring import typing type-hints
1 parent 8b366f5 commit 1c35bd0

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

click_option_group/_core.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
import typing as ty
3+
from typing import Optional, List, Tuple, Dict, Set
4+
45
import collections
56
import weakref
67
import inspect
@@ -91,8 +92,8 @@ class OptionGroup:
9192
:param help: the group help text or None
9293
"""
9394

94-
def __init__(self, name: ty.Optional[str] = None, *,
95-
hidden=False, help: ty.Optional[str] = None) -> None: # noqa
95+
def __init__(self, name: Optional[str] = None, *,
96+
hidden=False, help: Optional[str] = None) -> None: # noqa
9697
self._name = name if name else ''
9798
self._help = inspect.cleandoc(help if help else '')
9899
self._hidden = hidden
@@ -117,13 +118,13 @@ def help(self) -> str:
117118
return self._help
118119

119120
@property
120-
def name_extra(self) -> ty.List[str]:
121+
def name_extra(self) -> List[str]:
121122
"""Returns extra name attributes for the group
122123
"""
123124
return []
124125

125126
@property
126-
def forbidden_option_attrs(self) -> ty.List[str]:
127+
def forbidden_option_attrs(self) -> List[str]:
127128
"""Returns the list of forbidden option attributes for the group
128129
"""
129130
return []
@@ -140,7 +141,7 @@ def get_default_name(self, ctx: click.Context) -> str:
140141
option_names = '|'.join(self.get_option_names(ctx))
141142
return f'({option_names})'
142143

143-
def get_help_record(self, ctx: click.Context) -> ty.Optional[ty.Tuple[str, str]]:
144+
def get_help_record(self, ctx: click.Context) -> Optional[Tuple[str, str]]:
144145
"""Returns the help record for the group
145146
146147
:param ctx: Click Context object
@@ -186,17 +187,17 @@ def decorator(func):
186187

187188
return decorator
188189

189-
def get_options(self, ctx: click.Context) -> ty.Dict[str, GroupedOption]:
190+
def get_options(self, ctx: click.Context) -> Dict[str, GroupedOption]:
190191
"""Returns the dictionary with group options
191192
"""
192193
return self._options.get(resolve_wrappers(ctx.command.callback), {})
193194

194-
def get_option_names(self, ctx: click.Context) -> ty.List[str]:
195+
def get_option_names(self, ctx: click.Context) -> List[str]:
195196
"""Returns the list with option names ordered by addition in the group
196197
"""
197198
return list(reversed(list(self.get_options(ctx))))
198199

199-
def get_error_hint(self, ctx, option_names: ty.Optional[ty.Set[str]] = None) -> str:
200+
def get_error_hint(self, ctx, option_names: Optional[Set[str]] = None) -> str:
200201
options = self.get_options(ctx)
201202
text = ''
202203

@@ -258,11 +259,11 @@ class RequiredAnyOptionGroup(OptionGroup):
258259
"""
259260

260261
@property
261-
def forbidden_option_attrs(self) -> ty.List[str]:
262+
def forbidden_option_attrs(self) -> List[str]:
262263
return ['required']
263264

264265
@property
265-
def name_extra(self) -> ty.List[str]:
266+
def name_extra(self) -> List[str]:
266267
return super().name_extra + ['required_any']
267268

268269
def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: dict) -> None:
@@ -296,11 +297,11 @@ class RequiredAllOptionGroup(OptionGroup):
296297
"""
297298

298299
@property
299-
def forbidden_option_attrs(self) -> ty.List[str]:
300+
def forbidden_option_attrs(self) -> List[str]:
300301
return ['required', 'hidden']
301302

302303
@property
303-
def name_extra(self) -> ty.List[str]:
304+
def name_extra(self) -> List[str]:
304305
return super().name_extra + ['required_all']
305306

306307
def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: dict) -> None:
@@ -325,11 +326,11 @@ class MutuallyExclusiveOptionGroup(OptionGroup):
325326
"""
326327

327328
@property
328-
def forbidden_option_attrs(self) -> ty.List[str]:
329+
def forbidden_option_attrs(self) -> List[str]:
329330
return ['required']
330331

331332
@property
332-
def name_extra(self) -> ty.List[str]:
333+
def name_extra(self) -> List[str]:
333334
return super().name_extra + ['mutually_exclusive']
334335

335336
def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: dict) -> None:
@@ -356,7 +357,7 @@ class RequiredMutuallyExclusiveOptionGroup(MutuallyExclusiveOptionGroup):
356357
"""
357358

358359
@property
359-
def name_extra(self) -> ty.List[str]:
360+
def name_extra(self) -> List[str]:
360361
return super().name_extra + ['required']
361362

362363
def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: dict) -> None:
@@ -384,11 +385,11 @@ class AllOptionGroup(OptionGroup):
384385
"""
385386

386387
@property
387-
def forbidden_option_attrs(self) -> ty.List[str]:
388+
def forbidden_option_attrs(self) -> List[str]:
388389
return ['required', 'hidden']
389390

390391
@property
391-
def name_extra(self) -> ty.List[str]:
392+
def name_extra(self) -> List[str]:
392393
return super().name_extra + ['all_or_none']
393394

394395
def handle_parse_result(self, option: GroupedOption, ctx: click.Context, opts: dict) -> None:

click_option_group/_decorators.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
import typing as ty
3+
from typing import Optional, NamedTuple, List, Tuple, Dict, Any, Type
4+
45
import collections.abc as abc
56
import collections
67
import warnings
@@ -15,9 +16,9 @@
1516
)
1617

1718

18-
class OptionStackItem(ty.NamedTuple):
19-
param_decls: ty.Tuple[str, ...]
20-
attrs: ty.Dict[str, ty.Any]
19+
class OptionStackItem(NamedTuple):
20+
param_decls: Tuple[str, ...]
21+
attrs: Dict[str, Any]
2122
param_count: int
2223

2324

@@ -61,21 +62,24 @@ class _OptGroup:
6162
"""
6263

6364
def __init__(self) -> None:
64-
self._decorating_state: ty.Dict[abc.Callable, ty.List[OptionStackItem]] = collections.defaultdict(list)
65-
self._not_attached_options: ty.Dict[abc.Callable, ty.List[click.Option]] = collections.defaultdict(list)
65+
self._decorating_state: Dict[abc.Callable, List[OptionStackItem]] = collections.defaultdict(list)
66+
self._not_attached_options: Dict[abc.Callable, List[click.Option]] = collections.defaultdict(list)
6667
self._outer_frame_index = 1
6768

68-
def __call__(self, name: ty.Optional[str] = None, help: ty.Optional[str] = None,
69-
cls: ty.Optional[ty.Type[OptionGroup]] = None, **attrs):
69+
def __call__(self,
70+
name: Optional[str] = None,
71+
help: Optional[str] = None,
72+
cls: Optional[Type[OptionGroup]] = None, **attrs):
7073
try:
7174
self._outer_frame_index = 2
7275
return self.group(name, cls=cls, help=help, **attrs)
7376
finally:
7477
self._outer_frame_index = 1
7578

76-
def group(self, name: ty.Optional[str] = None, *,
77-
cls: ty.Optional[ty.Type[OptionGroup]] = None,
78-
help: ty.Optional[str] = None, **attrs):
79+
def group(self,
80+
name: Optional[str] = None, *,
81+
cls: Optional[Type[OptionGroup]] = None,
82+
help: Optional[str] = None, **attrs):
7983
"""The decorator creates a new group and collects its options
8084
8185
Creates the option group and registers all grouped options

click_option_group/_helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
import typing as ty
3+
from typing import List, Tuple
4+
45
import collections.abc as abc
56
import random
67
import string
@@ -11,7 +12,7 @@
1112
FAKE_OPT_NAME_LEN = 30
1213

1314

14-
def get_callback_and_params(func) -> ty.Tuple[abc.Callable, ty.List[click.Option]]:
15+
def get_callback_and_params(func) -> Tuple[abc.Callable, List[click.Option]]:
1516
"""Returns callback function and its parameters list
1617
1718
:param func: decorated function or click Command

0 commit comments

Comments
 (0)