types-attrs (Legacy Type Stubs for attrs)
types-attrs provides PEP 561 type stubs for the attrs library. However, the attrs library itself has included robust type annotations since version 18.2.0. Consequently, `types-attrs` is largely obsolete and should be uninstalled if you are using attrs version 18.2.0 or newer. It is an auto-generated package from the Python 'typeshed' repository and is considered deprecated for modern `attrs` usage.
Common errors
-
Mypy: error: Duplicate module 'attrs'
cause This error occurs when both the `attrs` library (version 18.2.0 or newer with built-in types) and the `types-attrs` stub package are installed. Mypy sees two sources of type information for the `attrs` module.fixUninstall the `types-attrs` package: `pip uninstall types-attrs`. Modern versions of `attrs` provide their own type annotations. -
Mypy: error: Cannot find module named 'attrs'
cause This error can occur if `attrs` is not installed, or if `types-attrs` was installed for an older `attrs` version but `attrs` itself is missing, or if Mypy is not configured to find installed packages.fixEnsure the `attrs` library is installed (`pip install attrs`). If using older `attrs` where `types-attrs` was necessary, ensure both are installed. Verify your Mypy configuration to include your project's installed packages. -
AttributeError: 'MyClass' object has no attribute 'some_field_name'
cause This typically happens when a field is declared with a type annotation but without `attrs.field()` (and `auto_attribs=True` is not set), making it a plain Python class attribute rather than an `attrs` managed attribute.fixTo make 'some_field_name' an `attrs` attribute, either define it using `some_field_name = field(type=str)` or, for modern `attrs`, use `@define(auto_attribs=True)` on the class and define it as `some_field_name: str`.
Warnings
- breaking The `types-attrs` package is obsolete for `attrs` versions 18.2.0 and newer. The `attrs` library itself ships with type annotations, making this stub package redundant and potentially problematic if both are installed.
- gotcha If you define a class with an `attrs.field()` that lacks a type annotation, `attrs` will ignore other fields that *do* have a type annotation but are *not* defined using `attrs.field()`.
- gotcha Defining an attribute with only a type annotation (e.g., `x: int = 10`) without `attrs.field()` does not make it an `attrs` attribute if other fields use `attrs.field()`. Such attributes will not participate in `attrs` features like `__init__`, `__repr__`, etc.
Install
-
pip install types-attrs
Imports
- define
from attrs import define
- field
from attrs import field
Quickstart
import attr
from attrs import define, field
from typing import List, ClassVar
@define
class User:
id: int = field(validator=attr.validators.instance_of(int))
name: str = field(default='Guest')
emails: List[str] = field(factory=list)
# Correct way to define a class variable with type annotation
CLASS_CONSTANT: ClassVar[str] = "SHARED_VALUE"
# Instantiate the class, type checkers will use annotations from `attrs`
user = User(id=123, name='Alice', emails=['alice@example.com'])
print(user)