Skip to content

Feat: Allow direct parameter access in ParametricFamilyDistribution via __getattr__` #119

Description

@iraedeus

Description:

Currently, accessing the parameters of an instantiated ParametricFamilyDistribution is quite verbose. Users have to explicitly route their calls through the parametrization object or dictionary:

dist = normal_family.distribution(mu=0.0, sigma=1.0)

# Current behavior
val = dist.parametrization.mu
# or
val = dist.parameters["mu"]

Because of this lack of syntactic sugar, downstream projects (such as pysatl-mpest) are forced to implement thick wrapper classes just to provide a clean, object-oriented API like dist.mu. This leads to code duplication, synchronization issues, and architectural debt.

I propose adding a magic __getattr__ method directly to the ParametricFamilyDistribution class in pysatl_core/families/distribution.py. This method would automatically delegate unresolved attribute lookups to the internal _parametrization dataclass.

Proposed implementation:

def __getattr__(self, name: str) -> Any:
    """
    Delegate attribute access to the underlying parametrization.
    Allows writing `dist.mu` instead of `dist.parametrization.mu`.
    """
    if hasattr(self._parametrization, name):
        return getattr(self._parametrization, name)
    
    raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")

With this change, the API becomes much cleaner:

val = dist.mu
val = dist.sigma

Describe alternatives you've considered
Keeping the ParametricAdapter wrapper in pysatl-mpest. However, this requires manually maintaining duplicate distributions (e.g., Normal, Uniform) and hiding the pysatl-core API from the end user.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions