|
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 TypeAlias, Never |
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,21 +60,20 @@ 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] | Any, split_regex: str = "[,]") -> list[str] | Any: |
| 63 | +def try_split(v: str | Sequence[str] | Any, split_regex: str = "[,]") -> list[str]: |
64 | 64 | """Split and trim a str or sequence (eg: list) of str into a list of str. |
65 | | - Non-str elements will simply be returned untouched. Feel free to one day |
66 | | - fix the typing of the calling code, and remove this caveat and Any.""" |
| 65 | + If an element of the input is not str, a type error will be raised.""" |
| 66 | + def complain(x: object, additional_info: str = "") -> Never: |
| 67 | + raise argparse.ArgumentTypeError(f"Expected a list or a stringified version thereof, but got: '{x}', of type {type(x)}.{additional_info}") |
67 | 68 | if isinstance(v, str): |
68 | 69 | items = [p.strip() for p in re.split(split_regex, v)] |
69 | 70 | if items and items[-1] == "": |
70 | 71 | items.pop(-1) |
71 | 72 | return items |
72 | 73 | elif isinstance(v, Sequence): |
73 | | - return [p.strip() if isinstance(p, str) else p for p in v] |
| 74 | + return [p.strip() if isinstance(p, str) else complain(p, additional_info=" (As an element of the list.)") for p in v] |
74 | 75 | else: |
75 | | - raise argparse.ArgumentTypeError( |
76 | | - f"Expected a list or a stringified version thereof, but got: '{v}'" |
77 | | - ) |
| 76 | + complain(v) |
78 | 77 |
|
79 | 78 |
|
80 | 79 | def validate_codes(codes: list[str]) -> list[str]: |
|
0 commit comments