Skip to content

Commit 72fd158

Browse files
committed
Use attrs and @attrs.define in tests
1 parent 09d469f commit 72fd158

File tree

7 files changed

+157
-157
lines changed

7 files changed

+157
-157
lines changed

docs/source/additional_features.rst

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,55 +121,54 @@ Type annotations can be added as follows:
121121
122122
import attr
123123
124-
@attr.s
124+
@attrs.define
125125
class A:
126-
one: int = attr.ib() # Variable annotation (Python 3.6+)
127-
two = attr.ib() # type: int # Type comment
128-
three = attr.ib(type=int) # type= argument
126+
one: int
127+
two: int = 7
128+
three: int = attrs.field(8)
129129
130-
If you're using ``auto_attribs=True`` you must use variable annotations.
130+
If you're using ``auto_attribs=False`` you must use ``attrs.field``:
131131

132132
.. code-block:: python
133133
134-
import attr
134+
import attrs
135135
136-
@attr.s(auto_attribs=True)
136+
@attrs.define
137137
class A:
138-
one: int
139-
two: int = 7
140-
three: int = attr.ib(8)
138+
one: int = attrs.field() # Variable annotation (Python 3.6+)
139+
two = attrs.field() # type: int # Type comment
140+
three = attrs.field(type=int) # type= argument
141141
142142
Typeshed has a couple of "white lie" annotations to make type checking
143-
easier. :py:func:`attr.ib` and :py:class:`attr.Factory` actually return objects, but the
143+
easier. :py:func:`attrs.field` and :py:class:`attrs.Factory` actually return objects, but the
144144
annotation says these return the types that they expect to be assigned to.
145145
That enables this to work:
146146

147147
.. code-block:: python
148148
149-
import attr
150-
from typing import Dict
149+
import attrs
151150
152-
@attr.s(auto_attribs=True)
151+
@attrs.define
153152
class A:
154-
one: int = attr.ib(8)
155-
two: Dict[str, str] = attr.Factory(dict)
156-
bad: str = attr.ib(16) # Error: can't assign int to str
153+
one: int = attrs.field(8)
154+
two: dict[str, str] = attrs.Factory(dict)
155+
bad: str = attrs.field(16) # Error: can't assign int to str
157156
158157
Caveats/Known Issues
159158
====================
160159

161160
* The detection of attr classes and attributes works by function name only.
162161
This means that if you have your own helper functions that, for example,
163-
``return attr.ib()`` mypy will not see them.
162+
``return attrs.field()`` mypy will not see them.
164163

165164
* All boolean arguments that mypy cares about must be literal ``True`` or ``False``.
166165
e.g the following will not work:
167166

168167
.. code-block:: python
169168
170-
import attr
169+
import attrs
171170
YES = True
172-
@attr.s(init=YES)
171+
@attrs.define(init=YES)
173172
class A:
174173
...
175174

mypyc/test-data/run-attrs.test

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
-- Test cases for dataclasses based on the attrs library, where auto_attribs=True
22

33
[case testRunAttrsclass]
4-
import attr
4+
import attrs
55
from typing import Set, List, Callable, Any
66

7-
@attr.s(auto_attribs=True)
7+
@attrs.define
88
class Person1:
99
age : int
1010
name : str
@@ -18,19 +18,19 @@ def testBool(p: Person1) -> bool:
1818
else:
1919
return False
2020

21-
@attr.s(auto_attribs=True)
21+
@attrs.define
2222
class Person1b(Person1):
2323
id: str = '000'
2424

25-
@attr.s(auto_attribs=True)
25+
@attrs.define
2626
class Person2:
2727
age : int
28-
name : str = attr.ib(default='robot')
28+
name : str = 'robot'
2929

30-
@attr.s(auto_attribs=True, order=True)
30+
@attrs.define(order=True)
3131
class Person3:
32-
age : int = attr.ib(default = 6)
33-
friendIDs : List[int] = attr.ib(factory = list)
32+
age : int = attrs.field(default=6)
33+
friendIDs : List[int] = attrs.field(factory=list)
3434

