labthings / labthings/labthings-fastapi

Warn about mutable defaults

Open
#302 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
9
Forks
4
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the property implementation and the code that handles its default value; no specific file or test is named in the issue. Reproduce a property with a dict or list default, then add a warning that recommends a default factory and verify that factory-based defaults do not trigger it.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.