MCPcopy
hub / github.com/python/mypy / try_split

Function try_split

mypy/config_parser.py:61–85  ·  view source on GitHub ↗

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 = ",")

Source from the content-addressed store, hash-verified

59
60
61def 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
88def validate_package_allow_list(allow_list: list[str]) -> list[str]:

Callers 1

config_parser.pyFile · 0.85

Calls 5

isinstanceFunction · 0.85
complainFunction · 0.85
stripMethod · 0.80
splitMethod · 0.80
popMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…