3535
def get_age(self) -> int:
3636
return (self.age)
@@ -49,7 +49,7 @@ def get_next_age(g: Callable[[Any], int]) -> Callable[[Any], int]:
4949
return g(a) + 1
5050
return f
5151

52-
@attr.s(auto_attribs=True)
52+
@attrs.define
5353
class Person4:
5454
age : int
5555
_name : str = 'Bot'
@@ -62,10 +62,10 @@ class Person4:
6262
def name(self) -> str:
6363
return self._name
6464

65-
@attr.s(auto_attribs=True)
65+
@attrs.define
6666
class Point:
67-
x : int = attr.ib(converter=int)
68-
y : int = attr.ib(init=False)
67+
x : int = attrs.field(converter=int)
68+
y : int = attrs.field(init=False)
6969

7070
def __attrs_post_init__(self):
7171
self.y = self.x + 1
@@ -165,10 +165,10 @@ assert isinstance(Person3().get_age, BuiltinMethodType)
165165

166166

167167
[case testRunAttrsclassNonAuto]
168-
import attr
168+
import attrs
169169
from typing import Set, List, Callable, Any
170170

171-
@attr.s
171+
@attrs.define
172172
class Person1:
173173
age = attr.ib(type=int)
174174
name = attr.ib(type=str)
@@ -182,16 +182,16 @@ def testBool(p: Person1) -> bool:
182182
else:
183183
return False
184184

185-
@attr.s
185+
@attrs.define
186186
class Person1b(Person1):
187187
id = attr.ib(type=str, default='000')
188188

189-
@attr.s
189+
@attrs.define
190190
class Person2:
191191
age = attr.ib(type=int)
192192
name = attr.ib(type=str, default='robot')
193193

194-
@attr.s(order=True)
194+
@attrs.define(order=True)
195195
class Person3:
196196
age = attr.ib(type=int, default=6)
197197
friendIDs = attr.ib(factory=list, type=List[int])
@@ -213,7 +213,7 @@ def get_next_age(g: Callable[[Any], int]) -> Callable[[Any], int]:
213213
return g(a) + 1
214214
return f
215215

216-
@attr.s
216+
@attrs.define
217217
class Person4:
218218
age = attr.ib(type=int)
219219
_name = attr.ib(type=str, default='Bot')
@@ -226,7 +226,7 @@ class Person4:
226226
def name(self) -> str:
227227
return self._name
228228

229-
@attr.s
229+
@attrs.define
230230
class Point:
231231
x = attr.ib(type=int, converter=int)
232232
y = attr.ib(type=int, init=False)

test-data/unit/check-flags.test

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,32 +1305,32 @@ main:3: error: Function is missing a type annotation for one or more arguments
13051305

13061306
[case testDisallowIncompleteDefsAttrsNoAnnotations]
13071307
# flags: --disallow-incomplete-defs
1308-
import attr
1308+
import attrs
13091309

1310-
@attr.s()
1310+
@attrs.define
13111311
class Unannotated:
1312-
foo = attr.ib()
1312+
foo = attrs.field()
13131313

13141314
[builtins fixtures/plugin_attrs.pyi]
13151315

13161316
[case testDisallowIncompleteDefsAttrsWithAnnotations]
13171317
# flags: --disallow-incomplete-defs
1318-
import attr
1318+
import attrs
13191319

1320-
@attr.s()
1320+
@attrs.define
13211321
class Annotated:
1322-
bar: int = attr.ib()
1322+
bar: int = attrs.field()
13231323

13241324
[builtins fixtures/plugin_attrs.pyi]
13251325

13261326
[case testDisallowIncompleteDefsAttrsPartialAnnotations]
13271327
# flags: --disallow-incomplete-defs
1328-
import attr
1328+
import attrs
13291329

1330-
@attr.s()
1330+
@attrs.define
13311331
class PartiallyAnnotated: # E: Function is missing a type annotation for one or more arguments
1332-
bar: int = attr.ib()
1333-
baz = attr.ib()
1332+
bar: int = attrs.field()
1333+
baz = attrs.field()
13341334

13351335
[builtins fixtures/plugin_attrs.pyi]
13361336

0 commit comments

Comments
 (0)