You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.pyi 8.1KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. from typing import (
  2. Any,
  3. Callable,
  4. Dict,
  5. Generic,
  6. List,
  7. Optional,
  8. Sequence,
  9. Mapping,
  10. Tuple,
  11. Type,
  12. TypeVar,
  13. Union,
  14. overload,
  15. )
  16. # `import X as X` is required to make these public
  17. from . import exceptions as exceptions
  18. from . import filters as filters
  19. from . import converters as converters
  20. from . import validators as validators
  21. from ._version_info import VersionInfo
  22. __version__: str
  23. __version_info__: VersionInfo
  24. __title__: str
  25. __description__: str
  26. __url__: str
  27. __uri__: str
  28. __author__: str
  29. __email__: str
  30. __license__: str
  31. __copyright__: str
  32. _T = TypeVar("_T")
  33. _C = TypeVar("_C", bound=type)
  34. _ValidatorType = Callable[[Any, Attribute[_T], _T], Any]
  35. _ConverterType = Callable[[Any], _T]
  36. _FilterType = Callable[[Attribute[_T], _T], bool]
  37. _ReprType = Callable[[Any], str]
  38. _ReprArgType = Union[bool, _ReprType]
  39. # FIXME: in reality, if multiple validators are passed they must be in a list or tuple,
  40. # but those are invariant and so would prevent subtypes of _ValidatorType from working
  41. # when passed in a list or tuple.
  42. _ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]
  43. # _make --
  44. NOTHING: object
  45. # NOTE: Factory lies about its return type to make this possible: `x: List[int] = Factory(list)`
  46. # Work around mypy issue #4554 in the common case by using an overload.
  47. @overload
  48. def Factory(factory: Callable[[], _T]) -> _T: ...
  49. @overload
  50. def Factory(
  51. factory: Union[Callable[[Any], _T], Callable[[], _T]],
  52. takes_self: bool = ...,
  53. ) -> _T: ...
  54. class Attribute(Generic[_T]):
  55. name: str
  56. default: Optional[_T]
  57. validator: Optional[_ValidatorType[_T]]
  58. repr: _ReprArgType
  59. cmp: bool
  60. eq: bool
  61. order: bool
  62. hash: Optional[bool]
  63. init: bool
  64. converter: Optional[_ConverterType[_T]]
  65. metadata: Dict[Any, Any]
  66. type: Optional[Type[_T]]
  67. kw_only: bool
  68. # NOTE: We had several choices for the annotation to use for type arg:
  69. # 1) Type[_T]
  70. # - Pros: Handles simple cases correctly
  71. # - Cons: Might produce less informative errors in the case of conflicting TypeVars
  72. # e.g. `attr.ib(default='bad', type=int)`
  73. # 2) Callable[..., _T]
  74. # - Pros: Better error messages than #1 for conflicting TypeVars
  75. # - Cons: Terrible error messages for validator checks.
  76. # e.g. attr.ib(type=int, validator=validate_str)
  77. # -> error: Cannot infer function type argument
  78. # 3) type (and do all of the work in the mypy plugin)
  79. # - Pros: Simple here, and we could customize the plugin with our own errors.
  80. # - Cons: Would need to write mypy plugin code to handle all the cases.
  81. # We chose option #1.
  82. # `attr` lies about its return type to make the following possible:
  83. # attr() -> Any
  84. # attr(8) -> int
  85. # attr(validator=<some callable>) -> Whatever the callable expects.
  86. # This makes this type of assignments possible:
  87. # x: int = attr(8)
  88. #
  89. # This form catches explicit None or no default but with no other arguments returns Any.
  90. @overload
  91. def attrib(
  92. default: None = ...,
  93. validator: None = ...,
  94. repr: _ReprArgType = ...,
  95. cmp: Optional[bool] = ...,
  96. hash: Optional[bool] = ...,
  97. init: bool = ...,
  98. metadata: Optional[Mapping[Any, Any]] = ...,
  99. type: None = ...,
  100. converter: None = ...,
  101. factory: None = ...,
  102. kw_only: bool = ...,
  103. eq: Optional[bool] = ...,
  104. order: Optional[bool] = ...,
  105. ) -> Any: ...
  106. # This form catches an explicit None or no default and infers the type from the other arguments.
  107. @overload
  108. def attrib(
  109. default: None = ...,
  110. validator: Optional[_ValidatorArgType[_T]] = ...,
  111. repr: _ReprArgType = ...,
  112. cmp: Optional[bool] = ...,
  113. hash: Optional[bool] = ...,
  114. init: bool = ...,
  115. metadata: Optional[Mapping[Any, Any]] = ...,
  116. type: Optional[Type[_T]] = ...,
  117. converter: Optional[_ConverterType[_T]] = ...,
  118. factory: Optional[Callable[[], _T]] = ...,
  119. kw_only: bool = ...,
  120. eq: Optional[bool] = ...,
  121. order: Optional[bool] = ...,
  122. ) -> _T: ...
  123. # This form catches an explicit default argument.
  124. @overload
  125. def attrib(
  126. default: _T,
  127. validator: Optional[_ValidatorArgType[_T]] = ...,
  128. repr: _ReprArgType = ...,
  129. cmp: Optional[bool] = ...,
  130. hash: Optional[bool] = ...,
  131. init: bool = ...,
  132. metadata: Optional[Mapping[Any, Any]] = ...,
  133. type: Optional[Type[_T]] = ...,
  134. converter: Optional[_ConverterType[_T]] = ...,
  135. factory: Optional[Callable[[], _T]] = ...,
  136. kw_only: bool = ...,
  137. eq: Optional[bool] = ...,
  138. order: Optional[bool] = ...,
  139. ) -> _T: ...
  140. # This form covers type=non-Type: e.g. forward references (str), Any
  141. @overload
  142. def attrib(
  143. default: Optional[_T] = ...,
  144. validator: Optional[_ValidatorArgType[_T]] = ...,
  145. repr: _ReprArgType = ...,
  146. cmp: Optional[bool] = ...,
  147. hash: Optional[bool] = ...,
  148. init: bool = ...,
  149. metadata: Optional[Mapping[Any, Any]] = ...,
  150. type: object = ...,
  151. converter: Optional[_ConverterType[_T]] = ...,
  152. factory: Optional[Callable[[], _T]] = ...,
  153. kw_only: bool = ...,
  154. eq: Optional[bool] = ...,
  155. order: Optional[bool] = ...,
  156. ) -> Any: ...
  157. @overload
  158. def attrs(
  159. maybe_cls: _C,
  160. these: Optional[Dict[str, Any]] = ...,
  161. repr_ns: Optional[str] = ...,
  162. repr: bool = ...,
  163. cmp: Optional[bool] = ...,
  164. hash: Optional[bool] = ...,
  165. init: bool = ...,
  166. slots: bool = ...,
  167. frozen: bool = ...,
  168. weakref_slot: bool = ...,
  169. str: bool = ...,
  170. auto_attribs: bool = ...,
  171. kw_only: bool = ...,
  172. cache_hash: bool = ...,
  173. auto_exc: bool = ...,
  174. eq: Optional[bool] = ...,
  175. order: Optional[bool] = ...,
  176. ) -> _C: ...
  177. @overload
  178. def attrs(
  179. maybe_cls: None = ...,
  180. these: Optional[Dict[str, Any]] = ...,
  181. repr_ns: Optional[str] = ...,
  182. repr: bool = ...,
  183. cmp: Optional[bool] = ...,
  184. hash: Optional[bool] = ...,
  185. init: bool = ...,
  186. slots: bool = ...,
  187. frozen: bool = ...,
  188. weakref_slot: bool = ...,
  189. str: bool = ...,
  190. auto_attribs: bool = ...,
  191. kw_only: bool = ...,
  192. cache_hash: bool = ...,
  193. auto_exc: bool = ...,
  194. eq: Optional[bool] = ...,
  195. order: Optional[bool] = ...,
  196. ) -> Callable[[_C], _C]: ...
  197. # TODO: add support for returning NamedTuple from the mypy plugin
  198. class _Fields(Tuple[Attribute[Any], ...]):
  199. def __getattr__(self, name: str) -> Attribute[Any]: ...
  200. def fields(cls: type) -> _Fields: ...
  201. def fields_dict(cls: type) -> Dict[str, Attribute[Any]]: ...
  202. def validate(inst: Any) -> None: ...
  203. # TODO: add support for returning a proper attrs class from the mypy plugin
  204. # we use Any instead of _CountingAttr so that e.g. `make_class('Foo', [attr.ib()])` is valid
  205. def make_class(
  206. name: str,
  207. attrs: Union[List[str], Tuple[str, ...], Dict[str, Any]],
  208. bases: Tuple[type, ...] = ...,
  209. repr_ns: Optional[str] = ...,
  210. repr: bool = ...,
  211. cmp: Optional[bool] = ...,
  212. hash: Optional[bool] = ...,
  213. init: bool = ...,
  214. slots: bool = ...,
  215. frozen: bool = ...,
  216. weakref_slot: bool = ...,
  217. str: bool = ...,
  218. auto_attribs: bool = ...,
  219. kw_only: bool = ...,
  220. cache_hash: bool = ...,
  221. auto_exc: bool = ...,
  222. eq: Optional[bool] = ...,
  223. order: Optional[bool] = ...,
  224. ) -> type: ...
  225. # _funcs --
  226. # TODO: add support for returning TypedDict from the mypy plugin
  227. # FIXME: asdict/astuple do not honor their factory args. waiting on one of these:
  228. # https://github.com/python/mypy/issues/4236
  229. # https://github.com/python/typing/issues/253
  230. def asdict(
  231. inst: Any,
  232. recurse: bool = ...,
  233. filter: Optional[_FilterType[Any]] = ...,
  234. dict_factory: Type[Mapping[Any, Any]] = ...,
  235. retain_collection_types: bool = ...,
  236. ) -> Dict[str, Any]: ...
  237. # TODO: add support for returning NamedTuple from the mypy plugin
  238. def astuple(
  239. inst: Any,
  240. recurse: bool = ...,
  241. filter: Optional[_FilterType[Any]] = ...,
  242. tuple_factory: Type[Sequence[Any]] = ...,
  243. retain_collection_types: bool = ...,
  244. ) -> Tuple[Any, ...]: ...
  245. def has(cls: type) -> bool: ...
  246. def assoc(inst: _T, **changes: Any) -> _T: ...
  247. def evolve(inst: _T, **changes: Any) -> _T: ...
  248. # _config --
  249. def set_run_validators(run: bool) -> None: ...
  250. def get_run_validators() -> bool: ...
  251. # aliases --
  252. s = attributes = attrs
  253. ib = attr = attrib
  254. dataclass = attrs # Technically, partial(attrs, auto_attribs=True) ;)