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

Fix/stray tags #163

Merged
merged 15 commits into from
Aug 24, 2022
8 changes: 8 additions & 0 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ def cast_variable_components_args(cls, v, values):

return v

@validator("mapping")
def check_stray_tag(cls, v, values):
phackstock marked this conversation as resolved.
Show resolved Hide resolved
"""Check that no '{' are left in codes after tag replacement"""
for code in v:
if "{" in code:
raise ValueError(f"Unexpected {{}} in codelist : {code}")
return v

def __setitem__(self, key, value):
if key in self.mapping:
raise DuplicateCodeError(name=self.name, code=key)
Expand Down
3 changes: 3 additions & 0 deletions tests/data/stray_tag/definitions/variable/tag_fuel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Fuel:
- Coal:
definition: coal
9 changes: 9 additions & 0 deletions tests/data/stray_tag/definitions/variable/variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- Primary Energy:
definition: Total primary energy consumption
unit: EJ/yr
- Primary Energy|{Feul}:
definition: Primary energy consumption of {Fuel}
unit: EJ/yr
- Share|{Fuel}:
definition: Share of {Fuel} in the total primary energy mix
unit:
11 changes: 11 additions & 0 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from nomenclature.testing import assert_valid_yaml
from conftest import TEST_DATA_DIR
from nomenclature.codelist import CodeList


def test_assert_yaml():
Expand All @@ -26,3 +27,13 @@ def test_assert_yaml_fails(caplog):
obs = log[2].replace("\n", "") # strip newlines from observed log message
exp = r"Error parsing file while scanning a simple key.*, line 4, column 1"
assert re.match(exp, obs)


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

match = "Unexpected {} in codelist : Primary Energy|{Feul}"
with pytest.raises(ValueError, match=match):
CodeList.from_directory(
"variable", TEST_DATA_DIR / "stray_tag" / "definitions" / "variable"
)