@@ -121,55 +121,54 @@ Type annotations can be added as follows:
121
121
122
122
import attr
123
123
124
- @attr.s
124
+ @attrs.define
125
125
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 )
129
129
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 ``:
131
131
132
132
.. code-block :: python
133
133
134
- import attr
134
+ import attrs
135
135
136
- @attr.s ( auto_attribs = True )
136
+ @attrs.define
137
137
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
141
141
142
142
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
144
144
annotation says these return the types that they expect to be assigned to.
145
145
That enables this to work:
146
146
147
147
.. code-block :: python
148
148
149
- import attr
150
- from typing import Dict
149
+ import attrs
151
150
152
- @attr.s ( auto_attribs = True )
151
+ @attrs.define
153
152
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
157
156
158
157
Caveats/Known Issues
159
158
====================
160
159
161
160
* The detection of attr classes and attributes works by function name only.
162
161
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.
164
163
165
164
* All boolean arguments that mypy cares about must be literal ``True `` or ``False ``.
166
165
e.g the following will not work:
167
166
168
167
.. code-block :: python
169
168
170
- import attr
169
+ import attrs
171
170
YES = True
172
- @attr.s (init = YES )
171
+ @attrs.define (init = YES )
173
172
class A :
174
173
...
175
174
0 commit comments