Skip to content

Commit

Permalink
Use ErrorCollector for collecting duplicate code errors
Browse files Browse the repository at this point in the history
  • Loading branch information
phackstock committed Jan 23, 2024
1 parent 0f25022 commit 211c417
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import nomenclature
from nomenclature.code import Code, MetaCode, RegionCode, VariableCode
from nomenclature.config import NomenclatureConfig
from nomenclature.error import custom_pydantic_errors
from nomenclature.error import custom_pydantic_errors, ErrorCollector
from pyam.utils import is_list_like

here = Path(__file__).parent.absolute()
Expand Down Expand Up @@ -213,12 +213,16 @@ def from_directory(
)
+ code_list
)

errors = ErrorCollector()
mapping: Dict[str, Code] = {}
for code in code_list:
if code.name in mapping:
raise ValueError(f"Duplicate item in {name} codelist: {code.name}")
errors.append(
ValueError(f"Duplicate item in {name} codelist: {code.name}")
)
mapping[code.name] = code
if errors:
raise ValueError(errors)
return cls(name=name, mapping=mapping)

@classmethod
Expand Down Expand Up @@ -636,11 +640,14 @@ def from_directory(

# translate to mapping
mapping: Dict[str, RegionCode] = {}

errors = ErrorCollector()
for code in code_list:
if code.name in mapping:
raise ValueError(f"Trying to set a duplicate code {code.name}")
errors.append(ValueError(f"Trying to set a duplicate code {code.name}"))
mapping[code.name] = code

if errors:
raise ValueError(errors)
return cls(name=name, mapping=mapping)

@property
Expand Down

0 comments on commit 211c417

Please sign in to comment.