16
16
)
17
17
18
18
if TYPE_CHECKING :
19
+ from docx import types as t
20
+ from docx .opc .part import XmlPart
19
21
from docx .oxml .xmlchemy import BaseOxmlElement
20
22
from docx .parts .story import StoryPart
21
23
@@ -34,7 +36,7 @@ class Length(int):
34
36
_EMUS_PER_PT = 12700
35
37
_EMUS_PER_TWIP = 635
36
38
37
- def __new__ (cls , emu ):
39
+ def __new__ (cls , emu : int ):
38
40
return int .__new__ (cls , emu )
39
41
40
42
@property
@@ -71,15 +73,15 @@ def twips(self):
71
73
class Inches (Length ):
72
74
"""Convenience constructor for length in inches, e.g. ``width = Inches(0.5)``."""
73
75
74
- def __new__ (cls , inches ):
76
+ def __new__ (cls , inches : float ):
75
77
emu = int (inches * Length ._EMUS_PER_INCH )
76
78
return Length .__new__ (cls , emu )
77
79
78
80
79
81
class Cm (Length ):
80
82
"""Convenience constructor for length in centimeters, e.g. ``height = Cm(12)``."""
81
83
82
- def __new__ (cls , cm ):
84
+ def __new__ (cls , cm : float ):
83
85
emu = int (cm * Length ._EMUS_PER_CM )
84
86
return Length .__new__ (cls , emu )
85
87
@@ -88,22 +90,22 @@ class Emu(Length):
88
90
"""Convenience constructor for length in English Metric Units, e.g. ``width =
89
91
Emu(457200)``."""
90
92
91
- def __new__ (cls , emu ):
93
+ def __new__ (cls , emu : int ):
92
94
return Length .__new__ (cls , int (emu ))
93
95
94
96
95
97
class Mm (Length ):
96
98
"""Convenience constructor for length in millimeters, e.g. ``width = Mm(240.5)``."""
97
99
98
- def __new__ (cls , mm ):
100
+ def __new__ (cls , mm : float ):
99
101
emu = int (mm * Length ._EMUS_PER_MM )
100
102
return Length .__new__ (cls , emu )
101
103
102
104
103
105
class Pt (Length ):
104
106
"""Convenience value class for specifying a length in points."""
105
107
106
- def __new__ (cls , points ):
108
+ def __new__ (cls , points : float ):
107
109
emu = int (points * Length ._EMUS_PER_PT )
108
110
return Length .__new__ (cls , emu )
109
111
@@ -114,7 +116,7 @@ class Twips(Length):
114
116
A twip is a twentieth of a point, 635 EMU.
115
117
"""
116
118
117
- def __new__ (cls , twips ):
119
+ def __new__ (cls , twips : float ):
118
120
emu = int (twips * Length ._EMUS_PER_TWIP )
119
121
return Length .__new__ (cls , emu )
120
122
@@ -263,7 +265,7 @@ def __set__(self, obj: Any, value: Any) -> None:
263
265
raise AttributeError ("can't set attribute" )
264
266
265
267
266
- def write_only_property (f ):
268
+ def write_only_property (f : Callable [[ Any , Any ], None ] ):
267
269
"""@write_only_property decorator.
268
270
269
271
Creates a property (descriptor attribute) that accepts assignment, but not getattr
@@ -282,11 +284,13 @@ class ElementProxy:
282
284
common type of class in python-docx other than custom element (oxml) classes.
283
285
"""
284
286
285
- def __init__ (self , element : BaseOxmlElement , parent : Any | None = None ):
287
+ def __init__ (
288
+ self , element : BaseOxmlElement , parent : t .ProvidesXmlPart | None = None
289
+ ):
286
290
self ._element = element
287
291
self ._parent = parent
288
292
289
- def __eq__ (self , other ):
293
+ def __eq__ (self , other : object ):
290
294
"""Return |True| if this proxy object refers to the same oxml element as does
291
295
`other`.
292
296
@@ -298,7 +302,7 @@ def __eq__(self, other):
298
302
return False
299
303
return self ._element is other ._element
300
304
301
- def __ne__ (self , other ):
305
+ def __ne__ (self , other : object ):
302
306
if not isinstance (other , ElementProxy ):
303
307
return True
304
308
return self ._element is not other ._element
@@ -309,8 +313,10 @@ def element(self):
309
313
return self ._element
310
314
311
315
@property
312
- def part (self ):
316
+ def part (self ) -> XmlPart :
313
317
"""The package part containing this object."""
318
+ if self ._parent is None :
319
+ raise ValueError ("part is not accessible from this element" )
314
320
return self ._parent .part
315
321
316
322
@@ -322,7 +328,7 @@ class Parented:
322
328
Provides ``self._parent`` attribute to subclasses.
323
329
"""
324
330
325
- def __init__ (self , parent ):
331
+ def __init__ (self , parent : t . ProvidesXmlPart ):
326
332
self ._parent = parent
327
333
328
334
@property
@@ -342,7 +348,7 @@ class StoryChild:
342
348
Provides `self._parent` attribute to subclasses.
343
349
"""
344
350
345
- def __init__ (self , parent : StoryChild ):
351
+ def __init__ (self , parent : t . ProvidesStoryPart ):
346
352
self ._parent = parent
347
353
348
354
@property
0 commit comments