fix(parts): set field defaults using default_factory
Metadata
Current evaluation
Merged #629 as 5529031d, replacing mutable PartSpec defaults with Pydantic default_factory to fix PyRight errors for PartSpec(plugin="foo"); backported via #640 and #641.
Resolution: closed resolved
Reason: PR #629 was approved and merged as commit 5529031d. The fix was also backported through PRs #640 and #641.
Evaluated by: mr-cal-bot
No scores available.
Issue body
- [x] Have you signed the [CLA](http://www.ubuntu.com/legal/contributors/)?
-----
I was getting some PyRight errors when trying to create a `PartSpec` with just a `plugin` value:
```python3
foo = PartSpec(plugin="foo")
```
PyRight gives me this error:
```
error: Arguments missing for parameters "organize", "overlay", "stage", "prime" (reportCallIssue)
```
It could also be PyRight misinterpreting something, but using `default_value` instead of `default` for mutable types seems like a good idea anyway, and as a side effect seems to silence PyRight's error report.
Conceptually, the `pydantic.Field`'s `default` vs `default_factory` have the same semantic as Python's own [`dataclasses.field`](https://docs.python.org/3/library/dataclasses.html#dataclasses.field) class, which says about `default_type`:
> Among other purposes, this can be used to specify fields with mutable default values, as discussed below.
Installed versions:
- Python 3.11.6
- via pip: craft-cli 2.5.0
- via pip: craft-parts 1.20.0
- via pip: craft-providers 1.12.0
- via pip: pydantic 1.10.12
- via pip: pydantic_core 2.14.6
- via pip: pydantic-yaml 0.11.2
- via snap: pyright 1.1.348 746 latest/stable alexmurray* classic
This might interact with #601.
See also: https://github.com/canonical/craft-parts/pull/601/files#r1463405279
Evaluation history
| Date | Model | Scores | Action | Summary |
|---|---|---|---|---|
| stealth/space-bunny-alpha | — | closed resolved | Merged #629 as 5529031d, replacing mutable PartSpec defaults with Pydantic default_factory to fix PyRight errors for PartSpec(plugin="foo"); backported via #640 and #641. | |
| stealth/space-bunny-alpha | — | closed resolved | Merged in #629 (5529031d), switching mutable PartSpec defaults to Pydantic default_factory and fixing PyRight errors for PartSpec(plugin="foo"). Backports also merged in #640 and #641. | |
| stealth/space-bunny-alpha | — | closed resolved | Merged PR #629 (commit 5529031d), replacing mutable Pydantic defaults with default_factory so PartSpec can be initialized with only plugin without PyRight errors. | |
| qwen/qwen3.6-35b-a3b | — | — | Merged a fix replacing Pydantic field defaults with default_factory for mutable types, resolving PyRight errors during PartSpec initialization. Approved by two reviewers and passed CI checks. | |
| qwen3.6-35b-a3b-mtp-q6 | — | — | Merged a fix replacing Pydantic field defaults with default_factory for mutable types in PartSpec. This resolves PyRight type-checking errors and aligns with Python dataclass best practices. |
Update history
No update history recorded yet.
Related work
-
Related To:
canonical/craft-parts#640
(confidence 99%)
Backport of #629, committed as 6b05a101.
-
Related To:
canonical/craft-parts#641
(confidence 99%)
Backport of #629, committed as ef9bbef6.
Related issues
| Issue | Project | State | Summary | Similarity |
|---|---|---|---|---|
| #641 fix(parts): set field defaults using default_factory (#629) | craft-parts | merged | Merged backport #641 as commit ef9bbef6, applying #629 to give mutable PartSpec fields safe Pydantic default_factory defaults and fix PyRight errors. | |
| #640 fix(parts): set field defaults using default_factory (#629) | craft-parts | merged | Merged as 6b05a101, cherry-picking #629 onto feature/namespaced-partitions to use default_factory for mutable PartSpec defaults, with Black 2024 formatting and a pytest 8 integration-test fix. |