Skip to main content

pydantic-config-parser

This library provides special utility when using pydantic to parse config file.

Features ✨

  • Override ONLY specified fields in config file.
  • Parse YAML string to JSON object utility.

Override only specified fields in config file

Example 1

from pydantic import BaseModel
from pydantic_config_parser import recursive_override_each_fields

class ConfigRoot(BaseModel):
    a: int = 0
    b: str = "default"

override_json_s = """
{
  "a": 1
}
"""

config = recursive_override_each_fields(ConfigRoot(), json_loads_as_dict(override_json_s))
config.model_dump_json()

# {
#   "a": 1,           # <- override
#   "b": "default"
# }

Example 2: Override sub model

from pydantic import BaseModel
from pydantic_config_parser import recursive_override_each_fields

class ConfigSub(BaseModel):
    a: int = 0
    b: str = "default"

class ConfigRoot(BaseModel):
    a: int = 0
    b: str = "default"
    c: list[int] = [1, 2, 3]
    d: dict[str, int] = {"a": 1, "b": 2}
    e: ConfigSub = ConfigSub()

override_json_s = """
{
  "e": {
    "a": 2
  }
}
"""

config = recursive_override_each_fields(ConfigRoot(), json_loads_as_dict(override_json_s))
config.model_dump_json()

# {
#   "a": 0,
#   "b": "default",
#   "c": [1, 2, 3],
#   "d": {"a": 1, "b": 2},
#   "e": {
#     "a": 2,             # <- override
#     "b": "default"
#   }
# }

Parse YAML utility

If you want to parse YAML string to JSON object, you can use yaml_loads_as_dict.

pip install pydantic-config-parser[yaml]

This will install PyYAML as a dependency.

from pydantic_config_parser import yaml_loads_as_json

class ConfigRoot(BaseModel):
    a: int = 0
    b: str = "default"


yaml_s = """
b: test
"""

dict_s = yaml_loads_as_json(yaml_s) # YAML feature is supported

config = recursive_override_each_fields(ConfigRoot(), dict_s)
config.model_dump_json()

# {
#   "a": 0,
#   "b": "test"  # <- override
# }

Metadata

Release files for pydantic-config-parser 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-config-parser 0.1.0
File Size Uploaded
pydantic_config_parser-0.1.0.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-config-parser 0.1.0
File Interpreter ABI Platform
pydantic_config_parser-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / pydantic_config_parser-0.1.0.tar.gz

Download URL pydantic_config_parser-0.1.0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bf297c97eb861173a370da0f458ad79513e6248c6b0df3fa0cb06b597a028ea3
BLAKE2b-256 checksum
How to use checksums
3ad4ced70502bb3f070b99ad873029a4a3d761cab3126cf546fe2d1256b3fd07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2024.

Transparency log

Release files / pydantic_config_parser-0.1.0-py3-none-any.whl

Download URL pydantic_config_parser-0.1.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
511fa42517fe1d6592cb7ba30b6c64a481831f415ad85595a671e82606c3915b
BLAKE2b-256 checksum
How to use checksums
7f00185bcf45b796b0ff79e1e5d5db69820bda1aac6821ebe88b1457b7bf0aba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page