Skip to content

Commit 72be94b

Browse files
committed
Add type annotations to config module
1 parent bbb9c6e commit 72be94b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

stagpy/config.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,36 @@
44
interface.
55
"""
66

7+
from __future__ import annotations
78
import pathlib
9+
import typing
810

911
from loam.manager import ConfOpt as Conf
1012
from loam import tools
1113
from loam.tools import switch_opt, command_flag
1214

15+
if typing.TYPE_CHECKING:
16+
from typing import Union, Callable, TypeVar, Tuple
17+
T = TypeVar('T')
1318

14-
def _slice_or_int(arg):
19+
20+
def _slice_or_int(arg: str) -> Union[slice, int]:
1521
"""Parse a string into an integer or slice."""
1622
if ':' in arg:
1723
idxs = arg.split(':')
1824
if len(idxs) > 3:
1925
raise ValueError(f'{arg} is an invalid slice')
20-
idxs[0] = int(idxs[0]) if idxs[0] else None
21-
idxs[1] = int(idxs[1]) if idxs[1] else None
26+
slice_parts = [int(idxs[0]) if idxs[0] else None,
27+
int(idxs[1]) if idxs[1] else None]
2228
if len(idxs) == 3:
23-
idxs[2] = int(idxs[2]) if idxs[2] else None
29+
slice_parts.append(int(idxs[2]) if idxs[2] else None)
2430
else:
25-
idxs = idxs[0:2] + [1]
26-
return slice(*idxs)
31+
slice_parts.append(1)
32+
return slice(*slice_parts)
2733
return int(arg)
2834

2935

30-
def _list_of(from_str):
36+
def _list_of(from_str: Callable[[str], T]) -> Callable[[str], Tuple[T]]:
3137
"""Return fn parsing a str as a comma-separated list of given type."""
3238
def parser(arg):
3339
return tuple(from_str(v) for v in map(str.strip, arg.split(',')) if v)

0 commit comments

Comments
 (0)