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

Allow countries attribute as string #412

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion nomenclature/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ class RegionCode(Code):
countries: Optional[List[str]] = None
iso3_codes: Optional[Union[List[str], str]] = None

@field_validator("countries")
@field_validator("countries", mode="before")
def check_countries(cls, v: List[str], info: ValidationInfo) -> List[str]:
"""Verifies that each country name is defined in `nomenclature.countries`."""
v = to_list(v)
if invalid_country_names := set(v) - set(countries.names):
raise ValueError(
f"Region '{info.data['name']}' uses non-standard country name(s): "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- common:
- World
- countries:
- Some region:
countries: Austria
9 changes: 9 additions & 0 deletions tests/test_codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ def test_region_codelist_nonexisting_country_name():
)


def test_region_codelist_str_country_name():
"""Check that country name as string is validated against `nomenclature.countries`"""
code = RegionCodeList.from_directory(
"region",
MODULE_TEST_DATA_DIR / "region_codelist" / "countries_attribute_str",
)
assert code["Some region"].countries == ["Austria"]


def test_norway_as_str():
"""guard against casting of 'NO' to boolean `False` by PyYAML or pydantic"""
region = RegionCodeList.from_directory(
Expand Down