Skip to content

Parameter definition

A class to define a migration parameter.

Version Added

1.0.0

Source code in pum/parameter.py
class ParameterDefinition:
    """A class to define a migration parameter.

    Version Added:
        1.0.0
    """

    def __init__(
        self,
        name: str,
        type: str | ParameterType,
        default: str | float | int | None = None,
        description: str | None = None,
        values: list | None = None,
        app_only: bool = False,
    ) -> None:
        """Initialize a ParameterDefintion instance.

        Args:
            name: The name of the parameter.
            type: The type of the parameter, as a string or ParameterType.
            default: The default value for the parameter. Defaults to None.
            description: A description of the parameter. Defaults to None.
            values: An optional list of allowed values for this parameter.
            app_only: If True, the parameter can be changed when recreating the app.
                Standard parameters (app_only=False) must remain the same across
                the whole application lifecycle. Defaults to False.

        Raises:
            ValueError: If type is a string and not a valid ParameterType.
            TypeError: If type is not a string or ParameterType.

        """
        self.name = name
        if isinstance(type, ParameterType):
            self.type = type
        elif isinstance(type, str):
            try:
                self.type = ParameterType(type)
            except ValueError:
                raise ValueError(f"Parameter '{name}' has an invalid type: {type}. ") from None
        else:
            raise TypeError("type must be a str or ParameterType")
        self.default = default
        self.description = description
        self.values = values
        self.app_only = app_only

    def __repr__(self) -> str:
        """Return a string representation of the ParameterDefinition instance."""
        return f"ParameterDefinition({self.name}, type: {self.type}, default: {self.default}, values: {self.values}, app_only: {self.app_only})"

    def __eq__(self, other: "ParameterDefinition") -> bool:
        """Check if two ParameterDefinition instances are equal."""
        if not isinstance(other, ParameterDefinition):
            return NotImplemented
        return (
            self.name == other.name
            and self.type == other.type
            and self.default == other.default
            and self.description == other.description
            and self.values == other.values
            and self.app_only == other.app_only
        )

__eq__

__eq__(other: ParameterDefinition) -> bool

Check if two ParameterDefinition instances are equal.

Source code in pum/parameter.py
def __eq__(self, other: "ParameterDefinition") -> bool:
    """Check if two ParameterDefinition instances are equal."""
    if not isinstance(other, ParameterDefinition):
        return NotImplemented
    return (
        self.name == other.name
        and self.type == other.type
        and self.default == other.default
        and self.description == other.description
        and self.values == other.values
        and self.app_only == other.app_only
    )

__init__

__init__(name: str, type: str | ParameterType, default: str | float | int | None = None, description: str | None = None, values: list | None = None, app_only: bool = False) -> None

Initialize a ParameterDefintion instance.

Parameters:

Name Type Description Default
name str

The name of the parameter.

required
type str | ParameterType

The type of the parameter, as a string or ParameterType.

required
default str | float | int | None

The default value for the parameter. Defaults to None.

None
description str | None

A description of the parameter. Defaults to None.

None
values list | None

An optional list of allowed values for this parameter.

None
app_only bool

If True, the parameter can be changed when recreating the app. Standard parameters (app_only=False) must remain the same across the whole application lifecycle. Defaults to False.

False

Raises:

Type Description
ValueError

If type is a string and not a valid ParameterType.

TypeError

If type is not a string or ParameterType.

Source code in pum/parameter.py
def __init__(
    self,
    name: str,
    type: str | ParameterType,
    default: str | float | int | None = None,
    description: str | None = None,
    values: list | None = None,
    app_only: bool = False,
) -> None:
    """Initialize a ParameterDefintion instance.

    Args:
        name: The name of the parameter.
        type: The type of the parameter, as a string or ParameterType.
        default: The default value for the parameter. Defaults to None.
        description: A description of the parameter. Defaults to None.
        values: An optional list of allowed values for this parameter.
        app_only: If True, the parameter can be changed when recreating the app.
            Standard parameters (app_only=False) must remain the same across
            the whole application lifecycle. Defaults to False.

    Raises:
        ValueError: If type is a string and not a valid ParameterType.
        TypeError: If type is not a string or ParameterType.

    """
    self.name = name
    if isinstance(type, ParameterType):
        self.type = type
    elif isinstance(type, str):
        try:
            self.type = ParameterType(type)
        except ValueError:
            raise ValueError(f"Parameter '{name}' has an invalid type: {type}. ") from None
    else:
        raise TypeError("type must be a str or ParameterType")
    self.default = default
    self.description = description
    self.values = values
    self.app_only = app_only

__repr__

__repr__() -> str

Return a string representation of the ParameterDefinition instance.

Source code in pum/parameter.py
def __repr__(self) -> str:
    """Return a string representation of the ParameterDefinition instance."""
    return f"ParameterDefinition({self.name}, type: {self.type}, default: {self.default}, values: {self.values}, app_only: {self.app_only})"