79428445

Date: 2025-02-10 22:22:58
Score: 0.5
Natty:
Report link

Using validation_alias with a BaseSettings class does the trick!

class WorkflowRun(BaseSettings):
    id: str
    name: str
    node: str = Field(validation_alias=AliasChoices('node', 'ENV_NODE_POOL'))
    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        extra="ignore",
        env_ignore_empty=True,
        populate_by_name=True,
    )

Invocations:

>>> WorkflowRun(**{
    "id": "1",
    "name": "test",
})
WorkflowRun(id='1', name='test', node='default-node')

>>> WorkflowRun(**{
     "id": "1",
     "name": "test",
     "node": "new-pool"})
WorkflowRun(id='1', name='test', node='new-pool')
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: tonystark1234