hmf.core.units.quantity_field

hmf.core.units.quantity_field(unit: UnitBase, *, doc: str, optional: bool = False, ndim: int | None = None, **kwargs: Any) → Any[source]

Define an attrs field (of a model or settings class) that carries a unit.

The field takes a Quantity in any unit convertible to unit without an H0 (models have no cosmology), and stores it as a read-only float Quantity in exactly unit, the canonical unit. So reading the field gives a Quantity, attrs.evolve round-trips it, and the field compares and hashes by its value in the canonical unit (hmf.core._serialise.quantity_key()). Kernels read the plain values through a private functools.cached_property() of the class.

Parameters:
  • unit – The canonical unit, one of the constants of this module (e.g. h_Mpc).

  • doc – The field’s documentation (see hmf.core._fields.field()).

  • optional – Whether None is accepted (and stored as None).

  • ndim – If given, the number of dimensions the value must have: 0 (a scalar) or 1 (a scalar becomes a 1-element array).

  • **kwargs – Passed on to attrs.field() (default, validator, …).

Returns:

The field definition.

Return type:

Any

Raises:
  • UnitBoundaryError – On construction, for a bare number or array (or None if not optional).

  • astropy.units.UnitConversionError – On construction, for a value in physical units, or not convertible to unit.

  • ValueError – On construction, for a value with the wrong number of dimensions.