Skip to content

Commit 89b399b

Browse files
committed
feat(typing): add py.typed, improve public types
1 parent f4a48b5 commit 89b399b

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

src/docx/api.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66
from __future__ import annotations
77

88
import os
9-
from typing import IO
9+
from typing import IO, TYPE_CHECKING, cast
1010

1111
from docx.opc.constants import CONTENT_TYPE as CT
1212
from docx.package import Package
1313

14+
if TYPE_CHECKING:
15+
from docx.document import Document as DocumentObject
16+
from docx.parts.document import DocumentPart
1417

15-
def Document(docx: str | IO[bytes] | None = None):
18+
19+
def Document(docx: str | IO[bytes] | None = None) -> DocumentObject:
1620
"""Return a |Document| object loaded from `docx`, where `docx` can be either a path
1721
to a ``.docx`` file (a string) or a file-like object.
1822
1923
If `docx` is missing or ``None``, the built-in default document "template" is
2024
loaded.
2125
"""
2226
docx = _default_docx_path() if docx is None else docx
23-
document_part = Package.open(docx).main_document_part
27+
document_part = cast("DocumentPart", Package.open(docx).main_document_part)
2428
if document_part.content_type != CT.WML_DOCUMENT_MAIN:
2529
tmpl = "file '%s' is not a Word file, content type is '%s'"
2630
raise ValueError(tmpl % (docx, document_part.content_type))

src/docx/oxml/text/parfmt.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@
2828
class CT_Ind(BaseOxmlElement):
2929
"""``<w:ind>`` element, specifying paragraph indentation."""
3030

31-
left = OptionalAttribute("w:left", ST_SignedTwipsMeasure)
32-
right = OptionalAttribute("w:right", ST_SignedTwipsMeasure)
33-
firstLine = OptionalAttribute("w:firstLine", ST_TwipsMeasure)
34-
hanging = OptionalAttribute("w:hanging", ST_TwipsMeasure)
31+
left: Length | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
32+
"w:left", ST_SignedTwipsMeasure
33+
)
34+
right: Length | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
35+
"w:right", ST_SignedTwipsMeasure
36+
)
37+
firstLine: Length | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
38+
"w:firstLine", ST_TwipsMeasure
39+
)
40+
hanging: Length | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
41+
"w:hanging", ST_TwipsMeasure
42+
)
3543

3644

3745
class CT_Jc(BaseOxmlElement):
@@ -45,6 +53,7 @@ class CT_Jc(BaseOxmlElement):
4553
class CT_PPr(BaseOxmlElement):
4654
"""``<w:pPr>`` element, containing the properties for a paragraph."""
4755

56+
get_or_add_ind: Callable[[], CT_Ind]
4857
get_or_add_pStyle: Callable[[], CT_String]
4958
_insert_sectPr: Callable[[CT_SectPr], None]
5059
_remove_pStyle: Callable[[], None]
@@ -98,13 +107,15 @@ class CT_PPr(BaseOxmlElement):
98107
numPr = ZeroOrOne("w:numPr", successors=_tag_seq[7:])
99108
tabs = ZeroOrOne("w:tabs", successors=_tag_seq[11:])
100109
spacing = ZeroOrOne("w:spacing", successors=_tag_seq[22:])
101-
ind = ZeroOrOne("w:ind", successors=_tag_seq[23:])
110+
ind: CT_Ind | None = ZeroOrOne( # pyright: ignore[reportAssignmentType]
111+
"w:ind", successors=_tag_seq[23:]
112+
)
102113
jc = ZeroOrOne("w:jc", successors=_tag_seq[27:])
103114
sectPr = ZeroOrOne("w:sectPr", successors=_tag_seq[35:])
104115
del _tag_seq
105116

106117
@property
107-
def first_line_indent(self):
118+
def first_line_indent(self) -> Length | None:
108119
"""A |Length| value calculated from the values of `w:ind/@w:firstLine` and
109120
`w:ind/@w:hanging`.
110121
@@ -122,7 +133,7 @@ def first_line_indent(self):
122133
return firstLine
123134

124135
@first_line_indent.setter
125-
def first_line_indent(self, value):
136+
def first_line_indent(self, value: Length | None):
126137
if self.ind is None and value is None:
127138
return
128139
ind = self.get_or_add_ind()
@@ -135,30 +146,30 @@ def first_line_indent(self, value):
135146
ind.firstLine = value
136147

137148
@property
138-
def ind_left(self):
149+
def ind_left(self) -> Length | None:
139150
"""The value of `w:ind/@w:left` or |None| if not present."""
140151
ind = self.ind
141152
if ind is None:
142153
return None
143154
return ind.left
144155

145156
@ind_left.setter
146-
def ind_left(self, value):
157+
def ind_left(self, value: Length | None):
147158
if value is None and self.ind is None:
148159
return
149160
ind = self.get_or_add_ind()
150161
ind.left = value
151162

152163
@property
153-
def ind_right(self):
164+
def ind_right(self) -> Length | None:
154165
"""The value of `w:ind/@w:right` or |None| if not present."""
155166
ind = self.ind
156167
if ind is None:
157168
return None
158169
return ind.right
159170

160171
@ind_right.setter
161-
def ind_right(self, value):
172+
def ind_right(self, value: Length | None):
162173
if value is None and self.ind is None:
163174
return
164175
ind = self.get_or_add_ind()
@@ -340,9 +351,15 @@ class CT_TabStop(BaseOxmlElement):
340351
only needs a __str__ method.
341352
"""
342353

343-
val = RequiredAttribute("w:val", WD_TAB_ALIGNMENT)
344-
leader = OptionalAttribute("w:leader", WD_TAB_LEADER, default=WD_TAB_LEADER.SPACES)
345-
pos = RequiredAttribute("w:pos", ST_SignedTwipsMeasure)
354+
val: WD_TAB_ALIGNMENT = RequiredAttribute( # pyright: ignore[reportAssignmentType]
355+
"w:val", WD_TAB_ALIGNMENT
356+
)
357+
leader: WD_TAB_LEADER | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
358+
"w:leader", WD_TAB_LEADER, default=WD_TAB_LEADER.SPACES
359+
)
360+
pos: Length = RequiredAttribute( # pyright: ignore[reportAssignmentType]
361+
"w:pos", ST_SignedTwipsMeasure
362+
)
346363

347364
def __str__(self) -> str:
348365
"""Text equivalent of a `w:tab` element appearing in a run.

src/docx/parts/document.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from docx.shared import lazyproperty
1717

1818
if TYPE_CHECKING:
19+
from docx.opc.coreprops import CoreProperties
1920
from docx.styles.style import BaseStyle
2021

2122

@@ -41,7 +42,7 @@ def add_header_part(self):
4142
return header_part, rId
4243

4344
@property
44-
def core_properties(self):
45+
def core_properties(self) -> CoreProperties:
4546
"""A |CoreProperties| object providing read/write access to the core properties
4647
of this document."""
4748
return self.package.core_properties

src/docx/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)