Split and trim a str or sequence (eg: list) of str into a list of str. If an element of the input is not str, a type error will be raised.
(v: str | Sequence[str] | object, split_regex: str = ",")
| 59 | |
| 60 | |
| 61 | def try_split(v: str | Sequence[str] | object, split_regex: str = ",") -> list[str]: |
| 62 | """Split and trim a str or sequence (eg: list) of str into a list of str. |
| 63 | If an element of the input is not str, a type error will be raised.""" |
| 64 | |
| 65 | def complain(x: object, additional_info: str = "") -> Never: |
| 66 | raise argparse.ArgumentTypeError( |
| 67 | f"Expected a list or a stringified version thereof, but got: '{x}', of type {type(x).__name__}.{additional_info}" |
| 68 | ) |
| 69 | |
| 70 | if isinstance(v, str): |
| 71 | items = [p.strip() for p in re.split(split_regex, v)] |
| 72 | if items and items[-1] == "": |
| 73 | items.pop(-1) |
| 74 | return items |
| 75 | elif isinstance(v, Sequence): |
| 76 | return [ |
| 77 | ( |
| 78 | p.strip() |
| 79 | if isinstance(p, str) |
| 80 | else complain(p, additional_info=" (As an element of the list.)") |
| 81 | ) |
| 82 | for p in v |
| 83 | ] |
| 84 | else: |
| 85 | complain(v) |
| 86 | |
| 87 | |
| 88 | def validate_package_allow_list(allow_list: list[str]) -> list[str]: |
no test coverage detected
searching dependent graphs…