Skip to content

ParameterDefinition

A class to define a migration parameter.

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

    def __init__(
        self,
        name: str,
        type: str | ParameterType,
        default: str | float | int | None = None,
        description: str | None = None,
    ) -> 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.

        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 = psycopg.sql.Literal(default)
        self.description = description

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

    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
        )

__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
    )

__init__

__init__(name: str, type: str | ParameterType, default: str | float | int | None = None, description: str | None = None) -> 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

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,
) -> 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.

    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 = psycopg.sql.Literal(default)
    self.description = description

__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"Parameter({self.name}, type: {self.type}, default: {self.default})"