validators.pyi 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from types import UnionType
  2. from typing import (
  3. Any,
  4. AnyStr,
  5. Callable,
  6. Container,
  7. ContextManager,
  8. Iterable,
  9. Mapping,
  10. Match,
  11. Pattern,
  12. TypeVar,
  13. overload,
  14. )
  15. from attrs import _ValidatorType
  16. from attrs import _ValidatorArgType
  17. _T = TypeVar("_T")
  18. _T1 = TypeVar("_T1")
  19. _T2 = TypeVar("_T2")
  20. _T3 = TypeVar("_T3")
  21. _T4 = TypeVar("_T4")
  22. _T5 = TypeVar("_T5")
  23. _T6 = TypeVar("_T6")
  24. _I = TypeVar("_I", bound=Iterable)
  25. _K = TypeVar("_K")
  26. _V = TypeVar("_V")
  27. _M = TypeVar("_M", bound=Mapping)
  28. def set_disabled(run: bool) -> None: ...
  29. def get_disabled() -> bool: ...
  30. def disabled() -> ContextManager[None]: ...
  31. # To be more precise on instance_of use some overloads.
  32. # If there are more than 3 items in the tuple then we fall back to Any
  33. @overload
  34. def instance_of(type: type[_T]) -> _ValidatorType[_T]: ...
  35. @overload
  36. def instance_of(type: tuple[type[_T]]) -> _ValidatorType[_T]: ...
  37. @overload
  38. def instance_of(
  39. type: tuple[type[_T1], type[_T2]],
  40. ) -> _ValidatorType[_T1 | _T2]: ...
  41. @overload
  42. def instance_of(
  43. type: tuple[type[_T1], type[_T2], type[_T3]],
  44. ) -> _ValidatorType[_T1 | _T2 | _T3]: ...
  45. @overload
  46. def instance_of(type: tuple[type, ...]) -> _ValidatorType[Any]: ...
  47. @overload
  48. def instance_of(type: UnionType) -> _ValidatorType[Any]: ...
  49. def optional(
  50. validator: (
  51. _ValidatorType[_T]
  52. | list[_ValidatorType[_T]]
  53. | tuple[_ValidatorType[_T], ...]
  54. ),
  55. ) -> _ValidatorType[_T | None]: ...
  56. def in_(options: Container[_T]) -> _ValidatorType[_T]: ...
  57. def and_(*validators: _ValidatorType[_T]) -> _ValidatorType[_T]: ...
  58. def matches_re(
  59. regex: Pattern[AnyStr] | AnyStr,
  60. flags: int = ...,
  61. func: Callable[[AnyStr, AnyStr, int], Match[AnyStr] | None] | None = ...,
  62. ) -> _ValidatorType[AnyStr]: ...
  63. def deep_iterable(
  64. member_validator: _ValidatorArgType[_T],
  65. iterable_validator: _ValidatorArgType[_I] | None = ...,
  66. ) -> _ValidatorType[_I]: ...
  67. @overload
  68. def deep_mapping(
  69. key_validator: _ValidatorArgType[_K],
  70. value_validator: _ValidatorArgType[_V] | None = ...,
  71. mapping_validator: _ValidatorArgType[_M] | None = ...,
  72. ) -> _ValidatorType[_M]: ...
  73. @overload
  74. def deep_mapping(
  75. key_validator: _ValidatorArgType[_K] | None = ...,
  76. value_validator: _ValidatorArgType[_V] = ...,
  77. mapping_validator: _ValidatorArgType[_M] | None = ...,
  78. ) -> _ValidatorType[_M]: ...
  79. def is_callable() -> _ValidatorType[_T]: ...
  80. def lt(val: _T) -> _ValidatorType[_T]: ...
  81. def le(val: _T) -> _ValidatorType[_T]: ...
  82. def ge(val: _T) -> _ValidatorType[_T]: ...
  83. def gt(val: _T) -> _ValidatorType[_T]: ...
  84. def max_len(length: int) -> _ValidatorType[_T]: ...
  85. def min_len(length: int) -> _ValidatorType[_T]: ...
  86. def not_(
  87. validator: _ValidatorType[_T],
  88. *,
  89. msg: str | None = None,
  90. exc_types: type[Exception] | Iterable[type[Exception]] = ...,
  91. ) -> _ValidatorType[_T]: ...
  92. @overload
  93. def or_(
  94. __v1: _ValidatorType[_T1],
  95. __v2: _ValidatorType[_T2],
  96. ) -> _ValidatorType[_T1 | _T2]: ...
  97. @overload
  98. def or_(
  99. __v1: _ValidatorType[_T1],
  100. __v2: _ValidatorType[_T2],
  101. __v3: _ValidatorType[_T3],
  102. ) -> _ValidatorType[_T1 | _T2 | _T3]: ...
  103. @overload
  104. def or_(
  105. __v1: _ValidatorType[_T1],
  106. __v2: _ValidatorType[_T2],
  107. __v3: _ValidatorType[_T3],
  108. __v4: _ValidatorType[_T4],
  109. ) -> _ValidatorType[_T1 | _T2 | _T3 | _T4]: ...
  110. @overload
  111. def or_(
  112. __v1: _ValidatorType[_T1],
  113. __v2: _ValidatorType[_T2],
  114. __v3: _ValidatorType[_T3],
  115. __v4: _ValidatorType[_T4],
  116. __v5: _ValidatorType[_T5],
  117. ) -> _ValidatorType[_T1 | _T2 | _T3 | _T4 | _T5]: ...
  118. @overload
  119. def or_(
  120. __v1: _ValidatorType[_T1],
  121. __v2: _ValidatorType[_T2],
  122. __v3: _ValidatorType[_T3],
  123. __v4: _ValidatorType[_T4],
  124. __v5: _ValidatorType[_T5],
  125. __v6: _ValidatorType[_T6],
  126. ) -> _ValidatorType[_T1 | _T2 | _T3 | _T4 | _T5 | _T6]: ...
  127. @overload
  128. def or_(
  129. __v1: _ValidatorType[Any],
  130. __v2: _ValidatorType[Any],
  131. __v3: _ValidatorType[Any],
  132. __v4: _ValidatorType[Any],
  133. __v5: _ValidatorType[Any],
  134. __v6: _ValidatorType[Any],
  135. *validators: _ValidatorType[Any],
  136. ) -> _ValidatorType[Any]: ...