in pytorchvideo/data/utils.py [0:0]
def __post_init__(self) -> None:
f"""
This function is run by the dataclass library after '__init__'.
Here we use this to ensure all fields are casted to their declared types
and to apply any complex field_initializer functions that have been
declared via the 'complex_initialized_dataclass_field' method of
this class.
A complex field_initializer for a given field would be stored in the
field.metadata dictionary at:
key = '{self.COMPLEX_INITIALIZER}' (self.COMPLEX_INITIALIZER)
"""
for field in dataclass_fields(self):
value = getattr(self, field.name)
# First check if the datafield has been set to the declared type or
# if the datafield has a declared complex field_initializer.
if (
not isinstance(value, field.type)
or DataclassFieldCaster.COMPLEX_INITIALIZER in field.metadata
):
# Apply the complex field_initializer function for this field's value,
# assert that the resultant type is the declared type of the field.
if DataclassFieldCaster.COMPLEX_INITIALIZER in field.metadata:
setattr(
self,
field.name,
field.metadata[DataclassFieldCaster.COMPLEX_INITIALIZER](value),
)
assert isinstance(getattr(self, field.name), field.type), (
f"'field_initializer' function of {field.name} must return "
f"type {field.type} but returned type {type(getattr(self, field.name))}"
)
else:
# Otherwise attempt to cast the field's value to its declared type.
setattr(self, field.name, field.type(value))