Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/docx/oxml/numbering.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _next_numId(self):
"""The first ``numId`` unused by a ``<w:num>`` element, starting at 1 and
filling any gaps in numbering between existing ``<w:num>`` elements."""
numId_strs = self.xpath("./w:num/@w:numId")
num_ids = [int(numId_str) for numId_str in numId_strs]
num_ids = {int(numId_str) for numId_str in numId_strs}
for num in range(1, len(num_ids) + 2):
if num not in num_ids:
break
Expand Down
26 changes: 26 additions & 0 deletions tests/oxml/test_numbering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Test suite for the docx.oxml.numbering module."""

from typing import cast

import pytest

from docx.oxml.numbering import CT_Numbering

from ..unitutil.cxml import element


class DescribeCT_Numbering:
@pytest.mark.parametrize(
("num_ids", "expected_value"),
[
([], 1),
([1, 2, 3], 4),
([1, 3, 4], 2),
],
)
def it_knows_the_next_unused_numId(self, num_ids: list[int], expected_value: int):
children = ",".join(f"w:num{{w:numId={num_id}}}" for num_id in num_ids)
cxml = "w:numbering" if not children else f"w:numbering/({children})"
numbering = cast(CT_Numbering, element(cxml))

assert numbering._next_numId == expected_value