MCPcopy
hub / github.com/pydantic/pydantic / sequence_validator

Function sequence_validator

pydantic/_internal/_validators.py:28–65  ·  view source on GitHub ↗

Validator for `Sequence` types, isinstance(v, Sequence) has already been called.

(
    input_value: Sequence[Any],
    /,
    validator: core_schema.ValidatorFunctionWrapHandler,
)

Source from the content-addressed store, hash-verified

26
27
28def sequence_validator(
29 input_value: Sequence[Any],
30 /,
31 validator: core_schema.ValidatorFunctionWrapHandler,
32) -> Sequence[Any]:
33 """Validator for `Sequence` types, isinstance(v, Sequence) has already been called."""
34 value_type = type(input_value)
35
36 # We don't accept any plain string as a sequence
37 # Relevant issue: https://github.com/pydantic/pydantic/issues/5595
38 if issubclass(value_type, (str, bytes)):
39 raise PydanticCustomError(
40 'sequence_str',
41 "'{type_name}' instances are not allowed as a Sequence value",
42 {'type_name': value_type.__name__},
43 )
44
45 # TODO: refactor sequence validation to validate with either a list or a tuple
46 # schema, depending on the type of the value.
47 # Additionally, we should be able to remove one of either this validator or the
48 # SequenceValidator in _std_types_schema.py (preferably this one, while porting over some logic).
49 # Effectively, a refactor for sequence validation is needed.
50 if value_type is tuple:
51 input_value = list(input_value)
52
53 v_list = validator(input_value)
54
55 # the rest of the logic is just re-creating the original type from `v_list`
56 if value_type is list:
57 return v_list
58 elif issubclass(value_type, range):
59 # return the list as we probably can't re-create the range
60 return v_list
61 elif value_type is tuple:
62 return tuple(v_list)
63 else:
64 # best guess at how to re-create the original type, more custom construction logic might be required
65 return value_type(v_list) # type: ignore[call-arg]
66
67
68def import_string(value: Any) -> Any:

Callers

nothing calls this directly

Calls 1

validatorFunction · 0.50

Tested by

no test coverage detected