Skip to content

Warn about mutable defaults #302

@rwb27

Description

@rwb27

Is your feature request related to a problem? Please describe.
If a property is created with a mutable default (e.g. myprop: dict = lt.property(default={"foo":"bar"})) then it's easy for Things to share state unintentionally.

Describe the solution you'd like
If a default value is set to a mutable object (e.g. a dict or list) it should warn about this and suggest using a default factory instead.

Describe alternatives you've considered
Automatically calling deepcopy() would be another potential solution, but I think it exposes us to the possibility of hard-to-debug edge cases, rather than just encouraging accepted best practice (default factories) in downstream code.

Additional context
This caused some head scratching in OFM#750 and OFM#751.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions