Skip to content

Commit

Permalink
Remove dimensions implicit default
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-almeida committed Nov 13, 2024
1 parent 4667a27 commit 3424777
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
8 changes: 4 additions & 4 deletions nomenclature/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ def __init__(self, path, dimensions=None):
self.dimensions = (
dimensions
or self.config.dimensions
or [
"region",
"variable",
]
or [x.stem for x in path.iterdir() if x.is_dir()]
)
if not self.dimensions:
raise ValueError("No dimensions specified in data structure.")

for dim in self.dimensions:
codelist_cls = SPECIAL_CODELIST.get(dim, CodeList)
self.__setattr__(
Expand Down
9 changes: 7 additions & 2 deletions nomenclature/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import yaml

from nomenclature.definition import DataStructureDefinition
from nomenclature.config import NomenclatureConfig
from nomenclature.processor import (
DataValidator,
RegionProcessor,
Expand Down Expand Up @@ -144,8 +145,12 @@ def assert_valid_structure(
f"Definitions directory not found: {path / definitions}"
)

if dimensions == (): # if "dimensions" were not specified
dimensions = [x.stem for x in (path / definitions).iterdir() if x.is_dir()]
if not dimensions: # if "dimensions" were not specified
dimensions = (
NomenclatureConfig.from_file(path / "nomenclature.yaml").dimensions
if (path / "nomenclature.yaml").is_file()
else [x.stem for x in (path / definitions).iterdir() if x.is_dir()]
)
if not dimensions:
raise FileNotFoundError(
f"`definitions` directory is empty: {path / definitions}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
dimensions:
- region
- variable
repositories:
common-definitions:
url: https://github.com/IAMconsortium/common-definitions.git/
Expand Down
5 changes: 2 additions & 3 deletions tests/test_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_nonexisting_path_raises():

def test_empty_codelist_raises():
"""Check that initializing a DataStructureDefinition with empty CodeList raises"""
match = "Empty codelist: region, variable"
match = "No dimensions specified in data structure."
with pytest.raises(ValueError, match=match):
DataStructureDefinition(TEST_DATA_DIR / "codelist" / "simple_codelist")

Expand Down Expand Up @@ -134,8 +134,7 @@ def test_create_yaml_from_xlsx(input_file, attrs, exp_file, tmpdir):

with open(file, "r", encoding="utf-8") as f:
obs = f.read()
with open(TEST_DATA_DIR / "io" / "excel_io" / exp_file, "r",
encoding="utf-8") as f:
with open(TEST_DATA_DIR / "io" / "excel_io" / exp_file, "r", encoding="utf-8") as f:
exp = f.read()

assert obs == exp
Expand Down

0 comments on commit 3424777

Please sign in to comment.