Skip to content

Commit

Permalink
Add PEP484 stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrik committed Nov 3, 2017
1 parent 1bb8fbd commit e3d43a9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/attr/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from typing import Any, Callable, Dict, Iterable, List, Optional, Mapping, Tuple, Type, TypeVar, Union, overload
from . import exceptions
from . import filters
from . import converters
from . import validators

# typing --

C = TypeVar('C', bound=type)
M = TypeVar('M', bound=Mapping)
T = TypeVar('T', bound=tuple)
I = TypeVar('I')

ValidatorType = Callable[[object, 'Attribute', Any], Any]
ConverterType = Callable[[Any], Any]
FilterType = Callable[['Attribute', Any], bool]

# _make --

class _CountingAttr: ...

NOTHING : object

class Attribute:
__slots__ = (
"name", "default", "validator", "repr", "cmp", "hash", "init",
"convert", "metadata",
)
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: ...

# NOTE: the stub for `attr` returns Any so that static analysis passes when used in the form: x : int = attr()
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: ...

@overload
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: ...
@overload
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]: ...

def fields(cls: type) -> Tuple[Attribute, ...]: ...
def validate(inst: object) -> None: ...

class Factory:
factory : Union[Callable[[Any], Any], Callable[[object, Any], Any]]
takes_self : bool
def __init__(self, factory: Union[Callable[[Any], Any], Callable[[object, Any], Any]], takes_self: bool = ...) -> None: ...

def make_class(name, attrs: Union[List[_CountingAttr], Dict[str, _CountingAttr]], bases: Tuple[type, ...] = ..., **attributes_arguments) -> type: ...

def and_(*validators: Iterable[ValidatorType]) -> ValidatorType: ...

# _funcs --

# FIXME: having problems assigning a default to the factory typevars
def asdict(inst: object, recurse: bool = ..., filter: Optional[FilterType] = ..., dict_factory: Callable[[], M] = ..., retain_collection_types: bool = ...) -> M: ...
def astuple(inst: object, recurse: bool = ..., filter: Optional[FilterType] = ..., tuple_factory: Callable[[Iterable], T] = ..., retain_collection_types: bool = ...) -> T: ...
def has(cls: type) -> bool: ...
def assoc(inst: I, **changes) -> I: ...
def evolve(inst: I, **changes) -> I: ...

# _config --

def set_run_validators(run: bool) -> None: ...
def get_run_validators() -> bool: ...

# aliases
s = attrs = attributes
ib = attrib = attr
3 changes: 3 additions & 0 deletions src/attr/converters.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import ConverterType

def optional(converter: ConverterType) -> ConverterType: ...
9 changes: 9 additions & 0 deletions src/attr/exceptions.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import List

class FrozenInstanceError(AttributeError):
msg : str = ...
args : List[str] = ...

class AttrsAttributeNotFoundError(ValueError): ...
class NotAnAttrsClassError(ValueError): ...
class DefaultAlreadySetError(RuntimeError): ...
5 changes: 5 additions & 0 deletions src/attr/filters.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Union
from . import Attribute, FilterType

def include(*what: Union[type, Attribute]) -> FilterType: ...
def exclude(*what: Union[type, Attribute]) -> FilterType: ...
10 changes: 10 additions & 0 deletions src/attr/validators.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Container, List, Union
from . import ValidatorType

def instance_of(type: type) -> ValidatorType: ...

def provides(interface) -> ValidatorType: ...

def optional(validator: Union[ValidatorType, List[ValidatorType]]) -> ValidatorType: ...

def in_(options: Container) -> ValidatorType: ...

0 comments on commit e3d43a9

Please sign in to comment.