Skip to content

Commit

Permalink
Merge pull request #3 from KNMI/strict-mode
Browse files Browse the repository at this point in the history
Enable Pydantic v2 Strict mode
  • Loading branch information
PaulVanSchayck authored Oct 17, 2023
2 parents 14fb3e8 + 152f167 commit 50d8d7f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ pip install git+https://github.com/KNMI/edr-pydantic.git

```python
from edr_pydantic.collections import Collection
from edr_pydantic.data_queries import EDRQuery, EDRQueryLink
from edr_pydantic.data_queries import EDRQuery, EDRQueryLink, DataQueries
from edr_pydantic.extent import Extent, Spatial
from edr_pydantic.link import Link
from edr_pydantic.observed_property import ObservedProperty
from edr_pydantic.parameter import Parameter
from edr_pydantic.parameter import Parameters, Parameter
from edr_pydantic.unit import Unit
from edr_pydantic.variables import Variables

Expand All @@ -64,8 +64,8 @@ c = Collection(
rel="service-doc"
)
],
data_queries={
'position': EDRQuery(
data_queries=DataQueries(
position=EDRQuery(
link=EDRQueryLink(
href="https://example.org/edr/collections/hrly_obs/position?coords={coords}",
rel="data",
Expand All @@ -77,8 +77,8 @@ c = Collection(
)
)
)
},
parameter_names={
),
parameter_names=Parameters({
"Wind Direction": Parameter(
unit=Unit(
label="degree true"
Expand All @@ -88,7 +88,7 @@ c = Collection(
label="Wind Direction"
)
)
}
})
)

print(c.model_dump_json(indent=2, exclude_none=True))
Expand Down
26 changes: 15 additions & 11 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from edr_pydantic.collections import Collection
from edr_pydantic.data_queries import DataQueries
from edr_pydantic.data_queries import EDRQuery
from edr_pydantic.data_queries import EDRQueryLink
from edr_pydantic.extent import Extent
from edr_pydantic.extent import Spatial
from edr_pydantic.link import Link
from edr_pydantic.observed_property import ObservedProperty
from edr_pydantic.parameter import Parameter
from edr_pydantic.parameter import Parameters
from edr_pydantic.unit import Unit
from edr_pydantic.variables import Variables

Expand All @@ -15,23 +17,25 @@
description="Observation data for UK observing sites",
extent=Extent(spatial=Spatial(bbox=[[-15.0, 48.0, 5.0, 62.0]], crs="WGS84")),
links=[Link(href="https://example.org/uk-hourly-site-specific-observations", rel="service-doc")],
data_queries={
"position": EDRQuery(
data_queries=DataQueries(
position=EDRQuery(
link=EDRQueryLink(
href="https://example.org/edr/collections/hrly_obs/position?coords={coords}",
rel="data",
variables=Variables(query_type="position", output_formats=["CoverageJSON"]),
)
)
},
parameter_names={
"Wind Direction": Parameter(
unit=Unit(label="degree true"),
observedProperty=ObservedProperty(
id="https://codes.wmo.int/common/quantity-kind/_windDirection", label="Wind Direction"
),
)
},
),
parameter_names=Parameters(
{
"Wind Direction": Parameter(
unit=Unit(label="degree true"),
observedProperty=ObservedProperty(
id="https://codes.wmo.int/common/quantity-kind/_windDirection", label="Wind Direction"
),
)
}
),
)

print(c.model_dump_json(indent=2, exclude_none=True))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers = [
"Topic :: Scientific/Engineering :: GIS",
"Typing :: Typed",
]
version = "0.1.1"
version = "0.2.0"
dependencies = ["pydantic>=2.3,<3"]

[project.optional-dependencies]
Expand Down
1 change: 1 addition & 0 deletions src/edr_pydantic/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class EdrBaseModel(PydanticBaseModel):
str_min_length=1,
validate_default=True,
validate_assignment=True,
strict=True,
)
5 changes: 2 additions & 3 deletions src/edr_pydantic/collections.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Dict
from typing import List
from typing import Optional

from .base_model import EdrBaseModel
from .data_queries import DataQueries
from .extent import Extent
from .link import Link
from .parameter import Parameter
from .parameter import Parameters


class Collection(EdrBaseModel):
Expand All @@ -20,7 +19,7 @@ class Collection(EdrBaseModel):
# TODO According to req A.13 it shall be CRS object, according to C.1 it is a string array
crs: Optional[List[str]] = None
output_formats: Optional[List[str]] = None
parameter_names: Dict[str, Parameter]
parameter_names: Parameters
# TODO According to req A.13 may have distanceunits. If radius is in link, it shall have distanceunits
distanceunits: Optional[List[str]] = None

Expand Down
2 changes: 2 additions & 0 deletions src/edr_pydantic/extent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Spatial(EdrBaseModel):


class Temporal(EdrBaseModel):
# TODO: Validate this list has two items (C.7. Temporal Object)
interval: List[List[AwareDatetime]]
# TODO: Validate this is a list of ISO 8601 single time, ISO 8601 time duration or ISO 8601 interval
values: List[str]
trs: str

Expand Down
5 changes: 5 additions & 0 deletions src/edr_pydantic/parameter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Dict
from typing import List
from typing import Literal
from typing import Optional

from pydantic import model_validator
from pydantic import RootModel

from .base_model import EdrBaseModel
from .observed_property import ObservedProperty
Expand All @@ -28,6 +30,9 @@ def must_not_have_unit_if_observed_property_has_categories(self):
return self


Parameters = RootModel[Dict[str, Parameter]]


class ParameterGroup(EdrBaseModel, extra="allow"):
type: Literal["ParameterGroup"] = "ParameterGroup"
id: Optional[str] = None
Expand Down

0 comments on commit 50d8d7f

Please sign in to comment.