|
17 | 17 |
|
18 | 18 | from collections.abc import Mapping, MutableMapping, Sequence
|
19 | 19 | from typing import Any, Callable, Final, TextIO, Union
|
20 |
| -from typing_extensions import TypeAlias as _TypeAlias |
| 20 | +from typing_extensions import Never, TypeAlias |
21 | 21 |
|
22 | 22 | from mypy import defaults
|
23 | 23 | from mypy.options import PER_MODULE_OPTIONS, Options
|
24 | 24 |
|
25 |
| -_CONFIG_VALUE_TYPES: _TypeAlias = Union[ |
| 25 | +_CONFIG_VALUE_TYPES: TypeAlias = Union[ |
26 | 26 | str, bool, int, float, dict[str, str], list[str], tuple[int, int]
|
27 | 27 | ]
|
28 |
| -_INI_PARSER_CALLABLE: _TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES] |
| 28 | +_INI_PARSER_CALLABLE: TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES] |
29 | 29 |
|
30 | 30 |
|
31 | 31 | class VersionTypeError(argparse.ArgumentTypeError):
|
@@ -60,14 +60,31 @@ def parse_version(v: str | float) -> tuple[int, int]:
|
60 | 60 | return major, minor
|
61 | 61 |
|
62 | 62 |
|
63 |
| -def try_split(v: str | Sequence[str], split_regex: str = "[,]") -> list[str]: |
64 |
| - """Split and trim a str or list of str into a list of str""" |
| 63 | +def try_split(v: str | Sequence[str] | object, split_regex: str = ",") -> list[str]: |
| 64 | + """Split and trim a str or sequence (eg: list) of str into a list of str. |
| 65 | + If an element of the input is not str, a type error will be raised.""" |
| 66 | + |
| 67 | + def complain(x: object, additional_info: str = "") -> Never: |
| 68 | + raise argparse.ArgumentTypeError( |
| 69 | + f"Expected a list or a stringified version thereof, but got: '{x}', of type {type(x).__name__}.{additional_info}" |
| 70 | + ) |
| 71 | + |
65 | 72 | if isinstance(v, str):
|
66 | 73 | items = [p.strip() for p in re.split(split_regex, v)]
|
67 | 74 | if items and items[-1] == "":
|
68 | 75 | items.pop(-1)
|
69 | 76 | return items
|
70 |
| - return [p.strip() for p in v] |
| 77 | + elif isinstance(v, Sequence): |
| 78 | + return [ |
| 79 | + ( |
| 80 | + p.strip() |
| 81 | + if isinstance(p, str) |
| 82 | + else complain(p, additional_info=" (As an element of the list.)") |
| 83 | + ) |
| 84 | + for p in v |
| 85 | + ] |
| 86 | + else: |
| 87 | + complain(v) |
71 | 88 |
|
72 | 89 |
|
73 | 90 | def validate_codes(codes: list[str]) -> list[str]:
|
|
0 commit comments