Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Property.MISSING value #1087

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Added Property.MISSING value #1087

wants to merge 2 commits into from

Conversation

gawebb-dstl
Copy link
Contributor

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 gawebb-dstl force-pushed the missing_default_value branch from 4c762fd to 83be47e Compare October 8, 2024 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant