Skip to content

Instantly share code, notes, and snippets.

@gidgid
Created February 21, 2021 20:23
Show Gist options
  • Save gidgid/73daa9b2af99e9f20a73108737348280 to your computer and use it in GitHub Desktop.
Save gidgid/73daa9b2af99e9f20a73108737348280 to your computer and use it in GitHub Desktop.
shows a short recap of how to read different values by an env variables with Pydantic
from typing import Union
from pydantic import BaseSettings, parse_obj_as
from typing_extensions import Literal
class LocalContext(BaseSettings):
env: Literal["local"] # 1
mongo_url: str
class ProdContext(BaseSettings):
env: Literal["prod"] # 1
mongo_url: str
mongo_replicaset: str # 2
Context = Union[LocalContext, ProdContext] # 3
context = parse_obj_as(Context, {}) # 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment