Skip to content

Commit ae01c53

Browse files
authored
Only complain about duplicate labels when the non-prefixed keys are different. (#366)
Closes #365
1 parent 6359d5b commit ae01c53

File tree

8 files changed

+45
-3
lines changed

8 files changed

+45
-3
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
* Bib files in conf.py are now resolved relative to confdir instead of srcdir
1717
(reported and fixed by rgambord, see issue #357 and pull request #358).
1818

19+
* Duplicate label warnings are no longer issued if the non-prefixed keys
20+
are identical. This allows having duplicate citations,
21+
as long as the keys are differently prefixed, which is a common use case.
22+
See issue #365 reported by ego-thales.
23+
1924
2.6.3 (12 September 2024)
2025
-------------------------
2126

src/sphinxcontrib/bibtex/domain.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,16 @@ def env_updated(self) -> Iterable[str]:
380380
if bibliography.list_ == "citation":
381381
used_keys.add(key)
382382
if formatted_entry.label not in used_labels:
383-
used_labels[formatted_entry.label] = key
384-
elif used_labels[formatted_entry.label] != key:
383+
used_labels[formatted_entry.label] = formatted_entry.key
384+
elif used_labels[formatted_entry.label] != formatted_entry.key:
385385
# if used_label[label] == key then already
386386
# duplicate key warning
387387
logger.warning(
388388
'duplicate label "%s" for keys "%s" and "%s"'
389389
% (
390390
formatted_entry.label,
391391
used_labels[formatted_entry.label],
392-
key,
392+
formatted_entry.key,
393393
),
394394
location=(bibliography_key.docname, bibliography.line),
395395
type="bibtex",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extensions = ["sphinxcontrib.bibtex"]
2+
exclude_patterns = ["_build"]
3+
bibtex_bibfiles = ["test.bib"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Doc1
2+
====
3+
4+
.. bibliography:: test.bib
5+
:keyprefix: doc1-
6+
:all:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Doc2
2+
====
3+
4+
.. bibliography:: test.bib
5+
:keyprefix: doc2-
6+
:all:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Contents
2+
========
3+
4+
.. toctree::
5+
6+
doc1
7+
doc2
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@Misc{Test,
2+
author = {Mr. Test},
3+
title = {Test},
4+
}

test/test_duplicate.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ def test_duplicate_label(app, warning) -> None:
1616
assert html_citations(label="1").search(output2)
1717

1818

19+
@pytest.mark.sphinx("html", testroot="duplicate_label_2")
20+
def test_duplicate_label_2(app, warning) -> None:
21+
# see github issue #365
22+
app.build()
23+
assert not warning.getvalue()
24+
output = (app.outdir / "doc1.html").read_text()
25+
output2 = (app.outdir / "doc2.html").read_text()
26+
assert html_citations(label="Tes").search(output)
27+
assert html_citations(label="Tes").search(output2)
28+
29+
1930
@pytest.mark.sphinx("html", testroot="duplicate_citation")
2031
def test_duplicate_citation(app, warning) -> None:
2132
app.build()

0 commit comments

Comments
 (0)