-
Notifications
You must be signed in to change notification settings - Fork 42
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:
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