11"""Run-related proxy objects for python-docx, Run in particular."""
22
3+ from __future__ import annotations
4+
5+ from typing import IO
6+
7+ from docx import types as t
38from docx .enum .style import WD_STYLE_TYPE
49from docx .enum .text import WD_BREAK
10+ from docx .oxml .text .run import CT_R , CT_Text
511from docx .shape import InlineShape
6- from docx .shared import Parented
12+ from docx .shared import Length , Parented
13+ from docx .styles .style import CharacterStyle
714from docx .text .font import Font
815
916
1017class Run (Parented ):
11- """Proxy object wrapping `` <w:r>` ` element.
18+ """Proxy object wrapping `<w:r>` element.
1219
A935
1320 Several of the properties on Run take a tri-state value, |True|, |False|, or |None|.
1421 |True| and |False| correspond to on and off respectively. |None| indicates the
1522 property is not specified directly on the run and its effective value is taken from
1623 the style hierarchy.
1724 """
1825
19- def __init__ (self , r , parent ):
26+ def __init__ (self , r : CT_R , parent : t . StoryChild ):
2027 super (Run , self ).__init__ (parent )
2128 self ._r = self ._element = self .element = r
2229
23- def add_break (self , break_type : WD_BREAK = WD_BREAK .LINE ):
30+ def add_break (self , break_type : WD_BREAK = WD_BREAK .LINE ): # pyright: ignore
2431 """Add a break element of `break_type` to this run.
2532
2633 `break_type` can take the values `WD_BREAK.LINE`, `WD_BREAK.PAGE`, and
@@ -41,7 +48,12 @@ def add_break(self, break_type: WD_BREAK = WD_BREAK.LINE):
4148 if clear is not None :
4249 br .clear = clear
4350
44- def add_picture (self , image_path_or_stream , width = None , height = None ):
51+ def add_picture (
52+ self ,
53+ image_path_or_stream : str | IO [bytes ],
54+ width : Length | None = None ,
55+ height : Length | None = None ,
56+ ) -> InlineShape :
4557 """Return an |InlineShape| instance containing the image identified by
4658 `image_path_or_stream`, added to the end of this run.
4759
@@ -62,7 +74,7 @@ def add_tab(self):
6274 tab character."""
6375 self ._r ._add_tab ()
6476
65- def add_text (self , text ):
77+ def add_text (self , text : str ):
6678 """Returns a newly appended |_Text| object (corresponding to a new ``<w:t>``
6779 child element) to the run, containing `text`.
6880
@@ -73,15 +85,15 @@ def add_text(self, text):
7385 return _Text (t )
7486
7587 @property
76- def bold (self ):
88+ def bold (self ) -> bool :
7789 """Read/write.
7890
7991 Causes the text of the run to appear in bold.
8092 """
8193 return self .font .bold
8294
8395 @bold .setter
84- def bold (self , value ):
96+ def bold (self , value : bool ):
8597 self .font .bold = value
8698
8799 def clear (self ):
@@ -99,19 +111,19 @@ def font(self):
99111 return Font (self ._element )
100112
101113 @property
102- def italic (self ):
114+ def italic (self ) -> bool :
103115 """Read/write tri-state value.
104116
105117 When |True|, causes the text of the run to appear in italics.
106118 """
107119 return self .font .italic
108120
109121 @italic .setter
110- def italic (self , value ):
122+ def italic (self , value : bool ):
111123 self .font .italic = value
112124
113125 @property
114- def style (self ):
126+ def style (self ) -> CharacterStyle | None :
115127 """Read/write.
116128
117129 A |_CharacterStyle| object representing the character style applied to this run.
@@ -123,7 +135,7 @@ def style(self):
123135 return self .part .get_style (style_id , WD_STYLE_TYPE .CHARACTER )
124136
125137 @style .setter
126- def style (self , style_or_name ):
138+ def style (self , style_or_name : str | CharacterStyle | None ):
127139 style_id = self .part .get_style_id (style_or_name , WD_STYLE_TYPE .CHARACTER )
128140 self ._r .style = style_id
129141
@@ -146,11 +158,11 @@ def text(self) -> str:
146158 return self ._r .text
147159
148160 @text .setter
149- def text (self , text ):
161+ def text (self , text : str ):
150162 self ._r .text = text
151163
152164 @property
153- def underline (self ):
165+ def underline (self ) -> bool :
154166 """The underline style for this |Run|, one of |None|, |True|, |False|, or a
155167 value from :ref:`WdUnderline`.
156168
@@ -165,13 +177,13 @@ def underline(self):
165177 return self .font .underline
166178
167179 @underline .setter
168- def underline (self , value ):
180+ def underline (self , value : bool ):
169181 self .font .underline = value
170182
171183
172184class _Text (object ):
173185 """Proxy object wrapping `<w:t>` element."""
174186
175- def __init__ (self , t_elm ):
187+ def __init__ (self , t_elm : CT_Text ):
176188 super (_Text , self ).__init__ ()
177189 self ._t = t_elm
0 commit comments