Skip to content

Commit 1f4ad8a

Browse files
committed
fix: Make all reference fields optional
Fixes #176
1 parent 014d004 commit 1f4ad8a

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

SCHEMA.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ constraints:
251251
### Reference
252252

253253
Bibliographic pointer.
254-
Requires a `title` and `authors` and optionally includes a `link` to the material.
254+
Requires either a `title` or a `link` and optionally a list of `authors`.
255255

256256
```yaml
257257
references:

src/opltools/schema.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,18 @@ def __hash__(self):
7373

7474

7575
class Reference(BaseModel):
76-
title: str
77-
authors: list[str]
76+
title: str | None = None
77+
authors: list[str] | None = None
7878
link: Link | None = None
7979

80+
@model_validator(mode="after")
81+
def _validate(self) -> Self:
82+
if self.title is None and self.link is None:
83+
raise ValueError("References must have either a title or a link.")
84+
return self
85+
8086
def __hash__(self):
81-
return (
82-
hash(self.title)
83-
+ sum([hash(author) for author in self.authors])
84-
+ hash(self.link)
85-
)
87+
return hash(self.title) + hash(self.link)
8688

8789

8890
class Usage(BaseModel):

tests/test_reference.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55

66

77
class TestReference:
8-
def test_minimal(self):
9-
ref = Reference(title="A paper", authors=["Alice", "Bob"])
8+
def test_only_author(self):
9+
ref = Reference(title="A paper")
1010
assert ref.title == "A paper"
11-
assert ref.authors == ["Alice", "Bob"]
11+
assert ref.authors is None
1212
assert ref.link is None
1313

14-
def test_with_link(self):
14+
def test_only_link(self):
15+
ref = Reference(link=Link(url="https://example.org"))
16+
assert ref.link.url == "https://example.org"
17+
18+
def test_full(self):
1519
ref = Reference(
1620
title="A paper",
1721
authors=["Alice"],
1822
link=Link(url="https://example.org"),
1923
)
2024
assert ref.link.url == "https://example.org"
2125

22-
def test_requires_authors(self):
26+
def test_requires_title_or_link(self):
2327
with pytest.raises(ValidationError):
24-
Reference(title="A paper")
28+
Reference(authors=["A paper"])

0 commit comments

Comments
 (0)