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

Specify illegal character(s) found and respective attribute(s) #456

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 10 additions & 10 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,28 +339,28 @@ def check_illegal_characters(self, config: NomenclatureConfig) -> dict[str, Code
illegal = ["{", "}"] + config.illegal_characters
errors = ErrorCollector()

def _check_string(value):
def _check_string(attr, value):
if isinstance(value, str):
if any(char in value for char in illegal):
if found := set(illegal).intersection(value):
found = "', '".join(sorted(found))
errors.append(
ValueError(
f"Unexpected character in {self.name}: '{code.name}'."
f"Unexpected character(s) '{found}' in {self.name}.{attr} of: '{code.name}'."
" Check for illegal characters and/or if tags were spelled correctly."
)
)
elif isinstance(value, dict):
for k in value.keys():
_check_string(k)
for v in value.values():
_check_string(v)
for k, v in value.items():
_check_string(k, k)
_check_string(k, v)
elif isinstance(value, list):
for item in value:
_check_string(item)
_check_string(attr, item)

for code in self.mapping.values():
if not code.repository:
for value in code.model_dump(exclude="file").values():
_check_string(value)
for attr, value in code.model_dump(exclude="file").items():
_check_string(attr, value)
if errors:
raise ValueError(errors)

Expand Down
19 changes: 15 additions & 4 deletions tests/test_codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,18 @@ def test_to_csv():
@pytest.mark.parametrize(
"subfolder, match",
[
("tag_in_str", r"Unexpected character in variable: 'Primary Energy\|{Feul}'"),
("tag_in_list", r"Unexpected character in variable: 'Share\|Coal'"),
("tag_in_dict", r"Unexpected character in variable: 'Primary Energy'"),
(
"tag_in_str",
r"Unexpected character\(s\) '{', '}' in variable.name of: 'Primary Energy\|{Feul}'",
),
(
"tag_in_list",
r"Unexpected character\(s\) '{' in variable.info of: 'Share\|Coal'",
),
(
"tag_in_dict",
r"Unexpected character\(s\) '}' in variable.invalid of: 'Primary Energy'",
),
],
)
def test_stray_tag_fails(subfolder, match):
Expand All @@ -287,7 +296,9 @@ def test_stray_tag_fails(subfolder, match):

def test_illegal_char_fails():
"""Check that illegal character raises expected error."""
match = r"Unexpected character in variable: 'Primary Energy\|Coal'"
match = (
r"Unexpected character\(s\) '\"' in variable.info of: 'Primary Energy\|Coal'"
)
with raises(ValueError, match=match):
DataStructureDefinition(
MODULE_TEST_DATA_DIR / "illegal_chars" / "char_in_str" / "definitions"
Expand Down
Loading