Vue d'ensemble

attrs 21.3, publié le 28 décembre 2021, introduit import attrs comme nouveau namespace et ajoute le support de match_args pour le pattern matching.

Fonctionnalités principales

Namespace attrs

Le nouveau namespace import attrs avec attrs.define remplace l'ancien @attr.s, offrant une API plus moderne.

python
import attrs

@attrs.define
class Point:
    x: float
    y: float

p = Point(1.0, 2.0)
print(p)  # Point(x=1.0, y=2.0)

match_args

Le support de __match_args__ permet d'utiliser les classes attrs avec le pattern matching de Python 3.10+.

python
import attrs

@attrs.define
class Command:
    action: str
    target: str

cmd = Command('move', 'north')

match cmd:
    case Command('move', direction):
        print(f'Moving {direction}')
    case Command('look', _):
        print('Looking around')

Sources