Skip to content

Commit

Permalink
Use Annotated for regex strings
Browse files Browse the repository at this point in the history
  • Loading branch information
irm-codebase committed Nov 19, 2024
1 parent 46ae5a0 commit 3186660
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
6 changes: 1 addition & 5 deletions src/calliope/schemas/data_table_schema.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
"""Implements the data table configuration class."""

from typing import Self

from pydantic import BaseModel, model_validator
from typing_extensions import Self

from calliope.util.schema import AttrStr, UniqueList
from calliope.util.tools import listify

# Get rid of pyright false negatives (see https://github.com/microsoft/pylance-release/issues/5457)
# pyright: reportInvalidTypeForm=false


class DataTable(BaseModel):
"""Data table validation model."""
Expand Down
5 changes: 3 additions & 2 deletions src/calliope/util/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Annotated, Literal, TypeVar

import jsonschema
from pydantic import AfterValidator, Field, constr
from pydantic import AfterValidator, Field
from pydantic_core import PydanticCustomError

from calliope.attrdict import AttrDict
Expand All @@ -24,7 +24,8 @@

# Regular string pattern for most calliope attributes
FIELD_REGEX = r"^[^_^\d][\w]*$"
AttrStr = constr(pattern=FIELD_REGEX)
AttrStr = Annotated[str, Field(pattern=FIELD_REGEX)]

# ==
# Taken from https://github.com/pydantic/pydantic-core/pull/820#issuecomment-1670475909
T = TypeVar("T", bound=Hashable)
Expand Down
29 changes: 24 additions & 5 deletions tests/test_data_table_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test data table schema validation."""

from pathlib import Path

import pytest
from pydantic import ValidationError

Expand All @@ -8,8 +10,10 @@

from .common.util import check_error_or_warning

FULL_TABLE_CONFIG = """
data: time_varying_df

@pytest.fixture
def full_data_table_config():
return """data: time_varying_df
rows: timesteps
columns: [comment, nodes, techs]
select:
Expand All @@ -25,6 +29,16 @@
"""


@pytest.fixture
def model_yaml_data_tables() -> AttrDict:
return AttrDict.from_yaml(
Path(__file__).parent
/ "common"
/ "national_scale_from_data_tables"
/ "model.yaml"
)


@pytest.mark.parametrize(
"data_table",
[{"rows": "timesteps"}, {"rows": "timesteps", "columns": ["techs", "nodes"]}],
Expand Down Expand Up @@ -71,7 +85,12 @@ def test_add_dims_overlap(rows, columns, add_dims):
)


@pytest.mark.parametrize("data_table", [FULL_TABLE_CONFIG])
def test_full_table_config(data_table):
def test_full_table_config(full_data_table_config):
"""Test a fully fledged data table configuration."""
DataTable(**AttrDict.from_yaml_string(data_table))
DataTable(**AttrDict.from_yaml_string(full_data_table_config))


def test_data_table_model(model_yaml_data_tables):
"""Data table validation must conform to expected usage."""
for data_table in model_yaml_data_tables["data_tables"].values():
DataTable(**data_table)

0 comments on commit 3186660

Please sign in to comment.