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

Extend stray tag check #411

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,28 @@ 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."
)
"""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 codelist: {code.name}."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f"Unexpected bracket in codelist: {code.name}."
f"Unexpected bracket in {self.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 codelist: Primary Energy\|{Feul}"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
("char_in_str", r"Unexpected bracket in codelist: Primary Energy\|{Feul}"),
("char_in_str", r"Unexpected bracket in variable 'Primary Energy\|{Feul}'"),

("char_in_list", r"Unexpected bracket in codelist: Share\|Coal"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
("char_in_list", r"Unexpected bracket in codelist: Share\|Coal"),
("char_in_list", r"Unexpected bracket in variable 'Share\|Coal'"),

("char_in_dict", r"Unexpected bracket in codelist: Primary Energy"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
("char_in_dict", r"Unexpected bracket in codelist: Primary Energy"),
("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"""
dc-almeida marked this conversation as resolved.
Show resolved Hide resolved
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