__init__.pyi 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import sys
  2. from typing import (
  3. Any,
  4. Callable,
  5. Dict,
  6. Generic,
  7. List,
  8. Mapping,
  9. Optional,
  10. Sequence,
  11. Tuple,
  12. Type,
  13. TypeVar,
  14. Union,
  15. overload,
  16. )
  17. # `import X as X` is required to make these public
  18. from . import converters as converters
  19. from . import exceptions as exceptions
  20. from . import filters as filters
  21. from . import setters as setters
  22. from . import validators as validators
  23. from ._version_info import VersionInfo
  24. __version__: str
  25. __version_info__: VersionInfo
  26. __title__: str
  27. __description__: str
  28. __url__: str
  29. __uri__: str
  30. __author__: str
  31. __email__: str
  32. __license__: str
  33. __copyright__: str
  34. _T = TypeVar("_T")
  35. _C = TypeVar("_C", bound=type)
  36. _EqOrderType = Union[bool, Callable[[Any], Any]]
  37. _ValidatorType = Callable[[Any, Attribute[_T], _T], Any]
  38. _ConverterType = Callable[[Any], Any]
  39. _FilterType = Callable[[Attribute[_T], _T], bool]
  40. _ReprType = Callable[[Any], str]
  41. _ReprArgType = Union[bool, _ReprType]
  42. _OnSetAttrType = Callable[[Any, Attribute[Any], Any], Any]
  43. _OnSetAttrArgType = Union[
  44. _OnSetAttrType, List[_OnSetAttrType], setters._NoOpType
  45. ]
  46. _FieldTransformer = Callable[[type, List[Attribute[Any]]], List[Attribute[Any]]]
  47. # FIXME: in reality, if multiple validators are passed they must be in a list
  48. # or tuple, but those are invariant and so would prevent subtypes of
  49. # _ValidatorType from working when passed in a list or tuple.
  50. _ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]
  51. # _make --
  52. NOTHING: object
  53. # NOTE: Factory lies about its return type to make this possible:
  54. # `x: List[int] # = Factory(list)`
  55. # Work around mypy issue #4554 in the common case by using an overload.
  56. if sys.version_info >= (3, 8):
  57. from typing import Literal
  58. @overload
  59. def Factory(factory: Callable[[], _T]) -> _T: ...
  60. @overload
  61. def Factory(
  62. factory: Callable[[Any], _T],
  63. takes_self: Literal[True],
  64. ) -> _T: ...
  65. @overload
  66. def Factory(
  67. factory: Callable[[], _T],
  68. takes_self: Literal[False],
  69. ) -> _T: ...
  70. else:
  71. @overload
  72. def Factory(factory: Callable[[], _T]) -> _T: ...
  73. @overload
  74. def Factory(
  75. factory: Union[Callable[[Any], _T], Callable[[], _T]],
  76. takes_self: bool = ...,
  77. ) -> _T: ...
  78. # Static type inference support via __dataclass_transform__ implemented as per:
  79. # https://github.com/microsoft/pyright/blob/1.1.135/specs/dataclass_transforms.md
  80. # This annotation must be applied to all overloads of "define" and "attrs"
  81. #
  82. # NOTE: This is a typing construct and does not exist at runtime. Extensions
  83. # wrapping attrs decorators should declare a separate __dataclass_transform__
  84. # signature in the extension module using the specification linked above to
  85. # provide pyright support.
  86. def __dataclass_transform__(
  87. *,
  88. eq_default: bool = True,
  89. order_default: bool = False,
  90. kw_only_default: bool = False,
  91. field_descriptors: Tuple[Union[type, Callable[..., Any]], ...] = (()),
  92. ) -> Callable[[_T], _T]: ...
  93. class Attribute(Generic[_T]):
  94. name: str
  95. default: Optional[_T]
  96. validator: Optional[_ValidatorType[_T]]
  97. repr: _ReprArgType
  98. cmp: _EqOrderType
  99. eq: _EqOrderType
  100. order: _EqOrderType
  101. hash: Optional[bool]
  102. init: bool
  103. converter: Optional[_ConverterType]
  104. metadata: Dict[Any, Any]
  105. type: Optional[Type[_T]]
  106. kw_only: bool
  107. on_setattr: _OnSetAttrType
  108. def evolve(self, **changes: Any) -> "Attribute[Any]": ...
  109. # NOTE: We had several choices for the annotation to use for type arg:
  110. # 1) Type[_T]
  111. # - Pros: Handles simple cases correctly
  112. # - Cons: Might produce less informative errors in the case of conflicting
  113. # TypeVars e.g. `attr.ib(default='bad', type=int)`
  114. # 2) Callable[..., _T]
  115. # - Pros: Better error messages than #1 for conflicting TypeVars
  116. # - Cons: Terrible error messages for validator checks.
  117. # e.g. attr.ib(type=int, validator=validate_str)
  118. # -> error: Cannot infer function type argument
  119. # 3) type (and do all of the work in the mypy plugin)
  120. # - Pros: Simple here, and we could customize the plugin with our own errors.
  121. # - Cons: Would need to write mypy plugin code to handle all the cases.
  122. # We chose option #1.
  123. # `attr` lies about its return type to make the following possible:
  124. # attr() -> Any
  125. # attr(8) -> int
  126. # attr(validator=<some callable>) -> Whatever the callable expects.
  127. # This makes this type of assignments possible:
  128. # x: int = attr(8)
  129. #
  130. # This form catches explicit None or no default but with no other arguments
  131. # returns Any.
  132. @overload
  133. def attrib(
  134. default: None = ...,
  135. validator: None = ...,
  136. repr: _ReprArgType = ...,
  137. cmp: Optional[_EqOrderType] = ...,
  138. hash: Optional[bool] = ...,
  139. init: bool = ...,
  140. metadata: Optional[Mapping[Any, Any]] = ...,
  141. type: None = ...,
  142. converter: None = ...,
  143. factory: None = ...,
  144. kw_only: bool = ...,
  145. eq: Optional[_EqOrderType] = ...,
  146. order: Optional[_EqOrderType] = ...,
  147. on_setattr: Optional[_OnSetAttrArgType] = ...,
  148. ) -> Any: ...
  149. # This form catches an explicit None or no default and infers the type from the
  150. # other arguments.
  151. @overload
  152. def attrib(
  153. default: None = ...,
  154. validator: Optional[_ValidatorArgType[_T]] = ...,
  155. repr: _ReprArgType = ...,
  156. cmp: Optional[_EqOrderType] = ...,
  157. hash: Optional[bool] = ...,
  158. init: bool = ...,
  159. metadata: Optional[Mapping[Any, Any]] = ...,
  160. type: Optional[Type[_T]] = ...,
  161. converter: Optional[_ConverterType] = ...,
  162. factory: Optional[Callable[[], _T]] = ...,
  163. kw_only: bool = ...,
  164. eq: Optional[_EqOrderType] = ...,
  165. order: Optional[_EqOrderType] = ...,
  166. on_setattr: Optional[_OnSetAttrArgType] = ...,
  167. ) -> _T: ...
  168. # This form catches an explicit default argument.
  169. @overload
  170. def attrib(
  171. default: _T,
  172. validator: Optional[_ValidatorArgType[_T]] = ...,
  173. repr: _ReprArgType = ...,
  174. cmp: Optional[_EqOrderType] = ...,
  175. hash: Optional[bool] = ...,
  176. init: bool = ...,
  177. metadata: Optional[Mapping[Any, Any]] = ...,
  178. type: Optional[Type[_T]] = ...,
  179. converter: Optional[_ConverterType] = ...,
  180. factory: Optional[Callable[[], _T]] = ...,
  181. kw_only: bool = ...,
  182. eq: Optional[_EqOrderType] = ...,
  183. order: Optional[_EqOrderType] = ...,
  184. on_setattr: Optional[_OnSetAttrArgType] = ...,
  185. ) -> _T: ...
  186. # This form covers type=non-Type: e.g. forward references (str), Any
  187. @overload
  188. def attrib(
  189. default: Optional[_T] = ...,
  190. validator: Optional[_ValidatorArgType[_T]] = ...,
  191. repr: _ReprArgType = ...,
  192. cmp: Optional[_EqOrderType] = ...,
  193. hash: Optional[bool] = ...,
  194. init: bool = ...,
  195. metadata: Optional[Mapping[Any, Any]] = ...,
  196. type: object = ...,
  197. converter: Optional[_ConverterType] = ...,
  198. factory: Optional[Callable[[], _T]] = ...,
  199. kw_only: bool = ...,
  200. eq: Optional[_EqOrderType] = ...,
  201. order: Optional[_EqOrderType] = ...,
  202. on_setattr: Optional[_OnSetAttrArgType] = ...,
  203. ) -> Any: ...
  204. @overload
  205. def field(
  206. *,
  207. default: None = ...,
  208. validator: None = ...,
  209. repr: _ReprArgType = ...,
  210. hash: Optional[bool] = ...,
  211. init: bool = ...,
  212. metadata: Optional[Mapping[Any, Any]] = ...,
  213. converter: None = ...,
  214. factory: None = ...,
  215. kw_only: bool = ...,
  216. eq: Optional[bool] = ...,
  217. order: Optional[bool] = ...,
  218. on_setattr: Optional[_OnSetAttrArgType] = ...,
  219. ) -> Any: ...
  220. # This form catches an explicit None or no default and infers the type from the
  221. # other arguments.
  222. @overload
  223. def field(
  224. *,
  225. default: None = ...,
  226. validator: Optional[_ValidatorArgType[_T]] = ...,
  227. repr: _ReprArgType = ...,
  228. hash: Optional[bool] = ...,
  229. init: bool = ...,
  230. metadata: Optional[Mapping[Any, Any]] = ...,
  231. converter: Optional[_ConverterType] = ...,
  232. factory: Optional[Callable[[], _T]] = ...,
  233. kw_only: bool = ...,
  234. eq: Optional[_EqOrderType] = ...,
  235. order: Optional[_EqOrderType] = ...,
  236. on_setattr: Optional[_OnSetAttrArgType] = ...,
  237. ) -> _T: ...
  238. # This form catches an explicit default argument.
  239. @overload
  240. def field(
  241. *,
  242. default: _T,
  243. validator: Optional[_ValidatorArgType[_T]] = ...,
  244. repr: _ReprArgType = ...,
  245. hash: Optional[bool] = ...,
  246. init: bool = ...,
  247. metadata: Optional[Mapping[Any, Any]] = ...,
  248. converter: Optional[_ConverterType] = ...,
  249. factory: Optional[Callable[[], _T]] = ...,
  250. kw_only: bool = ...,
  251. eq: Optional[_EqOrderType] = ...,
  252. order: Optional[_EqOrderType] = ...,
  253. on_setattr: Optional[_OnSetAttrArgType] = ...,
  254. ) -> _T: ...
  255. # This form covers type=non-Type: e.g. forward references (str), Any
  256. @overload
  257. def field(
  258. *,
  259. default: Optional[_T] = ...,
  260. validator: Optional[_ValidatorArgType[_T]] = ...,
  261. repr: _ReprArgType = ...,
  262. hash: Optional[bool] = ...,
  263. init: bool = ...,
  264. metadata: Optional[Mapping[Any, Any]] = ...,
  265. converter: Optional[_ConverterType] = ...,
  266. factory: Optional[Callable[[], _T]] = ...,
  267. kw_only: bool = ...,
  268. eq: Optional[_EqOrderType] = ...,
  269. order: Optional[_EqOrderType] = ...,
  270. on_setattr: Optional[_OnSetAttrArgType] = ...,
  271. ) -> Any: ...
  272. @overload
  273. @__dataclass_transform__(order_default=True, field_descriptors=(attrib, field))
  274. def attrs(
  275. maybe_cls: _C,
  276. these: Optional[Dict[str, Any]] = ...,
  277. repr_ns: Optional[str] = ...,
  278. repr: bool = ...,
  279. cmp: Optional[_EqOrderType] = ...,
  280. hash: Optional[bool] = ...,
  281. init: bool = ...,
  282. slots: bool = ...,
  283. frozen: bool = ...,
  284. weakref_slot: bool = ...,
  285. str: bool = ...,
  286. auto_attribs: bool = ...,
  287. kw_only: bool = ...,
  288. cache_hash: bool = ...,
  289. auto_exc: bool = ...,
  290. eq: Optional[_EqOrderType] = ...,
  291. order: Optional[_EqOrderType] = ...,
  292. auto_detect: bool = ...,
  293. collect_by_mro: bool = ...,
  294. getstate_setstate: Optional[bool] = ...,
  295. on_setattr: Optional[_OnSetAttrArgType] = ...,
  296. field_transformer: Optional[_FieldTransformer] = ...,
  297. ) -> _C: ...
  298. @overload
  299. @__dataclass_transform__(order_default=True, field_descriptors=(attrib, field))
  300. def attrs(
  301. maybe_cls: None = ...,
  302. these: Optional[Dict[str, Any]] = ...,
  303. repr_ns: Optional[str] = ...,
  304. repr: bool = ...,
  305. cmp: Optional[_EqOrderType] = ...,
  306. hash: Optional[bool] = ...,
  307. init: bool = ...,
  308. slots: bool = ...,
  309. frozen: bool = ...,
  310. weakref_slot: bool = ...,
  311. str: bool = ...,
  312. auto_attribs: bool = ...,
  313. kw_only: bool = ...,
  314. cache_hash: bool = ...,
  315. auto_exc: bool = ...,
  316. eq: Optional[_EqOrderType] = ...,
  317. order: Optional[_EqOrderType] = ...,
  318. auto_detect: bool = ...,
  319. collect_by_mro: bool = ...,
  320. getstate_setstate: Optional[bool] = ...,
  321. on_setattr: Optional[_OnSetAttrArgType] = ...,
  322. field_transformer: Optional[_FieldTransformer] = ...,
  323. ) -> Callable[[_C], _C]: ...
  324. @overload
  325. @__dataclass_transform__(field_descriptors=(attrib, field))
  326. def define(
  327. maybe_cls: _C,
  328. *,
  329. these: Optional[Dict[str, Any]] = ...,
  330. repr: bool = ...,
  331. hash: Optional[bool] = ...,
  332. init: bool = ...,
  333. slots: bool = ...,
  334. frozen: bool = ...,
  335. weakref_slot: bool = ...,
  336. str: bool = ...,
  337. auto_attribs: bool = ...,
  338. kw_only: bool = ...,
  339. cache_hash: bool = ...,
  340. auto_exc: bool = ...,
  341. eq: Optional[bool] = ...,
  342. order: Optional[bool] = ...,
  343. auto_detect: bool = ...,
  344. getstate_setstate: Optional[bool] = ...,
  345. on_setattr: Optional[_OnSetAttrArgType] = ...,
  346. field_transformer: Optional[_FieldTransformer] = ...,
  347. ) -> _C: ...
  348. @overload
  349. @__dataclass_transform__(field_descriptors=(attrib, field))
  350. def define(
  351. maybe_cls: None = ...,
  352. *,
  353. these: Optional[Dict[str, Any]] = ...,
  354. repr: bool = ...,
  355. hash: Optional[bool] = ...,
  356. init: bool = ...,
  357. slots: bool = ...,
  358. frozen: bool = ...,
  359. weakref_slot: bool = ...,
  360. str: bool = ...,
  361. auto_attribs: bool = ...,
  362. kw_only: bool = ...,
  363. cache_hash: bool = ...,
  364. auto_exc: bool = ...,
  365. eq: Optional[bool] = ...,
  366. order: Optional[bool] = ...,
  367. auto_detect: bool = ...,
  368. getstate_setstate: Optional[bool] = ...,
  369. on_setattr: Optional[_OnSetAttrArgType] = ...,
  370. field_transformer: Optional[_FieldTransformer] = ...,
  371. ) -> Callable[[_C], _C]: ...
  372. mutable = define
  373. frozen = define # they differ only in their defaults
  374. # TODO: add support for returning NamedTuple from the mypy plugin
  375. class _Fields(Tuple[Attribute[Any], ...]):
  376. def __getattr__(self, name: str) -> Attribute[Any]: ...
  377. def fields(cls: type) -> _Fields: ...
  378. def fields_dict(cls: type) -> Dict[str, Attribute[Any]]: ...
  379. def validate(inst: Any) -> None: ...
  380. def resolve_types(
  381. cls: _C,
  382. globalns: Optional[Dict[str, Any]] = ...,
  383. localns: Optional[Dict[str, Any]] = ...,
  384. attribs: Optional[List[Attribute[Any]]] = ...,
  385. ) -> _C: ...
  386. # TODO: add support for returning a proper attrs class from the mypy plugin
  387. # we use Any instead of _CountingAttr so that e.g. `make_class('Foo',
  388. # [attr.ib()])` is valid
  389. def make_class(
  390. name: str,
  391. attrs: Union[List[str], Tuple[str, ...], Dict[str, Any]],
  392. bases: Tuple[type, ...] = ...,
  393. repr_ns: Optional[str] = ...,
  394. repr: bool = ...,
  395. cmp: Optional[_EqOrderType] = ...,
  396. hash: Optional[bool] = ...,
  397. init: bool = ...,
  398. slots: bool = ...,
  399. frozen: bool = ...,
  400. weakref_slot: bool = ...,
  401. str: bool = ...,
  402. auto_attribs: bool = ...,
  403. kw_only: bool = ...,
  404. cache_hash: bool = ...,
  405. auto_exc: bool = ...,
  406. eq: Optional[_EqOrderType] = ...,
  407. order: Optional[_EqOrderType] = ...,
  408. collect_by_mro: bool = ...,
  409. on_setattr: Optional[_OnSetAttrArgType] = ...,
  410. field_transformer: Optional[_FieldTransformer] = ...,
  411. ) -> type: ...
  412. # _funcs --
  413. # TODO: add support for returning TypedDict from the mypy plugin
  414. # FIXME: asdict/astuple do not honor their factory args. Waiting on one of
  415. # these:
  416. # https://github.com/python/mypy/issues/4236
  417. # https://github.com/python/typing/issues/253
  418. def asdict(
  419. inst: Any,
  420. recurse: bool = ...,
  421. filter: Optional[_FilterType[Any]] = ...,
  422. dict_factory: Type[Mapping[Any, Any]] = ...,
  423. retain_collection_types: bool = ...,
  424. value_serializer: Optional[Callable[[type, Attribute[Any], Any], Any]] = ...,
  425. ) -> Dict[str, Any]: ...
  426. # TODO: add support for returning NamedTuple from the mypy plugin
  427. def astuple(
  428. inst: Any,
  429. recurse: bool = ...,
  430. filter: Optional[_FilterType[Any]] = ...,
  431. tuple_factory: Type[Sequence[Any]] = ...,
  432. retain_collection_types: bool = ...,
  433. ) -> Tuple[Any, ...]: ...
  434. def has(cls: type) -> bool: ...
  435. def assoc(inst: _T, **changes: Any) -> _T: ...
  436. def evolve(inst: _T, **changes: Any) -> _T: ...
  437. # _config --
  438. def set_run_validators(run: bool) -> None: ...
  439. def get_run_validators() -> bool: ...
  440. # aliases --
  441. s = attributes = attrs
  442. ib = attr = attrib
  443. dataclass = attrs # Technically, partial(attrs, auto_attribs=True) ;)