Exemple #1
0
class DummyInstance(InstanceBase):
    """
    Dummy instance class to be used in unit tests.

    """

    instance_id: str
    name: str

    input_path: str
    output_path: str

    user: str = field(metadata={
        **DataclassHookMixin.get_metadata(frozen_location_hook),
        **InstanceBaseMetadata.IMMUTABLE.value,
    }, )
    location: str = mutable_field()

    status: str = field(
        default="start",
        metadata={
            **DataclassHookMixin.get_metadata(frozen_input_hook, frozen_output_hook),
            **InstanceBaseMetadata.MUTABLE.value,
        },
    )

    def get_instance_id(self) -> str:
        return self.instance_id
Exemple #2
0
class DummyInstance(InstanceBase):
    """
    Dummy instance class to be used in unit tests.

    """

    instance_id: str
    name: str

    number: int = field(
        metadata=DataclassHookMixin.get_metadata(number_range_hook))

    # we can specify mutability
    # each field will be mutable as default
    counter: int = field(
        metadata={
            **InstanceBaseMetadata.MUTABLE.value,
            **DataclassHookMixin.get_metadata(counter_range_hook),
        })

    pressure: int = field(
        metadata={
            **InstanceBaseMetadata.MUTABLE.value,
            **DataclassHookMixin.get_metadata(pressure_range_hook),
        })

    def get_instance_id(self) -> str:
        return self.instance_id
    def _freeze_field(self, instance: T, field_name: str) -> None:
        """
        This function will freeze the field with field_name in object: instance
        """
        # pyre-ignore Undefined attribute [16]: instance has no attribute __dataclass_fields__
        field_obj = instance.__dataclass_fields__[field_name]

        # if this field is mutable now
        if not field_obj.metadata.get(IS_FROZEN_FIELD, False):
            # get the hooks of this field
            hooks: Iterable[HookEventType] = field_obj.metadata.get(
                DataclassHookMixin.HOOK_METADATA_STR, None
            )

            if hooks is None:
                # No hooks, so just set the metadata as immutable
                field_obj.metadata = InstanceBaseMetadata.IMMUTABLE.value
            else:
                # if this field has hooks
                # We want to set the metadata as immutable and keep a record
                # of the existing hooks (field metadata can't be updated because
                # it is a "mappingproxy" object, not a real dict)
                field_obj.metadata = {
                    **InstanceBaseMetadata.IMMUTABLE.value,
                    **DataclassHookMixin.get_metadata(hooks),
                }
class DummyInstance(InstanceBase):
    """
    Dummy instance class to be used in unit tests.

    """

    instance_id: str = field(
        metadata={
            **DataclassHookMixin.get_metadata(name_update_hook),
            **InstanceBaseMetadata.MUTABLE.value,
        })

    input_path: str = field(
        metadata={
            **DataclassHookMixin.get_metadata(ouput_update_hook, storage_update_hook),
            **InstanceBaseMetadata.MUTABLE.value,
        })

    name: str = mutable_field(init=False)
    output_path: str = mutable_field(init=False)
    storage: str = mutable_field(init=False)

    def get_instance_id(self) -> str:
        return self.instance_id
class DummyInstance(InstanceBase):
    """
    Dummy instance class to be used in unit tests.

    """

    instance_id: str
    name: str

    input_path: str
    output_path: str

    pressure: int = field(
        metadata=DataclassHookMixin.get_metadata(highest_pressure_hook), )

    highest_pressure: int = field(init=False)

    def get_instance_id(self) -> str:
        return self.instance_id
 def __setattr__(self, name: str, value: Any) -> None:
     # if setattr is called after initialization
     if self.initialized:
         # if we cannot find it, this field has not been initialized yet
         try:
             self.__getattribute__(name)
         except AttributeError:
             DataclassHookMixin.__setattr__(self, name, value)
         else:
             # if this field has been initialized and it is immutable
             # pyre-fixme Undefined attribute [16]: InstanceBase has no attribute __dataclass_fields__
             if self.__dataclass_fields__[name].metadata.get(
                     IS_FROZEN_FIELD, False):
                 raise InstanceFrozenFieldError(name)
             else:
                 DataclassHookMixin.__setattr__(self, name, value)
     else:
         # if setattr is called during initialization
         DataclassHookMixin.__setattr__(self, name, value)
class DummyInstance(InstanceBase):
    """
    Dummy instance class to be used in unit tests.

    """

    instance_id: str = field(
        metadata={
            **DataclassHookMixin.get_metadata(name_init_hook),
            **InstanceBaseMetadata.IMMUTABLE.value,
        })

    input_path: str = field(
        metadata={
            **DataclassHookMixin.get_metadata(ouput_update_hook, storage_update_hook),
            **InstanceBaseMetadata.MUTABLE.value,
        })

    user: str = field(
        metadata={
            **InstanceBaseMetadata.IMMUTABLE.value,
            **DataclassHookMixin.get_metadata(org_init_hook),
        })
    owner: Optional[str] = immutable_field()
    region: Optional[str] = field(metadata={
        **InstanceBaseMetadata.MUTABLE.value,
        **DataclassHookMixin.get_metadata(frozen_location_hook),
    }, )
    location: Optional[str] = mutable_field()

    # an attribute is mutable by default
    number: int = field(
        metadata=DataclassHookMixin.get_metadata(number_range_hook))

    counter: int = field(
        metadata={
            **InstanceBaseMetadata.MUTABLE.value,
            **DataclassHookMixin.get_metadata(counter_range_hook),
        })

    pressure: int = field(
        metadata={
            **InstanceBaseMetadata.MUTABLE.value,
            **DataclassHookMixin.get_metadata(pressure_range_hook, highest_pressure_hook),
        })

    priority: int = field(
        default=1,
        metadata={
            **InstanceBaseMetadata.IMMUTABLE.value,
            **DataclassHookMixin.get_metadata(priority_range_hook),
        },
    )

    status: str = field(
        default="start",
        metadata={
            **DataclassHookMixin.get_metadata(frozen_input_hook, frozen_output_hook),
            **InstanceBaseMetadata.MUTABLE.value,
        },
    )

    name: str = immutable_field(init=False, )
    org: str = immutable_field(init=False)
    output_path: str = mutable_field(init=False)
    storage: str = mutable_field(init=False)
    highest_pressure: int = mutable_field(init=False)

    def get_instance_id(self) -> str:
        return self.instance_id