Skip to content

utils.attrs

Danger

You probably don't want to touch these functions unless you are modifying the library itself.

Functions providing converters and validators for attrs classes.

attach_custom_repr

attach_custom_repr(cls)

Attach custom __repr__ and __rich_repr__ functions to an attrs class.

These functions will respect the repr attributes on individual attrs fields, but additionally: - Skip fields whose values are equal to their default values, or the repr_omit_if attribute, if set. - Omit field names if the repr_omits_field_name metadata attribute is set. - Print full_id instead of id for objects that have it (e.g., papers or volumes).

attach_parent

attach_parent(item, attr, value)

Attach parent object to a NameSpecification.

Intended to be called from on_setattr of an attrs.field.

auto_validate_types

auto_validate_types(cls, fields)

Add validators to attrs classes based on their type annotations.

Intended to be used with the field_transformer parameter of @attrs.define.

Supported type annotations
  • str, int
  • FileReference and derived classes
  • Name, NameSpecification
  • MarkupText
  • Optional[<type>]
  • list[<type>]
  • tuple[<type>, ...]
  • set[<type>]

The purpose of this function is to reduce the need for explicitly adding validators to the classes in acl_anthology.collections and acl_anthology.people.person. It does not automatically validate classes defined in acl_anthology.collections, as that would lead to circular imports.

See also: https://www.attrs.org/en/stable/extending.html#transform-fields

convert_orcid

convert_orcid(value)

Convert an object potentially representing an ORCID.

date_to_str

date_to_str(value)

Convert a date or datetime object to str (in ISO format), and leave unchanged otherwise.

Intended to be used as a converter for attrs.field.

int_to_str

int_to_str(value)

Convert an int to str, and leave unchanged otherwise.

Intended to be used as a converter for attrs.field.

repr_item_ids

repr_item_ids(ids)

Produce a repr for sequences of AnthologyIDTuples.

Will show a 'slice' of the first few IDs along with the sequence's length.

Intended to be set on the repr attribute of suitable attribute fields.

validate_anthology_id_tuple

validate_anthology_id_tuple(cls, attr, value)

Validate that an AnthologyIDTuple has the correct format.