__init__.pyi 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import enum
  2. import sys
  3. from typing import (
  4. Any,
  5. Callable,
  6. Generic,
  7. Literal,
  8. Mapping,
  9. Protocol,
  10. Sequence,
  11. TypeVar,
  12. overload,
  13. )
  14. # `import X as X` is required to make these public
  15. from . import converters as converters
  16. from . import exceptions as exceptions
  17. from . import filters as filters
  18. from . import setters as setters
  19. from . import validators as validators
  20. from ._cmp import cmp_using as cmp_using
  21. from ._typing_compat import AttrsInstance_
  22. from ._version_info import VersionInfo
  23. from attrs import (
  24. define as define,
  25. field as field,
  26. mutable as mutable,
  27. frozen as frozen,
  28. _EqOrderType,
  29. _ValidatorType,
  30. _ConverterType,
  31. _ReprArgType,
  32. _OnSetAttrType,
  33. _OnSetAttrArgType,
  34. _FieldTransformer,
  35. _ValidatorArgType,
  36. )
  37. if sys.version_info >= (3, 10):
  38. from typing import TypeGuard, TypeAlias
  39. else:
  40. from typing_extensions import TypeGuard, TypeAlias
  41. if sys.version_info >= (3, 11):
  42. from typing import dataclass_transform
  43. else:
  44. from typing_extensions import dataclass_transform
  45. __version__: str
  46. __version_info__: VersionInfo
  47. __title__: str
  48. __description__: str
  49. __url__: str
  50. __uri__: str
  51. __author__: str
  52. __email__: str
  53. __license__: str
  54. __copyright__: str
  55. _T = TypeVar("_T")
  56. _C = TypeVar("_C", bound=type)
  57. _FilterType = Callable[["Attribute[_T]", _T], bool]
  58. # We subclass this here to keep the protocol's qualified name clean.
  59. class AttrsInstance(AttrsInstance_, Protocol):
  60. pass
  61. _A = TypeVar("_A", bound=type[AttrsInstance])
  62. class _Nothing(enum.Enum):
  63. NOTHING = enum.auto()
  64. NOTHING = _Nothing.NOTHING
  65. NothingType: TypeAlias = Literal[_Nothing.NOTHING]
  66. # NOTE: Factory lies about its return type to make this possible:
  67. # `x: List[int] # = Factory(list)`
  68. # Work around mypy issue #4554 in the common case by using an overload.
  69. @overload
  70. def Factory(factory: Callable[[], _T]) -> _T: ...
  71. @overload
  72. def Factory(
  73. factory: Callable[[Any], _T],
  74. takes_self: Literal[True],
  75. ) -> _T: ...
  76. @overload
  77. def Factory(
  78. factory: Callable[[], _T],
  79. takes_self: Literal[False],
  80. ) -> _T: ...
  81. In = TypeVar("In")
  82. Out = TypeVar("Out")
  83. class Converter(Generic[In, Out]):
  84. @overload
  85. def __init__(self, converter: Callable[[In], Out]) -> None: ...
  86. @overload
  87. def __init__(
  88. self,
  89. converter: Callable[[In, AttrsInstance, Attribute], Out],
  90. *,
  91. takes_self: Literal[True],
  92. takes_field: Literal[True],
  93. ) -> None: ...
  94. @overload
  95. def __init__(
  96. self,
  97. converter: Callable[[In, Attribute], Out],
  98. *,
  99. takes_field: Literal[True],
  100. ) -> None: ...
  101. @overload
  102. def __init__(
  103. self,
  104. converter: Callable[[In, AttrsInstance], Out],
  105. *,
  106. takes_self: Literal[True],
  107. ) -> None: ...
  108. class Attribute(Generic[_T]):
  109. name: str
  110. default: _T | None
  111. validator: _ValidatorType[_T] | None
  112. repr: _ReprArgType
  113. cmp: _EqOrderType
  114. eq: _EqOrderType
  115. order: _EqOrderType
  116. hash: bool | None
  117. init: bool
  118. converter: Converter | None
  119. metadata: dict[Any, Any]
  120. type: type[_T] | None
  121. kw_only: bool
  122. on_setattr: _OnSetAttrType
  123. alias: str | None
  124. def evolve(self, **changes: Any) -> "Attribute[Any]": ...
  125. # NOTE: We had several choices for the annotation to use for type arg:
  126. # 1) Type[_T]
  127. # - Pros: Handles simple cases correctly
  128. # - Cons: Might produce less informative errors in the case of conflicting
  129. # TypeVars e.g. `attr.ib(default='bad', type=int)`
  130. # 2) Callable[..., _T]
  131. # - Pros: Better error messages than #1 for conflicting TypeVars
  132. # - Cons: Terrible error messages for validator checks.
  133. # e.g. attr.ib(type=int, validator=validate_str)
  134. # -> error: Cannot infer function type argument
  135. # 3) type (and do all of the work in the mypy plugin)
  136. # - Pros: Simple here, and we could customize the plugin with our own errors.
  137. # - Cons: Would need to write mypy plugin code to handle all the cases.
  138. # We chose option #1.
  139. # `attr` lies about its return type to make the following possible:
  140. # attr() -> Any
  141. # attr(8) -> int
  142. # attr(validator=<some callable>) -> Whatever the callable expects.
  143. # This makes this type of assignments possible:
  144. # x: int = attr(8)
  145. #
  146. # This form catches explicit None or no default but with no other arguments
  147. # returns Any.
  148. @overload
  149. def attrib(
  150. default: None = ...,
  151. validator: None = ...,
  152. repr: _ReprArgType = ...,
  153. cmp: _EqOrderType | None = ...,
  154. hash: bool | None = ...,
  155. init: bool = ...,
  156. metadata: Mapping[Any, Any] | None = ...,
  157. type: None = ...,
  158. converter: None = ...,
  159. factory: None = ...,
  160. kw_only: bool | None = ...,
  161. eq: _EqOrderType | None = ...,
  162. order: _EqOrderType | None = ...,
  163. on_setattr: _OnSetAttrArgType | None = ...,
  164. alias: str | None = ...,
  165. ) -> Any: ...
  166. # This form catches an explicit None or no default and infers the type from the
  167. # other arguments.
  168. @overload
  169. def attrib(
  170. default: None = ...,
  171. validator: _ValidatorArgType[_T] | None = ...,
  172. repr: _ReprArgType = ...,
  173. cmp: _EqOrderType | None = ...,
  174. hash: bool | None = ...,
  175. init: bool = ...,
  176. metadata: Mapping[Any, Any] | None = ...,
  177. type: type[_T] | None = ...,
  178. converter: _ConverterType
  179. | list[_ConverterType]
  180. | tuple[_ConverterType]
  181. | None = ...,
  182. factory: Callable[[], _T] | None = ...,
  183. kw_only: bool | None = ...,
  184. eq: _EqOrderType | None = ...,
  185. order: _EqOrderType | None = ...,
  186. on_setattr: _OnSetAttrArgType | None = ...,
  187. alias: str | None = ...,
  188. ) -> _T: ...
  189. # This form catches an explicit default argument.
  190. @overload
  191. def attrib(
  192. default: _T,
  193. validator: _ValidatorArgType[_T] | None = ...,
  194. repr: _ReprArgType = ...,
  195. cmp: _EqOrderType | None = ...,
  196. hash: bool | None = ...,
  197. init: bool = ...,
  198. metadata: Mapping[Any, Any] | None = ...,
  199. type: type[_T] | None = ...,
  200. converter: _ConverterType
  201. | list[_ConverterType]
  202. | tuple[_ConverterType]
  203. | None = ...,
  204. factory: Callable[[], _T] | None = ...,
  205. kw_only: bool | None = ...,
  206. eq: _EqOrderType | None = ...,
  207. order: _EqOrderType | None = ...,
  208. on_setattr: _OnSetAttrArgType | None = ...,
  209. alias: str | None = ...,
  210. ) -> _T: ...
  211. # This form covers type=non-Type: e.g. forward references (str), Any
  212. @overload
  213. def attrib(
  214. default: _T | None = ...,
  215. validator: _ValidatorArgType[_T] | None = ...,
  216. repr: _ReprArgType = ...,
  217. cmp: _EqOrderType | None = ...,
  218. hash: bool | None = ...,
  219. init: bool = ...,
  220. metadata: Mapping[Any, Any] | None = ...,
  221. type: object = ...,
  222. converter: _ConverterType
  223. | list[_ConverterType]
  224. | tuple[_ConverterType]
  225. | None = ...,
  226. factory: Callable[[], _T] | None = ...,
  227. kw_only: bool | None = ...,
  228. eq: _EqOrderType | None = ...,
  229. order: _EqOrderType | None = ...,
  230. on_setattr: _OnSetAttrArgType | None = ...,
  231. alias: str | None = ...,
  232. ) -> Any: ...
  233. @overload
  234. @dataclass_transform(order_default=True, field_specifiers=(attrib, field))
  235. def attrs(
  236. maybe_cls: _C,
  237. these: dict[str, Any] | None = ...,
  238. repr_ns: str | None = ...,
  239. repr: bool = ...,
  240. cmp: _EqOrderType | None = ...,
  241. hash: bool | None = ...,
  242. init: bool = ...,
  243. slots: bool = ...,
  244. frozen: bool = ...,
  245. weakref_slot: bool = ...,
  246. str: bool = ...,
  247. auto_attribs: bool = ...,
  248. kw_only: bool = ...,
  249. cache_hash: bool = ...,
  250. auto_exc: bool = ...,
  251. eq: _EqOrderType | None = ...,
  252. order: _EqOrderType | None = ...,
  253. auto_detect: bool = ...,
  254. collect_by_mro: bool = ...,
  255. getstate_setstate: bool | None = ...,
  256. on_setattr: _OnSetAttrArgType | None = ...,
  257. field_transformer: _FieldTransformer | None = ...,
  258. match_args: bool = ...,
  259. unsafe_hash: bool | None = ...,
  260. ) -> _C: ...
  261. @overload
  262. @dataclass_transform(order_default=True, field_specifiers=(attrib, field))
  263. def attrs(
  264. maybe_cls: None = ...,
  265. these: dict[str, Any] | None = ...,
  266. repr_ns: str | None = ...,
  267. repr: bool = ...,
  268. cmp: _EqOrderType | None = ...,
  269. hash: bool | None = ...,
  270. init: bool = ...,
  271. slots: bool = ...,
  272. frozen: bool = ...,
  273. weakref_slot: bool = ...,
  274. str: bool = ...,
  275. auto_attribs: bool = ...,
  276. kw_only: bool = ...,
  277. cache_hash: bool = ...,
  278. auto_exc: bool = ...,
  279. eq: _EqOrderType | None = ...,
  280. order: _EqOrderType | None = ...,
  281. auto_detect: bool = ...,
  282. collect_by_mro: bool = ...,
  283. getstate_setstate: bool | None = ...,
  284. on_setattr: _OnSetAttrArgType | None = ...,
  285. field_transformer: _FieldTransformer | None = ...,
  286. match_args: bool = ...,
  287. unsafe_hash: bool | None = ...,
  288. ) -> Callable[[_C], _C]: ...
  289. def fields(cls: type[AttrsInstance] | AttrsInstance) -> Any: ...
  290. def fields_dict(cls: type[AttrsInstance]) -> dict[str, Attribute[Any]]: ...
  291. def validate(inst: AttrsInstance) -> None: ...
  292. def resolve_types(
  293. cls: _A,
  294. globalns: dict[str, Any] | None = ...,
  295. localns: dict[str, Any] | None = ...,
  296. attribs: list[Attribute[Any]] | None = ...,
  297. include_extras: bool = ...,
  298. ) -> _A: ...
  299. # TODO: add support for returning a proper attrs class from the mypy plugin
  300. # we use Any instead of _CountingAttr so that e.g. `make_class('Foo',
  301. # [attr.ib()])` is valid
  302. def make_class(
  303. name: str,
  304. attrs: list[str] | tuple[str, ...] | dict[str, Any],
  305. bases: tuple[type, ...] = ...,
  306. class_body: dict[str, Any] | None = ...,
  307. repr_ns: str | None = ...,
  308. repr: bool = ...,
  309. cmp: _EqOrderType | None = ...,
  310. hash: bool | None = ...,
  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: _EqOrderType | None = ...,
  321. order: _EqOrderType | None = ...,
  322. collect_by_mro: bool = ...,
  323. on_setattr: _OnSetAttrArgType | None = ...,
  324. field_transformer: _FieldTransformer | None = ...,
  325. ) -> type: ...
  326. # _funcs --
  327. # TODO: add support for returning TypedDict from the mypy plugin
  328. # FIXME: asdict/astuple do not honor their factory args. Waiting on one of
  329. # these:
  330. # https://github.com/python/mypy/issues/4236
  331. # https://github.com/python/typing/issues/253
  332. # XXX: remember to fix attrs.asdict/astuple too!
  333. def asdict(
  334. inst: AttrsInstance,
  335. recurse: bool = ...,
  336. filter: _FilterType[Any] | None = ...,
  337. dict_factory: type[Mapping[Any, Any]] = ...,
  338. retain_collection_types: bool = ...,
  339. value_serializer: Callable[[type, Attribute[Any], Any], Any] | None = ...,
  340. tuple_keys: bool | None = ...,
  341. ) -> dict[str, Any]: ...
  342. # TODO: add support for returning NamedTuple from the mypy plugin
  343. def astuple(
  344. inst: AttrsInstance,
  345. recurse: bool = ...,
  346. filter: _FilterType[Any] | None = ...,
  347. tuple_factory: type[Sequence[Any]] = ...,
  348. retain_collection_types: bool = ...,
  349. ) -> tuple[Any, ...]: ...
  350. def has(cls: type) -> TypeGuard[type[AttrsInstance]]: ...
  351. def assoc(inst: _T, **changes: Any) -> _T: ...
  352. def evolve(inst: _T, **changes: Any) -> _T: ...
  353. # _config --
  354. def set_run_validators(run: bool) -> None: ...
  355. def get_run_validators() -> bool: ...
  356. # aliases --
  357. s = attributes = attrs
  358. ib = attr = attrib
  359. dataclass = attrs # Technically, partial(attrs, auto_attribs=True) ;)