_validators.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. """Validator functions for standard library types.
  2. Import of this module is deferred since it contains imports of many standard library modules.
  3. """
  4. from __future__ import annotations as _annotations
  5. import collections.abc
  6. import math
  7. import re
  8. import typing
  9. from collections.abc import Sequence
  10. from decimal import Decimal
  11. from fractions import Fraction
  12. from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
  13. from typing import Any, Callable, TypeVar, Union, cast
  14. from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
  15. import typing_extensions
  16. from pydantic_core import PydanticCustomError, PydanticKnownError, core_schema
  17. from typing_extensions import get_args, get_origin
  18. from typing_inspection import typing_objects
  19. from pydantic._internal._import_utils import import_cached_field_info
  20. from pydantic.errors import PydanticSchemaGenerationError
  21. def sequence_validator(
  22. input_value: Sequence[Any],
  23. /,
  24. validator: core_schema.ValidatorFunctionWrapHandler,
  25. ) -> Sequence[Any]:
  26. """Validator for `Sequence` types, isinstance(v, Sequence) has already been called."""
  27. value_type = type(input_value)
  28. # We don't accept any plain string as a sequence
  29. # Relevant issue: https://github.com/pydantic/pydantic/issues/5595
  30. if issubclass(value_type, (str, bytes)):
  31. raise PydanticCustomError(
  32. 'sequence_str',
  33. "'{type_name}' instances are not allowed as a Sequence value",
  34. {'type_name': value_type.__name__},
  35. )
  36. # TODO: refactor sequence validation to validate with either a list or a tuple
  37. # schema, depending on the type of the value.
  38. # Additionally, we should be able to remove one of either this validator or the
  39. # SequenceValidator in _std_types_schema.py (preferably this one, while porting over some logic).
  40. # Effectively, a refactor for sequence validation is needed.
  41. if value_type is tuple:
  42. input_value = list(input_value)
  43. v_list = validator(input_value)
  44. # the rest of the logic is just re-creating the original type from `v_list`
  45. if value_type is list:
  46. return v_list
  47. elif issubclass(value_type, range):
  48. # return the list as we probably can't re-create the range
  49. return v_list
  50. elif value_type is tuple:
  51. return tuple(v_list)
  52. else:
  53. # best guess at how to re-create the original type, more custom construction logic might be required
  54. return value_type(v_list) # type: ignore[call-arg]
  55. def import_string(value: Any) -> Any:
  56. if isinstance(value, str):
  57. try:
  58. return _import_string_logic(value)
  59. except ImportError as e:
  60. raise PydanticCustomError('import_error', 'Invalid python path: {error}', {'error': str(e)}) from e
  61. else:
  62. # otherwise we just return the value and let the next validator do the rest of the work
  63. return value
  64. def _import_string_logic(dotted_path: str) -> Any:
  65. """Inspired by uvicorn — dotted paths should include a colon before the final item if that item is not a module.
  66. (This is necessary to distinguish between a submodule and an attribute when there is a conflict.).
  67. If the dotted path does not include a colon and the final item is not a valid module, importing as an attribute
  68. rather than a submodule will be attempted automatically.
  69. So, for example, the following values of `dotted_path` result in the following returned values:
  70. * 'collections': <module 'collections'>
  71. * 'collections.abc': <module 'collections.abc'>
  72. * 'collections.abc:Mapping': <class 'collections.abc.Mapping'>
  73. * `collections.abc.Mapping`: <class 'collections.abc.Mapping'> (though this is a bit slower than the previous line)
  74. An error will be raised under any of the following scenarios:
  75. * `dotted_path` contains more than one colon (e.g., 'collections:abc:Mapping')
  76. * the substring of `dotted_path` before the colon is not a valid module in the environment (e.g., '123:Mapping')
  77. * the substring of `dotted_path` after the colon is not an attribute of the module (e.g., 'collections:abc123')
  78. """
  79. from importlib import import_module
  80. components = dotted_path.strip().split(':')
  81. if len(components) > 2:
  82. raise ImportError(f"Import strings should have at most one ':'; received {dotted_path!r}")
  83. attribute = None
  84. if len(components) == 2:
  85. attribute = components[1]
  86. module_path = components[0]
  87. if not module_path:
  88. raise ImportError(f'Import strings should have a nonempty module name; received {dotted_path!r}')
  89. try:
  90. module = import_module(module_path)
  91. except ModuleNotFoundError:
  92. if attribute is None and '.' in module_path:
  93. # Try interpreting the final dotted segment as an attribute, not a submodule
  94. maybe_module_path, maybe_attribute = module_path.rsplit('.', 1)
  95. try:
  96. return _import_string_logic(f'{maybe_module_path}:{maybe_attribute}')
  97. except ImportError:
  98. pass
  99. raise
  100. if attribute is not None:
  101. try:
  102. return getattr(module, attribute)
  103. except AttributeError as e:
  104. raise ImportError(f'cannot import name {attribute!r} from {module_path!r}') from e
  105. else:
  106. return module
  107. def pattern_either_validator(input_value: Any, /) -> re.Pattern[Any]:
  108. if isinstance(input_value, re.Pattern):
  109. return input_value
  110. elif isinstance(input_value, (str, bytes)):
  111. # todo strict mode
  112. return compile_pattern(input_value) # type: ignore
  113. else:
  114. raise PydanticCustomError('pattern_type', 'Input should be a valid pattern')
  115. def pattern_str_validator(input_value: Any, /) -> re.Pattern[str]:
  116. if isinstance(input_value, re.Pattern):
  117. if isinstance(input_value.pattern, str):
  118. return input_value
  119. else:
  120. raise PydanticCustomError('pattern_str_type', 'Input should be a string pattern')
  121. elif isinstance(input_value, str):
  122. return compile_pattern(input_value)
  123. elif isinstance(input_value, bytes):
  124. raise PydanticCustomError('pattern_str_type', 'Input should be a string pattern')
  125. else:
  126. raise PydanticCustomError('pattern_type', 'Input should be a valid pattern')
  127. def pattern_bytes_validator(input_value: Any, /) -> re.Pattern[bytes]:
  128. if isinstance(input_value, re.Pattern):
  129. if isinstance(input_value.pattern, bytes):
  130. return input_value
  131. else:
  132. raise PydanticCustomError('pattern_bytes_type', 'Input should be a bytes pattern')
  133. elif isinstance(input_value, bytes):
  134. return compile_pattern(input_value)
  135. elif isinstance(input_value, str):
  136. raise PydanticCustomError('pattern_bytes_type', 'Input should be a bytes pattern')
  137. else:
  138. raise PydanticCustomError('pattern_type', 'Input should be a valid pattern')
  139. PatternType = TypeVar('PatternType', str, bytes)
  140. def compile_pattern(pattern: PatternType) -> re.Pattern[PatternType]:
  141. try:
  142. return re.compile(pattern)
  143. except re.error:
  144. raise PydanticCustomError('pattern_regex', 'Input should be a valid regular expression')
  145. def ip_v4_address_validator(input_value: Any, /) -> IPv4Address:
  146. if isinstance(input_value, IPv4Address):
  147. return input_value
  148. try:
  149. return IPv4Address(input_value)
  150. except ValueError:
  151. raise PydanticCustomError('ip_v4_address', 'Input is not a valid IPv4 address')
  152. def ip_v6_address_validator(input_value: Any, /) -> IPv6Address:
  153. if isinstance(input_value, IPv6Address):
  154. return input_value
  155. try:
  156. return IPv6Address(input_value)
  157. except ValueError:
  158. raise PydanticCustomError('ip_v6_address', 'Input is not a valid IPv6 address')
  159. def ip_v4_network_validator(input_value: Any, /) -> IPv4Network:
  160. """Assume IPv4Network initialised with a default `strict` argument.
  161. See more:
  162. https://docs.python.org/library/ipaddress.html#ipaddress.IPv4Network
  163. """
  164. if isinstance(input_value, IPv4Network):
  165. return input_value
  166. try:
  167. return IPv4Network(input_value)
  168. except ValueError:
  169. raise PydanticCustomError('ip_v4_network', 'Input is not a valid IPv4 network')
  170. def ip_v6_network_validator(input_value: Any, /) -> IPv6Network:
  171. """Assume IPv6Network initialised with a default `strict` argument.
  172. See more:
  173. https://docs.python.org/library/ipaddress.html#ipaddress.IPv6Network
  174. """
  175. if isinstance(input_value, IPv6Network):
  176. return input_value
  177. try:
  178. return IPv6Network(input_value)
  179. except ValueError:
  180. raise PydanticCustomError('ip_v6_network', 'Input is not a valid IPv6 network')
  181. def ip_v4_interface_validator(input_value: Any, /) -> IPv4Interface:
  182. if isinstance(input_value, IPv4Interface):
  183. return input_value
  184. try:
  185. return IPv4Interface(input_value)
  186. except ValueError:
  187. raise PydanticCustomError('ip_v4_interface', 'Input is not a valid IPv4 interface')
  188. def ip_v6_interface_validator(input_value: Any, /) -> IPv6Interface:
  189. if isinstance(input_value, IPv6Interface):
  190. return input_value
  191. try:
  192. return IPv6Interface(input_value)
  193. except ValueError:
  194. raise PydanticCustomError('ip_v6_interface', 'Input is not a valid IPv6 interface')
  195. def fraction_validator(input_value: Any, /) -> Fraction:
  196. if isinstance(input_value, Fraction):
  197. return input_value
  198. try:
  199. return Fraction(input_value)
  200. except ValueError:
  201. raise PydanticCustomError('fraction_parsing', 'Input is not a valid fraction')
  202. def forbid_inf_nan_check(x: Any) -> Any:
  203. if not math.isfinite(x):
  204. raise PydanticKnownError('finite_number')
  205. return x
  206. def _safe_repr(v: Any) -> int | float | str:
  207. """The context argument for `PydanticKnownError` requires a number or str type, so we do a simple repr() coercion for types like timedelta.
  208. See tests/test_types.py::test_annotated_metadata_any_order for some context.
  209. """
  210. if isinstance(v, (int, float, str)):
  211. return v
  212. return repr(v)
  213. def greater_than_validator(x: Any, gt: Any) -> Any:
  214. try:
  215. if not (x > gt):
  216. raise PydanticKnownError('greater_than', {'gt': _safe_repr(gt)})
  217. return x
  218. except TypeError:
  219. raise TypeError(f"Unable to apply constraint 'gt' to supplied value {x}")
  220. def greater_than_or_equal_validator(x: Any, ge: Any) -> Any:
  221. try:
  222. if not (x >= ge):
  223. raise PydanticKnownError('greater_than_equal', {'ge': _safe_repr(ge)})
  224. return x
  225. except TypeError:
  226. raise TypeError(f"Unable to apply constraint 'ge' to supplied value {x}")
  227. def less_than_validator(x: Any, lt: Any) -> Any:
  228. try:
  229. if not (x < lt):
  230. raise PydanticKnownError('less_than', {'lt': _safe_repr(lt)})
  231. return x
  232. except TypeError:
  233. raise TypeError(f"Unable to apply constraint 'lt' to supplied value {x}")
  234. def less_than_or_equal_validator(x: Any, le: Any) -> Any:
  235. try:
  236. if not (x <= le):
  237. raise PydanticKnownError('less_than_equal', {'le': _safe_repr(le)})
  238. return x
  239. except TypeError:
  240. raise TypeError(f"Unable to apply constraint 'le' to supplied value {x}")
  241. def multiple_of_validator(x: Any, multiple_of: Any) -> Any:
  242. try:
  243. if x % multiple_of:
  244. raise PydanticKnownError('multiple_of', {'multiple_of': _safe_repr(multiple_of)})
  245. return x
  246. except TypeError:
  247. raise TypeError(f"Unable to apply constraint 'multiple_of' to supplied value {x}")
  248. def min_length_validator(x: Any, min_length: Any) -> Any:
  249. try:
  250. if not (len(x) >= min_length):
  251. raise PydanticKnownError(
  252. 'too_short', {'field_type': 'Value', 'min_length': min_length, 'actual_length': len(x)}
  253. )
  254. return x
  255. except TypeError:
  256. raise TypeError(f"Unable to apply constraint 'min_length' to supplied value {x}")
  257. def max_length_validator(x: Any, max_length: Any) -> Any:
  258. try:
  259. if len(x) > max_length:
  260. raise PydanticKnownError(
  261. 'too_long',
  262. {'field_type': 'Value', 'max_length': max_length, 'actual_length': len(x)},
  263. )
  264. return x
  265. except TypeError:
  266. raise TypeError(f"Unable to apply constraint 'max_length' to supplied value {x}")
  267. def _extract_decimal_digits_info(decimal: Decimal) -> tuple[int, int]:
  268. """Compute the total number of digits and decimal places for a given [`Decimal`][decimal.Decimal] instance.
  269. This function handles both normalized and non-normalized Decimal instances.
  270. Example: Decimal('1.230') -> 4 digits, 3 decimal places
  271. Args:
  272. decimal (Decimal): The decimal number to analyze.
  273. Returns:
  274. tuple[int, int]: A tuple containing the number of decimal places and total digits.
  275. Though this could be divided into two separate functions, the logic is easier to follow if we couple the computation
  276. of the number of decimals and digits together.
  277. """
  278. try:
  279. decimal_tuple = decimal.as_tuple()
  280. assert isinstance(decimal_tuple.exponent, int)
  281. exponent = decimal_tuple.exponent
  282. num_digits = len(decimal_tuple.digits)
  283. if exponent >= 0:
  284. # A positive exponent adds that many trailing zeros
  285. # Ex: digit_tuple=(1, 2, 3), exponent=2 -> 12300 -> 0 decimal places, 5 digits
  286. num_digits += exponent
  287. decimal_places = 0
  288. else:
  289. # If the absolute value of the negative exponent is larger than the
  290. # number of digits, then it's the same as the number of digits,
  291. # because it'll consume all the digits in digit_tuple and then
  292. # add abs(exponent) - len(digit_tuple) leading zeros after the decimal point.
  293. # Ex: digit_tuple=(1, 2, 3), exponent=-2 -> 1.23 -> 2 decimal places, 3 digits
  294. # Ex: digit_tuple=(1, 2, 3), exponent=-4 -> 0.0123 -> 4 decimal places, 4 digits
  295. decimal_places = abs(exponent)
  296. num_digits = max(num_digits, decimal_places)
  297. return decimal_places, num_digits
  298. except (AssertionError, AttributeError):
  299. raise TypeError(f'Unable to extract decimal digits info from supplied value {decimal}')
  300. def max_digits_validator(x: Any, max_digits: Any) -> Any:
  301. try:
  302. _, num_digits = _extract_decimal_digits_info(x)
  303. _, normalized_num_digits = _extract_decimal_digits_info(x.normalize())
  304. if (num_digits > max_digits) and (normalized_num_digits > max_digits):
  305. raise PydanticKnownError(
  306. 'decimal_max_digits',
  307. {'max_digits': max_digits},
  308. )
  309. return x
  310. except TypeError:
  311. raise TypeError(f"Unable to apply constraint 'max_digits' to supplied value {x}")
  312. def decimal_places_validator(x: Any, decimal_places: Any) -> Any:
  313. try:
  314. decimal_places_, _ = _extract_decimal_digits_info(x)
  315. if decimal_places_ > decimal_places:
  316. normalized_decimal_places, _ = _extract_decimal_digits_info(x.normalize())
  317. if normalized_decimal_places > decimal_places:
  318. raise PydanticKnownError(
  319. 'decimal_max_places',
  320. {'decimal_places': decimal_places},
  321. )
  322. return x
  323. except TypeError:
  324. raise TypeError(f"Unable to apply constraint 'decimal_places' to supplied value {x}")
  325. def deque_validator(input_value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> collections.deque[Any]:
  326. return collections.deque(handler(input_value), maxlen=getattr(input_value, 'maxlen', None))
  327. def defaultdict_validator(
  328. input_value: Any, handler: core_schema.ValidatorFunctionWrapHandler, default_default_factory: Callable[[], Any]
  329. ) -> collections.defaultdict[Any, Any]:
  330. if isinstance(input_value, collections.defaultdict):
  331. default_factory = input_value.default_factory
  332. return collections.defaultdict(default_factory, handler(input_value))
  333. else:
  334. return collections.defaultdict(default_default_factory, handler(input_value))
  335. def get_defaultdict_default_default_factory(values_source_type: Any) -> Callable[[], Any]:
  336. FieldInfo = import_cached_field_info()
  337. values_type_origin = get_origin(values_source_type)
  338. def infer_default() -> Callable[[], Any]:
  339. allowed_default_types: dict[Any, Any] = {
  340. tuple: tuple,
  341. collections.abc.Sequence: tuple,
  342. collections.abc.MutableSequence: list,
  343. list: list,
  344. typing.Sequence: list,
  345. set: set,
  346. typing.MutableSet: set,
  347. collections.abc.MutableSet: set,
  348. collections.abc.Set: frozenset,
  349. typing.MutableMapping: dict,
  350. typing.Mapping: dict,
  351. collections.abc.Mapping: dict,
  352. collections.abc.MutableMapping: dict,
  353. float: float,
  354. int: int,
  355. str: str,
  356. bool: bool,
  357. }
  358. values_type = values_type_origin or values_source_type
  359. instructions = 'set using `DefaultDict[..., Annotated[..., Field(default_factory=...)]]`'
  360. if typing_objects.is_typevar(values_type):
  361. def type_var_default_factory() -> None:
  362. raise RuntimeError(
  363. 'Generic defaultdict cannot be used without a concrete value type or an'
  364. ' explicit default factory, ' + instructions
  365. )
  366. return type_var_default_factory
  367. elif values_type not in allowed_default_types:
  368. # a somewhat subjective set of types that have reasonable default values
  369. allowed_msg = ', '.join([t.__name__ for t in set(allowed_default_types.values())])
  370. raise PydanticSchemaGenerationError(
  371. f'Unable to infer a default factory for keys of type {values_source_type}.'
  372. f' Only {allowed_msg} are supported, other types require an explicit default factory'
  373. ' ' + instructions
  374. )
  375. return allowed_default_types[values_type]
  376. # Assume Annotated[..., Field(...)]
  377. if typing_objects.is_annotated(values_type_origin):
  378. field_info = next((v for v in get_args(values_source_type) if isinstance(v, FieldInfo)), None)
  379. else:
  380. field_info = None
  381. if field_info and field_info.default_factory:
  382. # Assume the default factory does not take any argument:
  383. default_default_factory = cast(Callable[[], Any], field_info.default_factory)
  384. else:
  385. default_default_factory = infer_default()
  386. return default_default_factory
  387. def validate_str_is_valid_iana_tz(value: Any, /) -> ZoneInfo:
  388. if isinstance(value, ZoneInfo):
  389. return value
  390. try:
  391. return ZoneInfo(value)
  392. except (ZoneInfoNotFoundError, ValueError, TypeError):
  393. raise PydanticCustomError('zoneinfo_str', 'invalid timezone: {value}', {'value': value})
  394. NUMERIC_VALIDATOR_LOOKUP: dict[str, Callable] = {
  395. 'gt': greater_than_validator,
  396. 'ge': greater_than_or_equal_validator,
  397. 'lt': less_than_validator,
  398. 'le': less_than_or_equal_validator,
  399. 'multiple_of': multiple_of_validator,
  400. 'min_length': min_length_validator,
  401. 'max_length': max_length_validator,
  402. 'max_digits': max_digits_validator,
  403. 'decimal_places': decimal_places_validator,
  404. }
  405. IpType = Union[IPv4Address, IPv6Address, IPv4Network, IPv6Network, IPv4Interface, IPv6Interface]
  406. IP_VALIDATOR_LOOKUP: dict[type[IpType], Callable] = {
  407. IPv4Address: ip_v4_address_validator,
  408. IPv6Address: ip_v6_address_validator,
  409. IPv4Network: ip_v4_network_validator,
  410. IPv6Network: ip_v6_network_validator,
  411. IPv4Interface: ip_v4_interface_validator,
  412. IPv6Interface: ip_v6_interface_validator,
  413. }
  414. MAPPING_ORIGIN_MAP: dict[Any, Any] = {
  415. typing.DefaultDict: collections.defaultdict, # noqa: UP006
  416. collections.defaultdict: collections.defaultdict,
  417. typing.OrderedDict: collections.OrderedDict, # noqa: UP006
  418. collections.OrderedDict: collections.OrderedDict,
  419. typing_extensions.OrderedDict: collections.OrderedDict,
  420. typing.Counter: collections.Counter,
  421. collections.Counter: collections.Counter,
  422. # this doesn't handle subclasses of these
  423. typing.Mapping: dict,
  424. typing.MutableMapping: dict,
  425. # parametrized typing.{Mutable}Mapping creates one of these
  426. collections.abc.Mapping: dict,
  427. collections.abc.MutableMapping: dict,
  428. }