Skip to content

Commit cf17811

Browse files
committed
rfctr: docx.shared type-checks strict
1 parent b7f5903 commit cf17811

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/docx/shared.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
)
1717

1818
if TYPE_CHECKING:
19+
from docx import types as t
20+
from docx.opc.part import XmlPart
1921
from docx.oxml.xmlchemy import BaseOxmlElement
2022
from docx.parts.story import StoryPart
2123

@@ -34,7 +36,7 @@ class Length(int):
3436
_EMUS_PER_PT = 12700
3537
_EMUS_PER_TWIP = 635
3638

37-
def __new__(cls, emu):
39+
def __new__(cls, emu: int):
3840
return int.__new__(cls, emu)
3941

4042
@property
@@ -71,15 +73,15 @@ def twips(self):
7173
class Inches(Length):
7274
"""Convenience constructor for length in inches, e.g. ``width = Inches(0.5)``."""
7375

74-
def __new__(cls, inches):
76+
def __new__(cls, inches: float):
7577
emu = int(inches * Length._EMUS_PER_INCH)
7678
return Length.__new__(cls, emu)
7779

7880

7981
class Cm(Length):
8082
"""Convenience constructor for length in centimeters, e.g. ``height = Cm(12)``."""
8183

82-
def __new__(cls, cm):
84+
def __new__(cls, cm: float):
8385
emu = int(cm * Length._EMUS_PER_CM)
8486
return Length.__new__(cls, emu)
8587

@@ -88,22 +90,22 @@ class Emu(Length):
8890
"""Convenience constructor for length in English Metric Units, e.g. ``width =
8991
Emu(457200)``."""
9092

91-
def __new__(cls, emu):
93+
def __new__(cls, emu: int):
9294
return Length.__new__(cls, int(emu))
9395

9496

9597
class Mm(Length):
9698
"""Convenience constructor for length in millimeters, e.g. ``width = Mm(240.5)``."""
9799

98-
def __new__(cls, mm):
100+
def __new__(cls, mm: float):
99101
emu = int(mm * Length._EMUS_PER_MM)
100102
return Length.__new__(cls, emu)
101103

102104

103105
class Pt(Length):
104106
"""Convenience value class for specifying a length in points."""
105107

106-
def __new__(cls, points):
108+
def __new__(cls, points: float):
107109
emu = int(points * Length._EMUS_PER_PT)
108110
return Length.__new__(cls, emu)
109111

@@ -114,7 +116,7 @@ class Twips(Length):
114116
A twip is a twentieth of a point, 635 EMU.
115117
"""
116118

117-
def __new__(cls, twips):
119+
def __new__(cls, twips: float):
118120
emu = int(twips * Length._EMUS_PER_TWIP)
119121
return Length.__new__(cls, emu)
120122

@@ -263,7 +265,7 @@ def __set__(self, obj: Any, value: Any) -> None:
263265
raise AttributeError("can't set attribute")
264266

265267

266-
def write_only_property(f):
268+
def write_only_property(f: Callable[[Any, Any], None]):
267269
"""@write_only_property decorator.
268270
269271
Creates a property (descriptor attribute) that accepts assignment, but not getattr
@@ -282,11 +284,13 @@ class ElementProxy:
282284
common type of class in python-docx other than custom element (oxml) classes.
283285
"""
284286

285-
def __init__(self, element: BaseOxmlElement, parent: Any | None = None):
287+
def __init__(
288+
self, element: BaseOxmlElement, parent: t.ProvidesXmlPart | None = None
289+
):
286290
self._element = element
287291
self._parent = parent
288292

289-
def __eq__(self, other):
293+
def __eq__(self, other: object):
290294
"""Return |True| if this proxy object refers to the same oxml element as does
291295
`other`.
292296
@@ -298,7 +302,7 @@ def __eq__(self, other):
298302
return False
299303
return self._element is other._element
300304

301-
def __ne__(self, other):
305+
def __ne__(self, other: object):
302306
if not isinstance(other, ElementProxy):
303307
return True
304308
return self._element is not other._element
@@ -309,8 +313,10 @@ def element(self):
309313
return self._element
310314

311315
@property
312-
def part(self):
316+
def part(self) -> XmlPart:
313317
"""The package part containing this object."""
318+
if self._parent is None:
319+
raise ValueError("part is not accessible from this element")
314320
return self._parent.part
315321

316322

@@ -322,7 +328,7 @@ class Parented:
322328
Provides ``self._parent`` attribute to subclasses.
323329
"""
324330

325-
def __init__(self, parent):
331+
def __init__(self, parent: t.ProvidesXmlPart):
326332
self._parent = parent
327333

328334
@property
@@ -342,7 +348,7 @@ class StoryChild:
342348
Provides `self._parent` attribute to subclasses.
343349
"""
344350

345-
def __init__(self, parent: StoryChild):
351+
def __init__(self, parent: t.ProvidesStoryPart):
346352
self._parent = parent
347353

348354
@property

0 commit comments

Comments
 (0)