Skip to content

Commit

Permalink
🧪🩹 Make schema tests compatible with pydantic >= 2.9 AND < 2.9
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed Sep 26, 2024
1 parent 5632421 commit f8bce74
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 75 deletions.
1 change: 1 addition & 0 deletions .github/requirements_min.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cycler==0.10.0
docstring-parser==0.16
matplotlib==3.3.0
numpy==1.22.0
packaging==24.1
pydantic==2.7.0
pyglotaran==0.7.0
ruamel-yaml==0.18.6
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ repos:
rev: v4.0.0-alpha.8 # Use the sha or tag you want to point at
hooks:
- id: prettier
additional_dependencies: ["[email protected]"]

# Notebook tools
- repo: https://github.com/kynan/nbstripout
Expand Down
10 changes: 7 additions & 3 deletions pyglotaran_extras/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from typing import TYPE_CHECKING
from typing import Any

from packaging.version import Version
from pydantic import BaseModel
from pydantic import ConfigDict
from pydantic import PrivateAttr
from pydantic import PydanticUserError
from pydantic import __version__ as pydantic_version
from pydantic import create_model
from pydantic.fields import FieldInfo
from ruamel.yaml import YAML
Expand Down Expand Up @@ -447,9 +449,11 @@ def create_config_schema(
general_kwargs |= func_json_schema["$defs"][kwargs_model_name]["properties"]
json_schema["$defs"] |= func_json_schema.pop("$defs")
json_schema["$defs"][config_model_name] = func_json_schema
json_schema["$defs"]["PlotConfig"]["properties"][function_name] = {
"allOf": [{"$ref": f"#/$defs/{config_model_name}"}]
}
json_schema["$defs"]["PlotConfig"]["properties"][function_name] = (
{"$ref": f"#/$defs/{config_model_name}"}
if Version(pydantic_version) >= Version("2.9")
else {"allOf": [{"$ref": f"#/$defs/{config_model_name}"}]} # type:ignore[dict-item]
)
except PydanticUserError as error:
raise UsePlotConfigError(function_name, error) # noqa: B904
json_schema["$defs"]["PerFunctionPlotConfig"]["properties"]["default_args_override"][
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies = [
"docstring-parser>=0.16",
"matplotlib>=3.3",
"numpy>=1.22",
"packaging>=24.1",
"pydantic>=2.7",
"pyglotaran>=0.7",
"ruamel-yaml>=0.18.6",
Expand Down
13 changes: 7 additions & 6 deletions requirements_pinned.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Runtime dependencies

# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml -o requirements_pinned.txt --no-deps --no-annotate
cycler==0.12.1
numpy==1.26.4
docstring-parser==0.16
matplotlib==3.9.2
numpy==1.26.4
packaging==24.1
pydantic==2.9.2
pyglotaran==0.7.3
ruamel-yaml==0.18.6
tabulate==0.9.0
xarray==2024.7.0
ruamel.yaml==0.18.6
docstring_parser==0.16
pydantic==2.9.2
12 changes: 11 additions & 1 deletion tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import pytest
from jsonschema import Draft202012Validator
from packaging.version import Version
from pydantic import __version__ as pydantic_version
from ruamel.yaml import YAML

from pyglotaran_extras import create_config_schema
Expand Down Expand Up @@ -563,6 +565,8 @@ def test_func(
json_schema = json.loads(create_config_schema(tmp_path).read_text())
expected_schema = json.loads(
(TEST_DATA / f"config/{CONFIG_FILE_STEM}.schema.json").read_text()
if Version(pydantic_version) >= Version("2.9")
else (TEST_DATA / f"config/{CONFIG_FILE_STEM}.schema.pydantic_lt_2_9.json").read_text()
)

assert json_schema == expected_schema
Expand All @@ -585,7 +589,13 @@ def test_func(
pass

json_schema = json.loads(create_config_schema(tmp_path).read_text())
expected_schema = json.loads((TEST_DATA / "config/broken_config.schema.json").read_text())
expected_schema = (
json.loads((TEST_DATA / "config/broken_config.schema.json").read_text())
if Version(pydantic_version) >= Version("2.9")
else json.loads(
(TEST_DATA / "config/broken_config.schema.pydantic_lt_2_9.json").read_text()
)
)

assert json_schema == expected_schema

Expand Down
30 changes: 5 additions & 25 deletions tests/data/config/broken_config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,11 @@
"description": "Config for plot functions including default args and label overrides.",
"properties": {
"general": {
"allOf": [
{
"$ref": "#/$defs/PerFunctionPlotConfig"
}
],
"$ref": "#/$defs/PerFunctionPlotConfig",
"description": "Config that gets applied to all functions if not specified otherwise."
},
"test_func": {
"allOf": [
{
"$ref": "#/$defs/TestFuncConfig"
}
]
"$ref": "#/$defs/TestFuncConfig"
}
},
"title": "PlotConfig",
Expand Down Expand Up @@ -108,19 +100,11 @@
"description": "Plot function configuration specific to ``test_func`` (overrides values in general).",
"properties": {
"default_args_override": {
"allOf": [
{
"$ref": "#/$defs/TestFuncKwargs"
}
],
"$ref": "#/$defs/TestFuncKwargs",
"default": {}
},
"axis_label_override": {
"allOf": [
{
"$ref": "#/$defs/PlotLabelOverrideMap"
}
],
"$ref": "#/$defs/PlotLabelOverrideMap",
"default": {}
}
},
Expand All @@ -132,11 +116,7 @@
"description": "Main configuration class.",
"properties": {
"plotting": {
"allOf": [
{
"$ref": "#/$defs/PlotConfig"
}
],
"$ref": "#/$defs/PlotConfig",
"default": {
"general": {}
}
Expand Down
147 changes: 147 additions & 0 deletions tests/data/config/broken_config.schema.pydantic_lt_2_9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"$defs": {
"PerFunctionPlotConfig": {
"additionalProperties": false,
"description": "Per function plot configuration.",
"properties": {
"default_args_override": {
"description": "Default arguments to use if not specified in function call.",
"title": "Default Args Override",
"type": "object",
"properties": {
"will_update_arg": {
"default": "default update",
"title": "Will Update Arg"
}
},
"additionalProperties": false
},
"axis_label_override": {
"anyOf": [
{
"$ref": "#/$defs/PlotLabelOverrideMap"
},
{
"additionalProperties": {
"type": "string"
},
"type": "object"
}
],
"title": "Axis Label Override"
}
},
"title": "PerFunctionPlotConfig",
"type": "object"
},
"PlotConfig": {
"additionalProperties": true,
"description": "Config for plot functions including default args and label overrides.",
"properties": {
"general": {
"allOf": [
{
"$ref": "#/$defs/PerFunctionPlotConfig"
}
],
"description": "Config that gets applied to all functions if not specified otherwise."
},
"test_func": {
"allOf": [
{
"$ref": "#/$defs/TestFuncConfig"
}
]
}
},
"title": "PlotConfig",
"type": "object"
},
"PlotLabelOverrideMap": {
"additionalProperties": {
"anyOf": [
{
"$ref": "#/$defs/PlotLabelOverrideValue"
},
{
"type": "string"
}
]
},
"description": "Mapping to override axis labels.",
"title": "PlotLabelOverrideMap",
"type": "object"
},
"PlotLabelOverrideValue": {
"additionalProperties": false,
"description": "Value of ``PlotLabelOverrideMap``.",
"properties": {
"target_name": {
"title": "Target Name",
"type": "string"
},
"axis": {
"default": "both",
"enum": ["x", "y", "both"],
"title": "Axis",
"type": "string"
}
},
"required": ["target_name"],
"title": "PlotLabelOverrideValue",
"type": "object"
},
"TestFuncKwargs": {
"additionalProperties": false,
"description": "Default arguments to use for ``test_func``, if not specified in function call.",
"properties": {
"will_update_arg": {
"default": "default update",
"title": "Will Update Arg"
}
},
"title": "TestFuncKwargs",
"type": "object"
},
"TestFuncConfig": {
"additionalProperties": false,
"description": "Plot function configuration specific to ``test_func`` (overrides values in general).",
"properties": {
"default_args_override": {
"allOf": [
{
"$ref": "#/$defs/TestFuncKwargs"
}
],
"default": {}
},
"axis_label_override": {
"allOf": [
{
"$ref": "#/$defs/PlotLabelOverrideMap"
}
],
"default": {}
}
},
"title": "TestFuncConfig",
"type": "object"
}
},
"additionalProperties": false,
"description": "Main configuration class.",
"properties": {
"plotting": {
"allOf": [
{
"$ref": "#/$defs/PlotConfig"
}
],
"default": {
"general": {}
}
}
},
"title": "Config",
"type": "object"
}
48 changes: 8 additions & 40 deletions tests/data/config/pygta_config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,14 @@
"description": "Config for plot functions including default args and label overrides.",
"properties": {
"general": {
"allOf": [
{
"$ref": "#/$defs/PerFunctionPlotConfig"
}
],
"$ref": "#/$defs/PerFunctionPlotConfig",
"description": "Config that gets applied to all functions if not specified otherwise."
},
"other": {
"allOf": [
{
"$ref": "#/$defs/OtherConfig"
}
]
"$ref": "#/$defs/OtherConfig"
},
"test_func": {
"allOf": [
{
"$ref": "#/$defs/TestFuncConfig"
}
]
"$ref": "#/$defs/TestFuncConfig"
}
},
"title": "PlotConfig",
Expand Down Expand Up @@ -123,19 +111,11 @@
"description": "Plot function configuration specific to ``other`` (overrides values in general).",
"properties": {
"default_args_override": {
"allOf": [
{
"$ref": "#/$defs/OtherKwargs"
}
],
"$ref": "#/$defs/OtherKwargs",
"default": {}
},
"axis_label_override": {
"allOf": [
{
"$ref": "#/$defs/PlotLabelOverrideMap"
}
],
"$ref": "#/$defs/PlotLabelOverrideMap",
"default": {}
}
},
Expand Down Expand Up @@ -167,19 +147,11 @@
"description": "Plot function configuration specific to ``test_func`` (overrides values in general).",
"properties": {
"default_args_override": {
"allOf": [
{
"$ref": "#/$defs/TestFuncKwargs"
}
],
"$ref": "#/$defs/TestFuncKwargs",
"default": {}
},
"axis_label_override": {
"allOf": [
{
"$ref": "#/$defs/PlotLabelOverrideMap"
}
],
"$ref": "#/$defs/PlotLabelOverrideMap",
"default": {}
}
},
Expand All @@ -191,11 +163,7 @@
"description": "Main configuration class.",
"properties": {
"plotting": {
"allOf": [
{
"$ref": "#/$defs/PlotConfig"
}
],
"$ref": "#/$defs/PlotConfig",
"default": {
"general": {}
}
Expand Down
Loading

0 comments on commit f8bce74

Please sign in to comment.