Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/citeable/_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Article(CitationBase):
"""An ``@article`` BibTeX entry."""

journal: str
volume: int
volume: int | None
pages: str | None
article_number: str | None
number: int | None
Expand All @@ -166,8 +166,8 @@ def __init__(
title: str,
year: int,
journal: str,
volume: int,
*,
volume: int | None = None,
pages: str | None = None,
article_number: str | None = None,
number: int | None = None,
Expand All @@ -178,10 +178,6 @@ def __init__(
app: str | None = None,
) -> None:
require_field(journal, "journal", "Article")
require_field(volume, "volume", "Article")
if pages is None and article_number is None:
msg = "Article requires 'pages' or 'article_number'; both are None"
raise ValueError(msg)

self._init_base(
author, title, year, doi=doi, url=url, note=note, key=key, app=app
Expand All @@ -199,8 +195,9 @@ def __str__(self) -> str:
_format_bibtex_field("title", self.title),
_format_bibtex_field("journal", self.journal),
_format_bibtex_field("year", str(self.year)),
_format_bibtex_field("volume", str(self.volume)),
]
if self.volume is not None:
lines.append(_format_bibtex_field("volume", str(self.volume)))
if self.number is not None:
lines.append(_format_bibtex_field("number", str(self.number)))
if self.pages is not None:
Expand All @@ -217,8 +214,9 @@ def _repr_fields(self) -> list[tuple[str, object]]:
("title", self.title),
("year", self.year),
("journal", self.journal),
("volume", self.volume),
]
if self.volume is not None:
fields.append(("volume", self.volume))
if self.pages is not None:
fields.append(("pages", self.pages))
if self.article_number is not None:
Expand Down
32 changes: 11 additions & 21 deletions tests/test_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,17 @@ def test_article_with_article_number():
assert a.pages is None


def test_article_missing_pages_and_article_number():
with pytest.raises(ValueError, match=r"pages.*article_number"):
Article(
author=["Smith, Jane"],
title="Something",
year=2024,
journal="Nature",
volume=1,
)


def test_article_missing_volume():
with pytest.raises(ValueError, match="Article requires 'volume'"):
Article(
author=["Smith, Jane"],
title="Something",
year=2024,
journal="Nature",
volume=None, # type: ignore[arg-type]
pages="1-10",
)
def test_article_advance_access():
a = Article(
author=["Smith, Jane"],
title="Something",
year=2024,
journal="Nature",
doi="10.1234/example",
)
assert a.volume is None
assert a.pages is None
assert a.article_number is None


def test_article_missing_journal():
Expand Down