Skip to content

Commit 24b585f

Browse files
authored
Use attrs and @attrs.define in tests (#15152)
"attrs" namespace, "attrs.define" and "attrs.field" are the recommended new namespace and API of the attrs library (see https://www.attrs.org/en/stable/names.html) so we should modernize the bulk of our tests to use the current API.
1 parent d9d893f commit 24b585f

File tree

7 files changed

+148
-143
lines changed

7 files changed

+148
-143
lines changed

docs/source/additional_features.rst

+19-20
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

test-data/unit/check-flags.test

+10-10
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)