Skip to content

prefect.types

validate_set_T_from_delim_string(value, type_, delim=None)

"no-info" before validator useful in scooping env vars

e.g. PREFECT_CLIENT_RETRY_EXTRA_CODES=429,502,503 -> {429, 502, 503} e.g. PREFECT_CLIENT_RETRY_EXTRA_CODES=429 -> {429}

Source code in src/prefect/types/__init__.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def validate_set_T_from_delim_string(
    value: Union[str, T, Set[T], None], type_, delim=None
) -> Set[T]:
    """
    "no-info" before validator useful in scooping env vars

    e.g. `PREFECT_CLIENT_RETRY_EXTRA_CODES=429,502,503` -> `{429, 502, 503}`
    e.g. `PREFECT_CLIENT_RETRY_EXTRA_CODES=429` -> `{429}`
    """
    if not value:
        return set()

    T_adapter = TypeAdapter(type_)
    delim = delim or ","
    if isinstance(value, str):
        return {T_adapter.validate_strings(s) for s in value.split(delim)}
    errors = []
    try:
        return {T_adapter.validate_python(value)}
    except pydantic.ValidationError as e:
        errors.append(e)
    try:
        return TypeAdapter(Set[type_]).validate_python(value)
    except pydantic.ValidationError as e:
        errors.append(e)
    raise ValueError(f"Invalid set[{type_}]: {errors}")