Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use attrs and @attrs.define in tests #15152

Merged
merged 3 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions docs/source/additional_features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,55 +121,54 @@ Type annotations can be added as follows:

import attr

@attr.s
@attrs.define
class A:
one: int = attr.ib() # Variable annotation (Python 3.6+)
two = attr.ib() # type: int # Type comment
three = attr.ib(type=int) # type= argument
one: int
two: int = 7
three: int = attrs.field(8)

If you're using ``auto_attribs=True`` you must use variable annotations.
If you're using ``auto_attribs=False`` you must use ``attrs.field``:

.. code-block:: python

import attr
import attrs

@attr.s(auto_attribs=True)
@attrs.define
class A:
one: int
two: int = 7
three: int = attr.ib(8)
one: int = attrs.field() # Variable annotation (Python 3.6+)
two = attrs.field() # type: int # Type comment
three = attrs.field(type=int) # type= argument

Typeshed has a couple of "white lie" annotations to make type checking
easier. :py:func:`attr.ib` and :py:class:`attr.Factory` actually return objects, but the
easier. :py:func:`attrs.field` and :py:class:`attrs.Factory` actually return objects, but the
annotation says these return the types that they expect to be assigned to.
That enables this to work:

.. code-block:: python

import attr
from typing import Dict
import attrs

@attr.s(auto_attribs=True)
@attrs.define
class A:
one: int = attr.ib(8)
two: Dict[str, str] = attr.Factory(dict)
bad: str = attr.ib(16) # Error: can't assign int to str
one: int = attrs.field(8)
two: dict[str, str] = attrs.Factory(dict)
bad: str = attrs.field(16) # Error: can't assign int to str

Caveats/Known Issues
====================

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

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

.. code-block:: python

import attr
import attrs
YES = True
@attr.s(init=YES)
@attrs.define(init=YES)
class A:
...

Expand Down
20 changes: 10 additions & 10 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -1305,32 +1305,32 @@ main:3: error: Function is missing a type annotation for one or more arguments

[case testDisallowIncompleteDefsAttrsNoAnnotations]
# flags: --disallow-incomplete-defs
import attr
import attrs

@attr.s()
@attrs.define
class Unannotated:
foo = attr.ib()
foo = attrs.field()

[builtins fixtures/plugin_attrs.pyi]

[case testDisallowIncompleteDefsAttrsWithAnnotations]
# flags: --disallow-incomplete-defs
import attr
import attrs

@attr.s()
@attrs.define
class Annotated:
bar: int = attr.ib()
bar: int = attrs.field()

[builtins fixtures/plugin_attrs.pyi]

[case testDisallowIncompleteDefsAttrsPartialAnnotations]
# flags: --disallow-incomplete-defs
import attr
import attrs

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

[builtins fixtures/plugin_attrs.pyi]

Expand Down
Loading