From 91f16b3f2d29ec8e9375a830b4d3a77e32656cae Mon Sep 17 00:00:00 2001 From: antazoey Date: Wed, 3 Jan 2024 11:20:50 -0600 Subject: [PATCH] fix: bug where could not set `references` Source model (#111) --- ethpm_types/source.py | 15 +++++++++------ tests/test_source.py | 9 +++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/ethpm_types/source.py b/ethpm_types/source.py index 3754dfd..ebd668d 100644 --- a/ethpm_types/source.py +++ b/ethpm_types/source.py @@ -221,18 +221,21 @@ class Source(BaseModel): @model_validator(mode="before") def validate_model(cls, model): - str_model = None + content = None + other_props = {} if isinstance(model, str): - str_model = model + content = model elif isinstance(model, dict) and isinstance(model.get("content"), str): - str_model = model["content"] + content = model["content"] + other_props = {k: v for k, v in model.items() if k != "content"} - return ( - {"content": Content(root={i + 1: x for i, x in enumerate(str_model.splitlines())})} - if str_model + content_result = ( + {"content": Content(root={i + 1: x for i, x in enumerate(content.splitlines())})} + if content else model ) + return {**content_result, **other_props} def __repr__(self) -> str: repr_id = "Source" diff --git a/tests/test_source.py b/tests/test_source.py index 56ba44e..d9d6a7c 100644 --- a/tests/test_source.py +++ b/tests/test_source.py @@ -307,3 +307,12 @@ def test_source_excludes_extra_lines(): content = "helloworld\n\n\n \n\n\t\n\n" source = Source(content=content) assert len(source) == 1 + + +def test_references(): + """ + Tests against a bug where the model validation + would accidentally ignore all extra properties. + """ + source = Source(content="foo\n", references=["bar.txt"]) + assert source.references == ["bar.txt"]