Skip to content

Commit c1953f2

Browse files
eurestiJelleZijlstra
authored andcommitted
Sync attr stubs from attrs github repo (#2720)
This include some changes like kw_only and some formatting changes.
1 parent f4aed1f commit c1953f2

File tree

5 files changed

+195
-134
lines changed

5 files changed

+195
-134
lines changed

third_party/2and3/attr/__init__.pyi

Lines changed: 165 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
from typing import Any, Callable, Dict, Generic, List, Optional, Sequence, Mapping, Tuple, Type, TypeVar, Union, overload
1+
from typing import (
2+
Any,
3+
Callable,
4+
Dict,
5+
Generic,
6+
List,
7+
Optional,
8+
Sequence,
9+
Mapping,
10+
Tuple,
11+
Type,
12+
TypeVar,
13+
Union,
14+
overload,
15+
)
16+
217
# `import X as X` is required to make these public
318
from . import exceptions as exceptions
419
from . import filters as filters
520
from . import converters as converters
621
from . import validators as validators
722

8-
_T = TypeVar('_T')
9-
_C = TypeVar('_C', bound=type)
23+
_T = TypeVar("_T")
24+
_C = TypeVar("_C", bound=type)
1025

11-
_ValidatorType = Callable[[Any, Attribute, _T], Any]
26+
_ValidatorType = Callable[[Any, Attribute[_T], _T], Any]
1227
_ConverterType = Callable[[Any], _T]
13-
_FilterType = Callable[[Attribute, Any], bool]
28+
_FilterType = Callable[[Attribute[_T], _T], bool]
1429
# FIXME: in reality, if multiple validators are passed they must be in a list or tuple,
1530
# but those are invariant and so would prevent subtypes of _ValidatorType from working
1631
# when passed in a list or tuple.
@@ -25,7 +40,10 @@ NOTHING: object
2540
@overload
2641
def Factory(factory: Callable[[], _T]) -> _T: ...
2742
@overload
28-
def Factory(factory: Union[Callable[[Any], _T], Callable[[], _T]], takes_self: bool = ...) -> _T: ...
43+
def Factory(
44+
factory: Union[Callable[[Any], _T], Callable[[], _T]],
45+
takes_self: bool = ...,
46+
) -> _T: ...
2947

3048
class Attribute(Generic[_T]):
3149
name: str
@@ -38,30 +56,26 @@ class Attribute(Generic[_T]):
3856
converter: Optional[_ConverterType[_T]]
3957
metadata: Dict[Any, Any]
4058
type: Optional[Type[_T]]
41-
def __lt__(self, x: Attribute) -> bool: ...
42-
def __le__(self, x: Attribute) -> bool: ...
43-
def __gt__(self, x: Attribute) -> bool: ...
44-
def __ge__(self, x: Attribute) -> bool: ...
45-
59+
kw_only: bool
60+
def __lt__(self, x: Attribute[_T]) -> bool: ...
61+
def __le__(self, x: Attribute[_T]) -> bool: ...
62+
def __gt__(self, x: Attribute[_T]) -> bool: ...
63+
def __ge__(self, x: Attribute[_T]) -> bool: ...
4664

4765
# NOTE: We had several choices for the annotation to use for type arg:
4866
# 1) Type[_T]
49-
# - Pros: works in PyCharm without plugin support
50-
# - Cons: produces less informative error in the case of conflicting TypeVars
51-
# e.g. `attr.ib(default='bad', type=int)`
67+
# - Pros: Handles simple cases correctly
68+
# - Cons: Might produce less informative errors in the case of conflicting TypeVars
69+
# e.g. `attr.ib(default='bad', type=int)`
5270
# 2) Callable[..., _T]
53-
# - Pros: more informative errors than #1
54-
# - Cons: validator tests results in confusing error.
55-
# e.g. `attr.ib(type=int, validator=validate_str)`
71+
# - Pros: Better error messages than #1 for conflicting TypeVars
72+
# - Cons: Terrible error messages for validator checks.
73+
# e.g. attr.ib(type=int, validator=validate_str)
74+
# -> error: Cannot infer function type argument
5675
# 3) type (and do all of the work in the mypy plugin)
57-
# - Pros: in mypy, the behavior of type argument is exactly the same as with
58-
# annotations.
59-
# - Cons: completely disables type inspections in PyCharm when using the
60-
# type arg.
61-
# We chose option #1 until either PyCharm adds support for attrs, or python 2
62-
# reaches EOL.
63-
64-
# NOTE: If you update these, update `ib` and `attr` below.
76+
# - Pros: Simple here, and we could customize the plugin with our own errors.
77+
# - Cons: Would need to write mypy plugin code to handle all the cases.
78+
# We chose option #1.
6579

6680
# `attr` lies about its return type to make the following possible:
6781
# attr() -> Any
@@ -72,129 +86,156 @@ class Attribute(Generic[_T]):
7286
#
7387
# This form catches explicit None or no default but with no other arguments returns Any.
7488
@overload
75-
def attrib(default: None = ...,
76-
validator: None = ...,
77-
repr: bool = ...,
78-
cmp: bool = ...,
79-
hash: Optional[bool] = ...,
80-
init: bool = ...,
81-
convert: None = ...,
82-
metadata: Optional[Mapping[Any, Any]] = ...,
83-
type: None = ...,
84-
converter: None = ...,
85-
factory: None = ...,
86-
) -> Any: ...
89+
def attrib(
90+
default: None = ...,
91+
validator: None = ...,
92+
repr: bool = ...,
93+
cmp: bool = ...,
94+
hash: Optional[bool] = ...,
95+
init: bool = ...,
96+
convert: None = ...,
97+
metadata: Optional[Mapping[Any, Any]] = ...,
98+
type: None = ...,
99+
converter: None = ...,
100+
factory: None = ...,
101+
kw_only: bool = ...,
102+
) -> Any: ...
103+
87104
# This form catches an explicit None or no default and infers the type from the other arguments.
88105
@overload
89-
def attrib(default: None = ...,
90-
validator: Optional[_ValidatorArgType[_T]] = ...,
91-
repr: bool = ...,
92-
cmp: bool = ...,
93-
hash: Optional[bool] = ...,
94-
init: bool = ...,
95-
convert: Optional[_ConverterType[_T]] = ...,
96-
metadata: Optional[Mapping[Any, Any]] = ...,
97-
type: Optional[Type[_T]] = ...,
98-
converter: Optional[_ConverterType[_T]] = ...,
99-
factory: Optional[Callable[[], _T]] = ...,
100-
) -> _T: ...
106+
def attrib(
107+
default: None = ...,
108+
validator: Optional[_ValidatorArgType[_T]] = ...,
109+
repr: bool = ...,
110+
cmp: bool = ...,
111+
hash: Optional[bool] = ...,
112+
init: bool = ...,
113+
convert: Optional[_ConverterType[_T]] = ...,
114+
metadata: Optional[Mapping[Any, Any]] = ...,
115+
type: Optional[Type[_T]] = ...,
116+
converter: Optional[_ConverterType[_T]] = ...,
117+
factory: Optional[Callable[[], _T]] = ...,
118+
kw_only: bool = ...,
119+
) -> _T: ...
120+
101121
# This form catches an explicit default argument.
102122
@overload
103-
def attrib(default: _T,
104-
validator: Optional[_ValidatorArgType[_T]] = ...,
105-
repr: bool = ...,
106-
cmp: bool = ...,
107-
hash: Optional[bool] = ...,
108-
init: bool = ...,
109-
convert: Optional[_ConverterType[_T]] = ...,
110-
metadata: Optional[Mapping[Any, Any]] = ...,
111-
type: Optional[Type[_T]] = ...,
112-
converter: Optional[_ConverterType[_T]] = ...,
113-
factory: Optional[Callable[[], _T]] = ...,
114-
) -> _T: ...
123+
def attrib(
124+
default: _T,
125+
validator: Optional[_ValidatorArgType[_T]] = ...,
126+
repr: bool = ...,
127+
cmp: bool = ...,
128+
hash: Optional[bool] = ...,
129+
init: bool = ...,
130+
convert: Optional[_ConverterType[_T]] = ...,
131+
metadata: Optional[Mapping[Any, Any]] = ...,
132+
type: Optional[Type[_T]] = ...,
133+
converter: Optional[_ConverterType[_T]] = ...,
134+
factory: Optional[Callable[[], _T]] = ...,
135+
kw_only: bool = ...,
136+
) -> _T: ...
137+
115138
# This form covers type=non-Type: e.g. forward references (str), Any
116139
@overload
117-
def attrib(default: Optional[_T] = ...,
118-
validator: Optional[_ValidatorArgType[_T]] = ...,
119-
repr: bool = ...,
120-
cmp: bool = ...,
121-
hash: Optional[bool] = ...,
122-
init: bool = ...,
123-
convert: Optional[_ConverterType[_T]] = ...,
124-
metadata: Optional[Mapping[Any, Any]] = ...,
125-
type: object = ...,
126-
converter: Optional[_ConverterType[_T]] = ...,
127-
factory: Optional[Callable[[], _T]] = ...,
128-
) -> Any: ...
129-
130-
131-
# NOTE: If you update these, update `s` and `attributes` below.
140+
def attrib(
141+
default: Optional[_T] = ...,
142+
validator: Optional[_ValidatorArgType[_T]] = ...,
143+
repr: bool = ...,
144+
cmp: bool = ...,
145+
hash: Optional[bool] = ...,
146+
init: bool = ...,
147+
convert: Optional[_ConverterType[_T]] = ...,
148+
metadata: Optional[Mapping[Any, Any]] = ...,
149+
type: object = ...,
150+
converter: Optional[_ConverterType[_T]] = ...,
151+
factory: Optional[Callable[[], _T]] = ...,
152+
kw_only: bool = ...,
153+
) -> Any: ...
132154
@overload
133-
def attrs(maybe_cls: _C,
134-
these: Optional[Dict[str, Any]] = ...,
135-
repr_ns: Optional[str] = ...,
136-
repr: bool = ...,
137-
cmp: bool = ...,
138-
hash: Optional[bool] = ...,
139-
init: bool = ...,
140-
slots: bool = ...,
141-
frozen: bool = ...,
142-
str: bool = ...,
143-
auto_attribs: bool = ...) -> _C: ...
155+
def attrs(
156+
maybe_cls: _C,
157+
these: Optional[Dict[str, Any]] = ...,
158+
repr_ns: Optional[str] = ...,
159+
repr: bool = ...,
160+
cmp: bool = ...,
161+
hash: Optional[bool] = ...,
162+
init: bool = ...,
163+
slots: bool = ...,
164+
frozen: bool = ...,
165+
weakref_slot: bool = ...,
166+
str: bool = ...,
167+
auto_attribs: bool = ...,
168+
kw_only: bool = ...,
169+
cache_hash: bool = ...,
170+
) -> _C: ...
144171
@overload
145-
def attrs(maybe_cls: None = ...,
146-
these: Optional[Dict[str, Any]] = ...,
147-
repr_ns: Optional[str] = ...,
148-
repr: bool = ...,
149-
cmp: bool = ...,
150-
hash: Optional[bool] = ...,
151-
init: bool = ...,
152-
slots: bool = ...,
153-
frozen: bool = ...,
154-
str: bool = ...,
155-
auto_attribs: bool = ...) -> Callable[[_C], _C]: ...
156-
172+
def attrs(
173+
maybe_cls: None = ...,
174+
these: Optional[Dict[str, Any]] = ...,
175+
repr_ns: Optional[str] = ...,
176+
repr: bool = ...,
177+
cmp: bool = ...,
178+
hash: Optional[bool] = ...,
179+
init: bool = ...,
180+
slots: bool = ...,
181+
frozen: bool = ...,
182+
weakref_slot: bool = ...,
183+
str: bool = ...,
184+
auto_attribs: bool = ...,
185+
kw_only: bool = ...,
186+
cache_hash: bool = ...,
187+
) -> Callable[[_C], _C]: ...
157188

158189
# TODO: add support for returning NamedTuple from the mypy plugin
159-
class _Fields(Tuple[Attribute, ...]):
160-
def __getattr__(self, name: str) -> Attribute: ...
190+
class _Fields(Tuple[Attribute[Any], ...]):
191+
def __getattr__(self, name: str) -> Attribute[Any]: ...
161192

162193
def fields(cls: type) -> _Fields: ...
163-
def fields_dict(cls: type) -> Dict[str, Attribute]: ...
194+
def fields_dict(cls: type) -> Dict[str, Attribute[Any]]: ...
164195
def validate(inst: Any) -> None: ...
165196

166197
# TODO: add support for returning a proper attrs class from the mypy plugin
167198
# we use Any instead of _CountingAttr so that e.g. `make_class('Foo', [attr.ib()])` is valid
168-
def make_class(name: str,
169-
attrs: Union[List[str], Tuple[str, ...], Dict[str, Any]],
170-
bases: Tuple[type, ...] = ...,
171-
repr_ns: Optional[str] = ...,
172-
repr: bool = ...,
173-
cmp: bool = ...,
174-
hash: Optional[bool] = ...,
175-
init: bool = ...,
176-
slots: bool = ...,
177-
frozen: bool = ...,
178-
str: bool = ...,
179-
auto_attribs: bool = ...) -> type: ...
199+
def make_class(
200+
name: str,
201+
attrs: Union[List[str], Tuple[str, ...], Dict[str, Any]],
202+
bases: Tuple[type, ...] = ...,
203+
repr_ns: Optional[str] = ...,
204+
repr: bool = ...,
205+
cmp: bool = ...,
206+
hash: Optional[bool] = ...,
207+
init: bool = ...,
208+
slots: bool = ...,
209+
frozen: bool = ...,
210+
weakref_slot: bool = ...,
211+
str: bool = ...,
212+
auto_attribs: bool = ...,
213+
kw_only: bool = ...,
214+
cache_hash: bool = ...,
215+
) -> type: ...
180216

181217
# _funcs --
182218

183219
# TODO: add support for returning TypedDict from the mypy plugin
184220
# FIXME: asdict/astuple do not honor their factory args. waiting on one of these:
185221
# https://github.com/python/mypy/issues/4236
186222
# https://github.com/python/typing/issues/253
187-
def asdict(inst: Any,
188-
recurse: bool = ...,
189-
filter: Optional[_FilterType] = ...,
190-
dict_factory: Type[Mapping[Any, Any]] = ...,
191-
retain_collection_types: bool = ...) -> Dict[str, Any]: ...
223+
def asdict(
224+
inst: Any,
225+
recurse: bool = ...,
226+
filter: Optional[_FilterType[Any]] = ...,
227+
dict_factory: Type[Mapping[Any, Any]] = ...,
228+
retain_collection_types: bool = ...,
229+
) -> Dict[str, Any]: ...
230+
192231
# TODO: add support for returning NamedTuple from the mypy plugin
193-
def astuple(inst: Any,
194-
recurse: bool = ...,
195-
filter: Optional[_FilterType] = ...,
196-
tuple_factory: Type[Sequence] = ...,
197-
retain_collection_types: bool = ...) -> Tuple[Any, ...]: ...
232+
def astuple(
233+
inst: Any,
234+
recurse: bool = ...,
235+
filter: Optional[_FilterType[Any]] = ...,
236+
tuple_factory: Type[Sequence[Any]] = ...,
237+
retain_collection_types: bool = ...,
238+
) -> Tuple[Any, ...]: ...
198239
def has(cls: type) -> bool: ...
199240
def assoc(inst: _T, **changes: Any) -> _T: ...
200241
def evolve(inst: _T, **changes: Any) -> _T: ...
@@ -204,7 +245,6 @@ def evolve(inst: _T, **changes: Any) -> _T: ...
204245
def set_run_validators(run: bool) -> None: ...
205246
def get_run_validators() -> bool: ...
206247

207-
208248
# aliases --
209249

210250
s = attributes = attrs

third_party/2and3/attr/converters.pyi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
from typing import TypeVar, Optional
1+
from typing import TypeVar, Optional, Callable, overload
22
from . import _ConverterType
33

4-
_T = TypeVar('_T')
4+
_T = TypeVar("_T")
55

6-
def optional(converter: _ConverterType[_T]) -> _ConverterType[Optional[_T]]: ...
6+
def optional(
7+
converter: _ConverterType[_T]
8+
) -> _ConverterType[Optional[_T]]: ...
9+
@overload
10+
def default_if_none(default: _T) -> _ConverterType[_T]: ...
11+
@overload
12+
def default_if_none(*, factory: Callable[[], _T]) -> _ConverterType[_T]: ...

third_party/2and3/attr/exceptions.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class FrozenInstanceError(AttributeError):
22
msg: str = ...
3+
34
class AttrsAttributeNotFoundError(ValueError): ...
45
class NotAnAttrsClassError(ValueError): ...
56
class DefaultAlreadySetError(RuntimeError): ...

third_party/2and3/attr/filters.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from typing import Union
1+
from typing import Union, Any
22
from . import Attribute, _FilterType
33

4-
def include(*what: Union[type, Attribute]) -> _FilterType: ...
5-
def exclude(*what: Union[type, Attribute]) -> _FilterType: ...
4+
def include(*what: Union[type, Attribute[Any]]) -> _FilterType[Any]: ...
5+
def exclude(*what: Union[type, Attribute[Any]]) -> _FilterType[Any]: ...

0 commit comments

Comments
 (0)