Skip to content

Commit

Permalink
fix: bug where could not set references Source model (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jan 3, 2024
1 parent e14e101 commit 91f16b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ethpm_types/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions tests/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit 91f16b3

Please sign in to comment.