Refactor private attributes in Pydantic models to use PrivateAttr
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 99
- Forks
- 62
- Avg merge
- 6d 12h
- Merged PRs (30d)
- 17
Description
Description
In Pydantic v2, private attributes (those starting with underscore) should be declared using PrivateAttr() for stability and to follow the official API. While bare annotations like _attr: Type | None = None may work incidentally, they are not part of the guaranteed Pydantic API and can break with minor version updates.
Background
This issue was identified during review of PR #799:
- PR: https://github.com/NVIDIA/cloudai/pull/799
- Comment: https://github.com/NVIDIA/cloudai/pull/799#discussion_r2787752019
Affected Files
The following files contain Pydantic models with private attributes that should be refactored to use PrivateAttr:
Workloads (19 files)
src/cloudai/workloads/vllm/vllm.py-_docker_image,_hf_modelsrc/cloudai/workloads/ucc_test/ucc.py-_docker_imagesrc/cloudai/workloads/slurm_container/slurm_container.py-_docker_imagesrc/cloudai/workloads/nixl_perftest/nixl_perftest.py-_docker_imagesrc/cloudai/workloads/osu_bench/osu_bench.py-_osu_imagesrc/cloudai/workloads/triton_inference/triton_inference.py-_server_image,_client_imagesrc/cloudai/workloads/nemo_run/nemo_run.py-_docker_imagesrc/cloudai/workloads/nemo_launcher/nemo_launcher.py-_docker_image,_python_executablesrc/cloudai/workloads/nixl_kvbench/nixl_kvbench.py-_docker_imagesrc/cloudai/workloads/nixl_bench/nixl_bench.py-_nixl_imagesrc/cloudai/workloads/nccl_test/nccl.py-_docker_imagesrc/cloudai/workloads/jax_toolbox/nemotron.py-_docker_imagesrc/cloudai/workloads/jax_toolbox/grok.py-_docker_imagesrc/cloudai/workloads/jax_toolbox/gpt.py-_docker_imagesrc/cloudai/workloads/megatron_run/megatron_run.py-_docker_imagesrc/cloudai/workloads/deepep/deepep.py-_docker_imagesrc/cloudai/workloads/chakra_replay/chakra_replay.py-_docker_imagesrc/cloudai/workloads/ddlb/ddlb.py-_docker_imagesrc/cloudai/workloads/megatron_bridge/megatron_bridge.py-_docker_image,_python_executable,_megatron_bridge_reposrc/cloudai/workloads/ai_dynamo/ai_dynamo.py-_docker_image,_hf_model
Systems (1 file)
src/cloudai/systems/kubernetes/kubernetes_system.py-_core_v1,_batch_v1,_custom_objects_api,_genai_perf_completed
Recommended Changes
For each affected file:
-
Add
PrivateAttrto the pydantic imports:from pydantic import Field, PrivateAttr -
Replace bare annotations with
PrivateAttr:# Before _docker_image: DockerImage | None = None # After _docker_image: DockerImage | None = PrivateAttr(default=None)
Good Example
src/cloudai/systems/runai/runai_system.py already follows this pattern correctly:
from pydantic import Field, PrivateAttr
class RunAISystem(KubernetesSystem):
_api_client: Optional[RunAIRestClient] = PrivateAttr(default=None)
Note
src/cloudai/_core/installables.py uses dataclass field(), not Pydantic, so it doesn't need to be changed.
References
- Pydantic v2 Private Attributes Documentation: https://docs.pydantic.dev/latest/concepts/models/#private-model-attributes
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read src/cloudai/systems/runai/runai_system.py for the existing PrivateAttr pattern, then inspect the listed workload and Kubernetes model files. Replace each affected bare private-attribute annotation with PrivateAttr while preserving its default and type. Done means all listed Pydantic attributes use the official pattern and src/cloudai/_core/installables.py remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100