Skip to content

Map[K]V

PyGo provides first-class support for generic map types using the Map[K, V] syntax. Maps are type-safe at compile time and mechanically transpiled to idiomatic Go map[K]V and Python dict[K, V] types.

model Config:
app_name: String
settings: Map[String, String] # Map of string keys to string values
thresholds: Map[String, Int] # Map of string keys to int values
model User:
name: String
model Team:
name: String
members: Map[String, User] # Map of user IDs to User objects
model MultiTenantConfig:
data: Map[String, Map[String, String]] # 2D map of strings

Maps are validated at compile time:

config = Config(
app_name="MyApp",
settings={"theme": "dark", "lang": "en"},
thresholds={"max_retries": 3, "timeout": 30}
)
# Valid — accessing a known key type
theme = config.settings["theme"]
# Invalid — the transpiler rejects this at compile time
# config.settings[123] # Error: 123 is not String

Go:

type Config struct {
AppName string
Settings map[string]string
Thresholds map[string]int
}

Python:

from typing import Dict
class Config(BaseModel):
app_name: str
settings: Dict[str, str]
thresholds: Dict[str, int]

Maps are stored in the database as follows:

  • SQLite: JSON-encoded as TEXT
  • PostgreSQL: JSONB column type
CREATE TABLE config (
id INTEGER PRIMARY KEY,
app_name TEXT NOT NULL,
settings TEXT, -- JSON: {"theme": "dark", "lang": "en"}
thresholds TEXT -- JSON: {"max_retries": 3, "timeout": 30}
);

PyGo supports the following operations on maps:

config.settings["theme"] = "light"
theme = config.settings.get("theme", "dark")
del config.settings["theme"]
if "theme" in config.settings:
print(config.settings["theme"])
keys = config.settings.keys() # ["theme", "lang"]
values = config.settings.values() # ["light", "en"]
items = config.settings.items() # [("theme", "light"), ("lang", "en")]

Use Map[K, V]? for nullable maps:

model Config:
app_name: String
settings: Map[String, String]? = None # Can be NULL
  • Use Map[String, String] for flexible key-value configuration
  • Use Map[String, Model] for lookup tables and indexed collections
  • Use Map[String, Map[String, V]] sparingly — consider flattening the structure
  • Always provide default empty maps to avoid null issues