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 ISO3 codes as string #253

Merged
merged 3 commits into from
May 25, 2023
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
17 changes: 9 additions & 8 deletions nomenclature/code.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import re
import pyam
import pycountry
from keyword import iskeyword
from pathlib import Path
Expand Down Expand Up @@ -220,19 +221,19 @@ class RegionCode(Code):
"""

hierarchy: str = None
iso3_codes: List[str] = None
iso3_codes: Union[List[str], str] = None

@validator("iso3_codes")
def check_iso3_codes(cls, v, values) -> List[str]:
"""Verifies that each ISO3 code is valid according to pycountry library."""
invalid_iso3_codes: List[str] = []
for iso3_code in v:
if pycountry.countries.get(alpha_3=iso3_code) is None:
invalid_iso3_codes.append(iso3_code)
if invalid_iso3_codes:
if invalid_iso3_codes := [
iso3_code
for iso3_code in pyam.to_list(v)
if pycountry.countries.get(alpha_3=iso3_code) is None
]:
raise ValueError(
f"Region {values['name']} has invalid"
f" ISO3 country codes: {invalid_iso3_codes}"
f"Region '{values['name']}' has invalid ISO3 country code(s): "
+ ", ".join(invalid_iso3_codes)
)
return v

Expand Down
28 changes: 20 additions & 8 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_RegionCode_hierarchy_attribute():
assert reg.hierarchy == "R5"


def test_RegionCode_iso3_code():
def test_RegionCode_iso3_code_list():
reg = RegionCode(
name="Western Europe",
hierarchy="R5OECD",
Expand Down Expand Up @@ -119,7 +119,12 @@ def test_RegionCode_iso3_code():
]


def test_RegionCode_iso3_code_fail():
def test_RegionCode_iso3_code_str():
reg = RegionCode(name="Austria", hierarchy="country", iso3_codes="AUT")
assert reg.iso3_codes == "AUT"


def test_RegionCode_iso3_code_list_fail():
iso3_codes = [
"DMK",
"IPL",
Expand Down Expand Up @@ -149,17 +154,24 @@ def test_RegionCode_iso3_code_fail():
]

error_pattern = (
"1 validation error for RegionCode\niso3_codes\n Reg"
"ion Western Europe has invalid ISO3 country codes"
": \['DMK', 'IPL', 'ATZ', 'FNL', 'FRE', 'DEX', 'GRE'," # noqa
" 'IBL', 'ITL', 'LIC', 'MLA', 'BEG', 'FRT', 'ANB', " # noqa
"'GDR', 'LXB', 'MNO', 'NTD', 'NRW', 'PRE', 'EPA', " # noqa
"'SWD', 'CEW', 'GTR', 'SOR'\] \(type=value_error\)" # noqa
"1 validation error for RegionCode\niso3_codes\n Region 'Western Europe' has "
"invalid ISO3 country code\(s\): DMK, IPL, ATZ, FNL, FRE, DEX, GRE, " # noqa
"IBL, ITL, LIC, MLA, BEG, FRT, ANB, GDR, LXB, MNO, NTD, NRW, PRE, EPA, " # noqa
"SWD, CEW, GTR, SOR \(type=value_error\)" # noqa
)
with pytest.raises(ValueError, match=error_pattern):
RegionCode(name="Western Europe", hierarchy="R5OECD", iso3_codes=iso3_codes)


def test_RegionCode_iso3_code_str_fail():
error_pattern = (
"1 validation error for RegionCode\niso3_codes\n Region 'Austria' has invalid "
"ISO3 country code\(s\): AUTT \(type=value_error\)"
)
with pytest.raises(ValueError, match=error_pattern):
RegionCode(name="Austria", hierarchy="country", iso3_codes="AUTT")


def test_MetaCode_allowed_values_attribute():
meta = MetaCode(
name="MetaCode test",
Expand Down