Skip to content

Commit

Permalink
Extend stray tag check (#411)
Browse files Browse the repository at this point in the history
* Expand stray tag check to all attributes

* Update stray tag tests

* Parametrize test

* Fix variable unit

* Improve error message
  • Loading branch information
dc-almeida authored Oct 9, 2024
1 parent f528935 commit e64ed34
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
33 changes: 25 additions & 8 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,31 @@ def __eq__(self, other):

@field_validator("mapping")
@classmethod
def check_stray_tag(cls, v: Dict[str, Code]) -> Dict[str, Code]:
"""Check that no '{' are left in codes after tag replacement"""
for code in v:
if "{" in code:
raise ValueError(
f"Unexpected {{}} in codelist: {code}."
" Check if the tag was spelled correctly."
)
def check_stray_tag(
cls, v: Dict[str, Code], info: ValidationInfo
) -> Dict[str, Code]:
"""Check that no stray tags are left in codes after tag replacement"""
forbidden = ["{", "}"]

def _check_string(value):
if isinstance(value, str):
if any(char in value for char in forbidden):
raise ValueError(
f"Unexpected bracket in {info.data['name']}: '{code.name}'."
" Check if tags were spelled correctly."
)
elif isinstance(value, dict):
for v in value.values():
_check_string(v)
elif isinstance(value, list):
for item in value:
_check_string(item)

for code in v.values():
for attr in code.model_fields:
if attr not in ["file", "repository"]:
value = getattr(code, attr)
_check_string(value)
return v

@field_validator("mapping")
Expand Down
13 changes: 13 additions & 0 deletions tests/data/codelist/stray_tag/char_in_dict/variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- Primary Energy:
definition: Total primary energy consumption
unit: EJ/yr
info:
valid: Valid information.
invalid: Invalid bracket } information.
final: Another valid information.
- Primary Energy|Coal:
definition: Primary energy consumption of coal
unit: EJ/yr
- Share|Coal:
definition: Share of coal in the total primary energy mix
unit: '%'
14 changes: 14 additions & 0 deletions tests/data/codelist/stray_tag/char_in_list/variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- Primary Energy:
definition: Total primary energy consumption
unit: EJ/yr
- Primary Energy|Coal:
definition: Primary energy consumption of coal
unit: EJ/yr
- Share|Coal:
definition: Share of coal in the total primary energy mix
unit: '%'
info:
- Valid information.
- Invalid bracket { information.
- Another valid information.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
unit: EJ/yr
- Share|{Fuel}:
definition: Share of {Fuel} in the total primary energy mix
unit:
unit: '%'
16 changes: 11 additions & 5 deletions tests/test_codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,19 @@ def test_to_csv():
assert obs == exp


def test_stray_tag_fails():
"""Check that typos in a tag raises expected error"""

match = r"Unexpected {} in codelist: Primary Energy\|{Feul}"
@pytest.mark.parametrize(
"subfolder, match",
[
("char_in_str", r"Unexpected bracket in variable: 'Primary Energy\|{Feul}'"),
("char_in_list", r"Unexpected bracket in variable: 'Share\|Coal'"),
("char_in_dict", r"Unexpected bracket in variable: 'Primary Energy'"),
],
)
def test_stray_tag_fails(subfolder, match):
"""Check that stray brackets from, e.g. typos in a tag, raises expected error"""
with raises(ValueError, match=match):
VariableCodeList.from_directory(
"variable", MODULE_TEST_DATA_DIR / "stray_tag" / "definitions" / "variable"
"variable", MODULE_TEST_DATA_DIR / "stray_tag" / subfolder
)


Expand Down

0 comments on commit e64ed34

Please sign in to comment.