Skip to content

Commit 8af46fe

Browse files
committed
comments: add Comment.author
1 parent 7cf36d6 commit 8af46fe

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

features/cmt-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Feature: Get comment properties
99
Then comment.comment_id is the comment identifier
1010

1111

12-
@wip
1312
Scenario: Comment.author
1413
Given a Comment object
1514
Then comment.author is the author of the comment

src/docx/comments.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def __init__(self, comment_elm: CT_Comment, comments_part: CommentsPart):
5353
super().__init__(comment_elm, comments_part)
5454
self._comment_elm = comment_elm
5555

56+
@property
57+
def author(self) -> str:
58+
"""The recorded author of this comment."""
59+
return self._comment_elm.author
60+
5661
@property
5762
def comment_id(self) -> int:
5863
"""The unique identifier of this comment."""

src/docx/oxml/comments.py

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

33
from __future__ import annotations
44

5-
from docx.oxml.simpletypes import ST_DecimalNumber
5+
from docx.oxml.simpletypes import ST_DecimalNumber, ST_String
66
from docx.oxml.xmlchemy import BaseOxmlElement, RequiredAttribute, ZeroOrMore
77

88

@@ -35,3 +35,4 @@ class CT_Comment(BaseOxmlElement):
3535
"""
3636

3737
id: int = RequiredAttribute("w:id", ST_DecimalNumber) # pyright: ignore[reportAssignmentType]
38+
author: str = RequiredAttribute("w:author", ST_String) # pyright: ignore[reportAssignmentType]

tests/test_comments.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ def it_knows_its_comment_id(self, comments_part_: Mock):
101101

102102
assert comment.comment_id == 42
103103

104+
def it_knows_its_author(self, comments_part_: Mock):
105+
comment_elm = cast(CT_Comment, element("w:comment{w:id=42,w:author=Steve Canny}"))
106+
comment = Comment(comment_elm, comments_part_)
107+
108+
assert comment.author == "Steve Canny"
109+
104110
# -- fixtures --------------------------------------------------------------------------------
105111

106112
@pytest.fixture

0 commit comments

Comments
 (0)