__init__.pyi 15 KB

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