Skip to content

Commit 0eeaa2f

Browse files
committed
comments: add Comment.comment_id
1 parent e2aec42 commit 0eeaa2f

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

features/doc-comments.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Feature: Document.comments
3030
Then iterating comments yields 4 Comment objects
3131

3232

33-
@wip
3433
Scenario: Comments.get()
3534
Given a Comments object with 4 comments
3635
When I call comments.get(2)

src/docx/comments.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ class Comment(BlockItemContainer):
5252
def __init__(self, comment_elm: CT_Comment, comments_part: CommentsPart):
5353
super().__init__(comment_elm, comments_part)
5454
self._comment_elm = comment_elm
55+
56+
@property
57+
def comment_id(self) -> int:
58+
"""The unique identifier of this comment."""
59+
return self._comment_elm.id

src/docx/oxml/comments.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from __future__ import annotations
44

5-
from docx.oxml.xmlchemy import BaseOxmlElement, ZeroOrMore
5+
from docx.oxml.simpletypes import ST_DecimalNumber
6+
from docx.oxml.xmlchemy import BaseOxmlElement, RequiredAttribute, ZeroOrMore
67

78

89
class CT_Comments(BaseOxmlElement):
@@ -32,3 +33,5 @@ class CT_Comment(BaseOxmlElement):
3233
While probably most often used for a single sentence or phrase, a comment can contain rich
3334
content, including multiple rich-text paragraphs, hyperlinks, images, and tables.
3435
"""
36+
37+
id: int = RequiredAttribute("w:id", ST_DecimalNumber) # pyright: ignore[reportAssignmentType]

tests/test_comments.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from docx.comments import Comment, Comments
1212
from docx.opc.constants import CONTENT_TYPE as CT
1313
from docx.opc.packuri import PackURI
14-
from docx.oxml.comments import CT_Comments
14+
from docx.oxml.comments import CT_Comment, CT_Comments
1515
from docx.package import Package
1616
from docx.parts.comments import CommentsPart
1717

@@ -90,3 +90,19 @@ def it_can_get_a_comment_by_id(self, package_: Mock):
9090
@pytest.fixture
9191
def package_(self, request: FixtureRequest):
9292
return instance_mock(request, Package)
93+
94+
95+
class DescribeComment:
96+
"""Unit-test suite for `docx.comments.Comment`."""
97+
98+
def it_knows_its_comment_id(self, comments_part_: Mock):
99+
comment_elm = cast(CT_Comment, element("w:comment{w:id=42}"))
100+
comment = Comment(comment_elm, comments_part_)
101+
102+
assert comment.comment_id == 42
103+
104+
# -- fixtures --------------------------------------------------------------------------------
105+
106+
@pytest.fixture
107+
def comments_part_(self, request: FixtureRequest):
108+
return instance_mock(request, CommentsPart)

0 commit comments

Comments
 (0)