File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -251,7 +251,7 @@ constraints:
251251# ## Reference
252252
253253Bibliographic 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
257257references:
Original file line number Diff line number Diff line change @@ -73,16 +73,18 @@ def __hash__(self):
7373
7474
7575class 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
8890class Usage (BaseModel ):
Original file line number Diff line number Diff line change 55
66
77class 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" ] )
You can’t perform that action at this time.
0 commit comments