Skip to content

Commit 58e3807

Browse files
committed
Add PEP484 stubs
1 parent ba9e8bc commit 58e3807

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

src/attr/__init__.pyi

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from typing import Any, Callable, Dict, Iterable, List, Optional, Mapping, Tuple, Type, TypeVar, Union, overload
2+
from . import exceptions
3+
from . import filters
4+
from . import converters
5+
from . import validators
6+
7+
# typing --
8+
9+
C = TypeVar('C', bound=type)
10+
M = TypeVar('M', bound=Mapping)
11+
T = TypeVar('T', bound=tuple)
12+
I = TypeVar('I')
13+
14+
ValidatorType = Callable[[object, 'Attribute', Any], Any]
15+
ConverterType = Callable[[Any], Any]
16+
FilterType = Callable[['Attribute', Any], bool]
17+
18+
# _make --
19+
20+
class _CountingAttr: ...
21+
22+
NOTHING : object
23+
24+
class Attribute:
25+
__slots__ = (
26+
"name", "default", "validator", "repr", "cmp", "hash", "init",
27+
"convert", "metadata",
28+
)
29+
def __init__(self, name: str, default: Any, validator: Optional[Union[ValidatorType, List[ValidatorType]]], repr: bool, cmp: bool, hash: Optional[bool], init: bool, convert: Optional[ConverterType] = ..., metadata: Mapping = ...) -> None: ...
30+
31+
# NOTE: the stub for `attr` returns Any so that static analysis passes when used in the form: x : int = attr()
32+
def attr(default: Any = ..., validator: Optional[Union[ValidatorType, List[ValidatorType]]] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., convert: Optional[ConverterType] = ..., metadata: Mapping = ...) -> Any: ...
33+
34+
@overload
35+
def attributes(maybe_cls: C = ..., these: Optional[Dict[str, _CountingAttr]] = ..., repr_ns: Optional[str] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., slots: bool = ..., frozen: bool = ..., str: bool = ...) -> C: ...
36+
@overload
37+
def attributes(maybe_cls: None = ..., these: Optional[Dict[str, _CountingAttr]] = ..., repr_ns: Optional[str] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., slots: bool = ..., frozen: bool = ..., str: bool = ...) -> Callable[[C], C]: ...
38+
39+
def fields(cls: type) -> Tuple[Attribute, ...]: ...
40+
def validate(inst: object) -> None: ...
41+
42+
class Factory:
43+
factory : Union[Callable[[Any], Any], Callable[[object, Any], Any]]
44+
takes_self : bool
45+
def __init__(self, factory: Union[Callable[[Any], Any], Callable[[object, Any], Any]], takes_self: bool = ...) -> None: ...
46+
47+
def make_class(name, attrs: Union[List[_CountingAttr], Dict[str, _CountingAttr]], bases: Tuple[type, ...] = ..., **attributes_arguments) -> type: ...
48+
49+
def and_(*validators: Iterable[ValidatorType]) -> ValidatorType: ...
50+
51+
# _funcs --
52+
53+
# FIXME: having problems assigning a default to the factory typevars
54+
def asdict(inst: object, recurse: bool = ..., filter: Optional[FilterType] = ..., dict_factory: Callable[[], M] = ..., retain_collection_types: bool = ...) -> M: ...
55+
def astuple(inst: object, recurse: bool = ..., filter: Optional[FilterType] = ..., tuple_factory: Callable[[Iterable], T] = ..., retain_collection_types: bool = ...) -> T: ...
56+
def has(cls: type) -> bool: ...
57+
def assoc(inst: I, **changes) -> I: ...
58+
def evolve(inst: I, **changes) -> I: ...
59+
60+
# _config --
61+
62+
def set_run_validators(run: bool) -> None: ...
63+
def get_run_validators() -> bool: ...
64+
65+
# aliases
66+
s = attrs = attributes
67+
ib = attrib = attr

src/attr/converters.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import ConverterType
2+
3+
def optional(converter: ConverterType) -> ConverterType: ...

src/attr/exceptions.pyi

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List
2+
3+
class FrozenInstanceError(AttributeError):
4+
msg : str = ...
5+
args : List[str] = ...
6+
7+
class AttrsAttributeNotFoundError(ValueError): ...
8+
class NotAnAttrsClassError(ValueError): ...
9+
class DefaultAlreadySetError(RuntimeError): ...

src/attr/filters.pyi

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from typing import Union
2+
from . import Attribute, FilterType
3+
4+
def include(*what: Union[type, Attribute]) -> FilterType: ...
5+
def exclude(*what: Union[type, Attribute]) -> FilterType: ...

src/attr/validators.pyi

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Container, List, Union
2+
from . import ValidatorType
3+
4+
def instance_of(type: type) -> ValidatorType: ...
5+
6+
def provides(interface) -> ValidatorType: ...
7+
8+
def optional(validator: Union[ValidatorType, List[ValidatorType]]) -> ValidatorType: ...
9+
10+
def in_(options: Container) -> ValidatorType: ...

0 commit comments

Comments
 (0)