Skip to content

Commit b3c7c29

Browse files
committed
Add ~types~
1 parent 06b5900 commit b3c7c29

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/attr/__init__.pyi

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ else:
100100
takes_self: bool = ...,
101101
) -> _T: ...
102102

103+
In = TypeVar("In")
104+
Out = TypeVar("Out")
105+
106+
class Converter(Generic[In, Out]):
107+
@overload
108+
def __init__(
109+
self,
110+
converter: Callable[[In, AttrsInstance, Any], Out],
111+
*,
112+
takes_self: Literal[True] = ...,
113+
takes_field: Literal[True] = ...,
114+
) -> None: ...
115+
@overload
116+
def __init__(
117+
self,
118+
converter: Callable[[In, Any], Out],
119+
*,
120+
takes_field: Literal[True] = ...,
121+
) -> None: ...
122+
@overload
123+
def __init__(
124+
self,
125+
converter: Callable[[In, AttrsInstance], Out],
126+
*,
127+
takes_self: Literal[True] = ...,
128+
) -> None: ...
129+
@overload
130+
def __init__(self, converter: Callable[[In], Out]) -> None: ...
131+
103132
class Attribute(Generic[_T]):
104133
name: str
105134
default: _T | None
@@ -110,7 +139,7 @@ class Attribute(Generic[_T]):
110139
order: _EqOrderType
111140
hash: bool | None
112141
init: bool
113-
converter: _ConverterType | None
142+
converter: _ConverterType | Converter[Any, _T] | None
114143
metadata: dict[Any, Any]
115144
type: type[_T] | None
116145
kw_only: bool
@@ -174,7 +203,7 @@ def attrib(
174203
init: bool = ...,
175204
metadata: Mapping[Any, Any] | None = ...,
176205
type: type[_T] | None = ...,
177-
converter: _ConverterType | None = ...,
206+
converter: _ConverterType | Converter[Any, _T] | None = ...,
178207
factory: Callable[[], _T] | None = ...,
179208
kw_only: bool = ...,
180209
eq: _EqOrderType | None = ...,
@@ -194,7 +223,7 @@ def attrib(
194223
init: bool = ...,
195224
metadata: Mapping[Any, Any] | None = ...,
196225
type: type[_T] | None = ...,
197-
converter: _ConverterType | None = ...,
226+
converter: _ConverterType | Converter[Any, _T] | None = ...,
198227
factory: Callable[[], _T] | None = ...,
199228
kw_only: bool = ...,
200229
eq: _EqOrderType | None = ...,
@@ -214,7 +243,7 @@ def attrib(
214243
init: bool = ...,
215244
metadata: Mapping[Any, Any] | None = ...,
216245
type: object = ...,
217-
converter: _ConverterType | None = ...,
246+
converter: _ConverterType | Converter[Any, _T] | None = ...,
218247
factory: Callable[[], _T] | None = ...,
219248
kw_only: bool = ...,
220249
eq: _EqOrderType | None = ...,

src/attrs/__init__.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ from attr import Attribute as Attribute
2525
from attr import AttrsInstance as AttrsInstance
2626
from attr import cmp_using as cmp_using
2727
from attr import converters as converters
28+
from attr import Converter as Converter
2829
from attr import evolve as evolve
2930
from attr import exceptions as exceptions
3031
from attr import Factory as Factory
@@ -93,7 +94,7 @@ def field(
9394
hash: bool | None = ...,
9495
init: bool = ...,
9596
metadata: Mapping[Any, Any] | None = ...,
96-
converter: _ConverterType | None = ...,
97+
converter: _ConverterType | Converter[Any, _T] | None = ...,
9798
factory: Callable[[], _T] | None = ...,
9899
kw_only: bool = ...,
99100
eq: _EqOrderType | None = ...,
@@ -113,7 +114,7 @@ def field(
113114
hash: bool | None = ...,
114115
init: bool = ...,
115116
metadata: Mapping[Any, Any] | None = ...,
116-
converter: _ConverterType | None = ...,
117+
converter: _ConverterType | Converter[Any, _T] | None = ...,
117118
factory: Callable[[], _T] | None = ...,
118119
kw_only: bool = ...,
119120
eq: _EqOrderType | None = ...,
@@ -133,7 +134,7 @@ def field(
133134
hash: bool | None = ...,
134135
init: bool = ...,
135136
metadata: Mapping[Any, Any] | None = ...,
136-
converter: _ConverterType | None = ...,
137+
converter: _ConverterType | Converter[Any, _T] | None = ...,
137138
factory: Callable[[], _T] | None = ...,
138139
kw_only: bool = ...,
139140
eq: _EqOrderType | None = ...,

0 commit comments

Comments
 (0)