您正在使用
nested settings
,因此您需要正确设置:
class WebhookSettings(BaseModel):
run_type: Literal["webhook"]
webhook_url: str
webhook_path: str
class PollingSettings(BaseModel):
run_type: Literal["polling"]
polling_interval: int
class RunSettings(BaseSettings):
model_config = SettingsConfigDict(env_nested_delimiter="__")
run: Annotated[Union[WebhookSettings, PollingSettings], Field(discriminator="run_type")]
对于webhook设置:
os.environ["run__run_type"] = "webhook"
os.environ["run__webhook_url"] = "http://test.com"
os.environ["run__webhook_path"] = "v1/tests"
settings = RunSettings()
print(settings.model_dump())
# {'run': {'run_type': 'webhook', 'webhook_url': 'http://test.com', 'webhook_path': 'v1/tests'}}
轮询设置:
os.environ["run__run_type"] = "polling"
os.environ["run__polling_interval"] = "10"
settings = RunSettings()
print(settings.model_dump())
# {'run': {'run_type': 'polling', 'polling_interval': 10}}