Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config yaml schema support #304

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/config_files/.schemas/Bob_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"properties": {
"foo": {
"default": 123,
"title": "Foo",
"type": "integer",
"description": "A very important field."
}
},
"title": "Bob",
"type": "object",
"description": "Some docstring."
}
35 changes: 35 additions & 0 deletions examples/config_files/.schemas/Nested_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$defs": {
"Bob": {
"properties": {
"foo": {
"default": 123,
"title": "Foo",
"type": "integer",
"description": "A very important field."
}
},
"title": "Bob",
"type": "object",
"description": "Some docstring."
}
},
"properties": {
"bob": {
"$ref": "#/$defs/Bob",
"description": "bobobobo."
},
"other_field": {
"title": "Other Field",
"type": "string",
"description": "This is a docstring for the other field."
}
},
"required": [
"bob",
"other_field"
],
"title": "Nested",
"type": "object",
"description": "Some docstring of the 'Nested' class."
}
2 changes: 2 additions & 0 deletions examples/config_files/bob_with_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# yaml-language-server: $schema=.schemas/Bob_schema.json
foo: 222
4 changes: 4 additions & 0 deletions examples/config_files/nested_with_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# yaml-language-server: $schema=.schemas/Nested_schema.json
bob:
foo: 222
other_field: babab
35 changes: 35 additions & 0 deletions examples/config_files/schema_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from dataclasses import dataclass
from pathlib import Path

from simple_parsing.helpers.serialization.yaml_schema import save_yaml_with_schema


@dataclass
class Bob:
"""Some docstring."""

foo: int = 123
"""A very important field."""


@dataclass
class Nested:
"""Some docstring of the 'Nested' class."""

bob: Bob # inline comment for field `bob` of class `Nested`
"""bobobobo."""

other_field: str # inline comment for `other_field` of class `Nested`
"""This is a docstring for the other field."""


if __name__ == "__main__":
save_yaml_with_schema(
Bob(foo=222),
Path(__file__).parent / "bob_with_schema.yaml",
)

save_yaml_with_schema(
Nested(bob=Bob(foo=222), other_field="babab"),
Path(__file__).parent / "nested_with_schema.yaml",
)
128 changes: 127 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typing-extensions = ">=4.5.0"
pyyaml = {version = "^6.0.1", optional = true}
tomli = {version = "^2.0.1", optional = true}
tomli-w = {version = "^1.0.0", optional = true}
pydantic = {version = "^2.6.1", optional = true}

[tool.poetry.group.dev.dependencies]
pytest = "^8.0.0"
Expand All @@ -26,6 +27,7 @@ pytest-benchmark = "^4.0.0"
[tool.poetry.extras]
yaml = ["pyyaml"]
toml = ["tomli", "tomli_w"]
pydantic = ["pydantic"]


[tool.poetry-dynamic-versioning]
Expand Down
35 changes: 32 additions & 3 deletions simple_parsing/helpers/serialization/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .decoding import *
from .encoding import *
from .decoding import decode_field, get_decoding_fn, register_decoding_fn
from .encoding import SimpleJsonEncoder, encode
from .serializable import (
FrozenSerializable,
Serializable,
Expand All @@ -19,9 +19,38 @@
save_yaml,
to_dict,
)
from .yaml_schema import save_yaml_with_schema

JsonSerializable = Serializable

try:
from .yaml_serialization import YamlSerializable
except ImportError:
pass
JsonSerializable = Serializable

__all__ = [
"JsonSerializable",
"get_decoding_fn",
"register_decoding_fn",
"decode_field",
"SimpleJsonEncoder",
"encode",
"FrozenSerializable",
"Serializable",
"SerializableMixin",
"dump",
"dump_json",
"dump_yaml",
"dumps",
"dumps_json",
"dumps_yaml",
"from_dict",
"load",
"load_json",
"load_yaml",
"save",
"save_json",
"save_yaml",
"to_dict",
"save_yaml_with_schema",
]