Overview
attrs 22.2, released on December 21, 2022, adds the alias option for renaming fields at initialization.
Main Features
Field alias
The alias option defines an alternative name for the constructor parameter, useful for JSON APIs or Python reserved names.
python
import attrs
@attrs.define
class Item:
name: str
class_: str = attrs.field(alias='class')
# Using the alias name
item = Item(name='widget', **{'class': 'premium'})
print(item.class_) # 'premium'
Internal improvements
Internal code now uses slots by default with attrs.define, improving performance and reducing memory usage.
python
import attrs
# attrs.define uses slots by default
@attrs.define
class Coord:
x: float
y: float
z: float = 0.0
c = Coord(1.0, 2.0)
print(c.__slots__) # ('x', 'y', 'z')
