Skip to content

Commit

Permalink
Guard against methods-terms as sub-category in variable name (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann authored Apr 11, 2024
1 parent 836c3fe commit f582737
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions definitions/variable/sdg-indicators/sdg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@
sdg: 2
unit: kcal/cap/day
weight: Population
- Price|Agriculture|Livestock|Index:
- Price|Agriculture|Livestock [Index]:
description: Weighted average price index of livestock
sdg: 2
unit: Index (2020 = 1)
skip-region-aggregation: true
# weight: Agricultural Production|Non-Energy
shape: Price|Agriculture|Livestock|Index
- Health|Premature Deaths|PM2.5:
description: Number of premature deaths associated with increased health risks from
exposure to air pollution of fine particles with a diameter of 2.5 μm or less (PM2.5)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_legacy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from nomenclature import DataStructureDefinition

LEGACY_PROJECTS = ["navigate", "engage", "shape"]


def test_legacy_variables():
# Check that (new) variables are not referenced as deprecated legacy variables
Expand All @@ -8,7 +10,7 @@ def test_legacy_variables():

legacy_variables = {}
for code, attrs in dsd.variable.items():
for project in ["navigate", "engage"]:
for project in LEGACY_PROJECTS:
if project in attrs.extra_attributes:
legacy_var = attrs.__getattr__(project)
if legacy_var in existing_variables:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_reserved_terms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from nomenclature import DataStructureDefinition

RESERVED_TERMS = ["Index", "Share", "Value", "Volume"]


# TODO: Move this test to the nomenclature `validate-project` utility
# see https://github.com/IAMconsortium/nomenclature/issues/341


def test_variable_ops_as_square_brackets():
# Check that variables use square brackets for operations
# https://github.com/IAMconsortium/common-definitions/issues/55

dsd = DataStructureDefinition("definitions/", dimensions=["variable"])

error = []
for variable in dsd.variable:
if reserved_terms := [r for r in RESERVED_TERMS if "|" + r in variable]:
error.append(f"Variable '{variable}' -> '... [{''.join(reserved_terms)}]'")
if error:
raise ValueError(
f"Found reserved terms in the following variables:\n{'\n - '.join(error)}"
)

0 comments on commit f582737

Please sign in to comment.