0

I want to have typed settings and came across this answer and TypedDict but could not get it working with nested structures.

Assuming the following dict, how can I best add typing?

{
    "global": {
        "region": "eu-central-1",
    },
    "env": {
        "dev": {
            "name": "dev.local",
        },
        "prod": {
            "name": "prod.world",
        }
    }
}

P.S.: I would also be interested, how to do that with data classes, if you have the answer?

lony
  • 6,733
  • 11
  • 60
  • 92

1 Answers1

0

Try something like this:

from typing import TypedDict

class Env(TypedDict):
    name: str

class Region(TypedDict):
    region: str

class EnvDescr(TypedDict):
    global: Region
    dev: Env
    prod: Env
scottsome
  • 432
  • 1
  • 4
  • 5