Skip to content

Commit

Permalink
Allow extra fields in the config
Browse files Browse the repository at this point in the history
Right now we cannot add extra fields to the config without it not being
backwards compatible.  Should follow the rules of protobuf here where
you can add new fields but they must be optional.
  • Loading branch information
joelynch committed Oct 4, 2023
1 parent d08f0dc commit f8f0559
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions astacus/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
from contextlib import contextmanager
from multiprocessing.dummy import Pool # fastapi + fork = bad idea
from pathlib import Path
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from typing import Any, AsyncIterable, Callable, Dict, Final, Iterable, Optional, TypeVar, Union

import asyncio
import datetime
import httpcore
import httpx
import json as _json
import logging
import os
import re
Expand All @@ -39,12 +39,6 @@ class Config:
# enumname.value (it is also slightly less safe but oh well)
use_enum_values = True

# Extra values should be errors, as they are most likely typos
# which lead to grief when not detected. However, if we ever
# start deprecating some old fields and not wanting to parse
# them, this might need to be revisited.
extra = "forbid"

# Validate field default values too
validate_all = True

Expand All @@ -54,16 +48,8 @@ class Config:
# possibly the tests themselves are broken

def jsondict(self, **kw):
# By default,
#
# .json() returns json string.
# .dict() returns Python dict, but it has things that are not
# json serializable.
#
# We provide json seralizable dict (super inefficiently) here.
#
# This is mostly used for test code so that should be fine
return _json.loads(self.json(**kw))
"""Serialize as a json encodable dict, eg datetimes become strings."""
return jsonable_encoder(self.dict(**kw))


def get_or_create_state(*, state: object, key: str, factory: Callable[[], Any]) -> Any:
Expand Down

0 comments on commit f8f0559

Please sign in to comment.