A record describing a field in a struct type. Parameters ---------- name: str The field name as seen by Python code (e.g. ``field_one``). encode_name: str The name used when encoding/decoding the field. This may differ if the field is renamed (e.g. ``fieldOne
| 30 | |
| 31 | |
| 32 | class FieldInfo(Struct): |
| 33 | """A record describing a field in a struct type. |
| 34 | |
| 35 | Parameters |
| 36 | ---------- |
| 37 | name: str |
| 38 | The field name as seen by Python code (e.g. ``field_one``). |
| 39 | encode_name: str |
| 40 | The name used when encoding/decoding the field. This may differ if |
| 41 | the field is renamed (e.g. ``fieldOne``). |
| 42 | type: Any |
| 43 | The full field type annotation. |
| 44 | default: Any, optional |
| 45 | A default value for the field. Will be `NODEFAULT` if no default value |
| 46 | is set. |
| 47 | default_factory: Any, optional |
| 48 | A callable that creates a default value for the field. Will be |
| 49 | `NODEFAULT` if no ``default_factory`` is set. |
| 50 | """ |
| 51 | |
| 52 | name: str |
| 53 | encode_name: str |
| 54 | type: Any |
| 55 | default: Any = field(default_factory=lambda: NODEFAULT) |
| 56 | default_factory: Any = field(default_factory=lambda: NODEFAULT) |
| 57 | |
| 58 | @property |
| 59 | def required(self) -> bool: |
| 60 | """A helper for checking whether a field is required""" |
| 61 | return self.default is NODEFAULT and self.default_factory is NODEFAULT |
| 62 | |
| 63 | |
| 64 | def fields(type_or_instance: Struct | type[Struct]) -> tuple[FieldInfo]: |