espnet3.contract.data.check_fields
Less than 1 minute
espnet3.contract.data.check_fields
espnet3.contract.data.check_fields(obj, attr: str) → Tuple[Field, ...] | None
Return obj.<attr> if well-formed, or None if undeclared.
A dataset’s fields is one standalone tuple: a tuple of Field, non-empty, with distinct names. None lets a caller decide whether an undeclared attr is allowed, or not (call require_fields()).
- Parameters:
- obj – The class to check, or an instance whose own attributes (set in its
__init__) declare a contract its class does not. - attr – The attribute name, such as
"fields".
- obj – The class to check, or an instance whose own attributes (set in its
- Returns: The declared fields, or
Nonewhenobjdoes not declareattr. - Raises:TypeError –
obj.<attr>is declared but malformed.
Examples
>>> class Good:
... fields = (Field("speech", "audio"), Field("text", "text"))
>>> check_fields(Good, "fields")[0].name
'speech'
>>> class Bad:
... fields = (Field("text", "text"), Field("text", "text"))
>>> check_fields(Bad, "fields")
Traceback (most recent call last):
TypeError: Bad.fields repeats a name: ['text', 'text']