Skip to content

Commit bafb656

Browse files
committed
Add support for the new type argument.
1 parent b45e44c commit bafb656

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/attr/__init__.pyi

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
from typing import Any, Callable, Dict, Iterable, List, Optional, Mapping, Tuple, Type, TypeVar, Union, overload
1+
from typing import Any, Callable, Dict, Generic, Iterable, List, Optional, Mapping, Tuple, Type, TypeVar, Union, overload
22
from . import exceptions
33
from . import filters
44
from . import converters
55
from . import validators
66

77
# typing --
88

9-
C = TypeVar('C', bound=type)
10-
M = TypeVar('M', bound=Mapping)
11-
T = TypeVar('T', bound=tuple)
12-
I = TypeVar('I')
9+
_T = TypeVar('_T')
10+
_C = TypeVar('_C', bound=type)
11+
_M = TypeVar('_M', bound=Mapping)
12+
_TT = TypeVar('_TT', bound=tuple)
1313

1414
ValidatorType = Callable[[object, 'Attribute', Any], Any]
1515
ConverterType = Callable[[Any], Any]
@@ -21,41 +21,41 @@ class _CountingAttr: ...
2121

2222
NOTHING : object
2323

24+
class Factory(Generic[_T]):
25+
factory : Union[Callable[[], _T], Callable[[object], _T]]
26+
takes_self : bool
27+
def __init__(self, factory: Union[Callable[[], _T], Callable[[object], _T]], takes_self: bool = ...) -> None: ...
28+
2429
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+
__slots__ = ("name", "default", "validator", "repr", "cmp", "hash", "init", "convert", "metadata", "type")
31+
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 = ..., type: Union[type, Factory] = ...) -> None: ...
3032

31-
# NOTE: the stub for `attr` returns Any so that static analysis passes when used in the form: x : int = attr()
33+
# NOTE: this overload for `attr` returns Any so that static analysis passes when used in the form: x : int = attr()
34+
@overload
3235
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: ...
36+
@overload
37+
def attr(default: Any = ..., validator: Optional[Union[ValidatorType, List[ValidatorType]]] = ..., repr: bool = ..., cmp: bool = ..., hash: Optional[bool] = ..., init: bool = ..., convert: Optional[ConverterType] = ..., metadata: Mapping = ..., type: Union[Type[_T], Factory[_T]] = ...) -> _T: ...
3338

3439
@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: ...
40+
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: ...
3641
@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]: ...
42+
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]: ...
3843

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

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-
4747
def make_class(name, attrs: Union[List[_CountingAttr], Dict[str, _CountingAttr]], bases: Tuple[type, ...] = ..., **attributes_arguments) -> type: ...
4848

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

5151
# _funcs --
5252

5353
# 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: ...
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], _TT] = ..., retain_collection_types: bool = ...) -> _TT: ...
5656
def has(cls: type) -> bool: ...
57-
def assoc(inst: I, **changes) -> I: ...
58-
def evolve(inst: I, **changes) -> I: ...
57+
def assoc(inst: _T, **changes) -> _T: ...
58+
def evolve(inst: _T, **changes) -> _T: ...
5959

6060
# _config --
6161

0 commit comments

Comments
 (0)