Stone-Soup icon indicating copy to clipboard operation
Stone-Soup copied to clipboard

Added Property.MISSING value

Open gawebb-dstl opened this issue 4 months ago • 0 comments

Frequently in stonesoup a mutable default value is wanted. default=[] can't be used as the default value would be shared across multiple instances.

>>> class A(Base):
>>>     states: list = Property(default=[])

>>> a1 = A()
>>> a1.states.append(1)
>>> a2 = A()
>>> print(a2.states)  
[1]

Instead often the default value of None is used and then a list is create in __init__, for example StateMutableSequence.

    states: MutableSequence[State] = Property(default=None)

    def __init__(self, states=None, *args, **kwargs):
        if states is None:
            states = []

There may be times where the desired default value is a mutable object but None is also an acceptable value as well. For example:

class Example(Base):
        values: Optional[list] = Property(default=Property.MISSING, doc='List of values. `None` represents error generating values.')

        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            if self.values is Property.MISSING:
                self.values = list()

This pull request introduces Property.MISSING for default values that are mutable and where None isn't suitable.

gawebb-dstl avatar Oct 08 '24 12:10 gawebb-dstl