Skip to content

Commit e46dba5

Browse files
[py] PEP 484 type hints for selenium.webdriver.common.print_page_options (#9608)
Signed-off-by: oleg.hoefling <[email protected]> Co-authored-by: David Burns <[email protected]>
1 parent d2333a2 commit e46dba5

File tree

2 files changed

+70
-33
lines changed

2 files changed

+70
-33
lines changed

py/selenium/webdriver/common/print_page_options.py

+67-31
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,66 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
19-
class PrintOptions():
18+
import sys
19+
from typing import TYPE_CHECKING, List, Optional
20+
21+
# necessary to support types for Python 3.7
22+
if TYPE_CHECKING:
23+
if sys.version_info >= (3, 8):
24+
from typing import Literal, TypedDict
25+
else:
26+
from typing_extensions import Literal, TypedDict
27+
28+
Orientation = Literal['portrait', 'landscape']
29+
30+
class _MarginOpts(TypedDict, total=False):
31+
left: float
32+
right: float
33+
top: float
34+
bottom: float
35+
36+
class _PageOpts(TypedDict, total=False):
37+
width: float
38+
height: float
39+
40+
class _PrintOpts(TypedDict, total=False):
41+
margin: _MarginOpts
42+
page: _PageOpts
43+
background: bool
44+
orientation: Orientation
45+
scale: float
46+
shrinkToFit: bool
47+
pageRanges: List[str]
48+
else:
49+
from typing import Any, Dict
50+
51+
Orientation = str
52+
_MarginOpts = _PageOpts = _PrintOpts = Dict[str, Any]
53+
54+
55+
class PrintOptions:
2056
ORIENTATION_VALUES = ['portrait', 'landscape']
2157

22-
def __init__(self):
23-
self._print_options = {}
24-
self._page = {}
25-
self._margin = {}
58+
def __init__(self) -> None:
59+
self._print_options: _PrintOpts = {}
60+
self._page: _PageOpts = {}
61+
self._margin: _MarginOpts = {}
2662

27-
def to_dict(self):
63+
def to_dict(self) -> _PrintOpts:
2864
"""
2965
:Returns: A hash of print options configured
3066
"""
3167
return self._print_options
3268

3369
@property
34-
def orientation(self):
70+
def orientation(self) -> Optional[Orientation]:
3571
"""
3672
:Returns: Orientation that was set for the page
3773
"""
3874
return self._print_options.get('orientation', None)
3975

4076
@orientation.setter
41-
def orientation(self, value):
77+
def orientation(self, value: Orientation) -> None:
4278
"""
4379
Allows you to set orientation of the page
4480
:Args:
@@ -50,14 +86,14 @@ def orientation(self, value):
5086
self._print_options['orientation'] = value
5187

5288
@property
53-
def scale(self):
89+
def scale(self) -> Optional[float]:
5490
"""
5591
:Returns: Scale that was set for the page
5692
"""
5793
return self._print_options.get('scale', None)
5894

5995
@scale.setter
60-
def scale(self, value):
96+
def scale(self, value: float) -> None:
6197
"""
6298
Allows you to to set scale for the page
6399
:Args:
@@ -71,14 +107,14 @@ def scale(self, value):
71107
self._print_options['scale'] = value
72108

73109
@property
74-
def background(self):
110+
def background(self) -> Optional[bool]:
75111
"""
76112
:Returns: Background value that was set
77113
"""
78114
return self._print_options.get('background', None)
79115

80116
@background.setter
81-
def background(self, value):
117+
def background(self, value: bool) -> None:
82118
"""
83119
Allows you to set the boolean value for the background
84120
:Args:
@@ -89,14 +125,14 @@ def background(self, value):
89125
self._print_options['background'] = value
90126

91127
@property
92-
def page_width(self):
128+
def page_width(self) -> Optional[float]:
93129
"""
94130
:Returns: Page width that was set
95131
"""
96132
return self._page.get('width', None)
97133

98134
@page_width.setter
99-
def page_width(self, value):
135+
def page_width(self, value: float) -> None:
100136
"""
101137
Allows you to set width of the page
102138
:Args:
@@ -108,14 +144,14 @@ def page_width(self, value):
108144
self._print_options['page'] = self._page
109145

110146
@property
111-
def page_height(self):
147+
def page_height(self) -> Optional[float]:
112148
"""
113149
:Returns: Page height that was set
114150
"""
115151
return self._page.get('height', None)
116152

117153
@page_height.setter
118-
def page_height(self, value):
154+
def page_height(self, value: float) -> None:
119155
"""
120156
Allows you to set height of the page
121157
:Args:
@@ -127,14 +163,14 @@ def page_height(self, value):
127163
self._print_options['page'] = self._page
128164

129165
@property
130-
def margin_top(self):
166+
def margin_top(self) -> Optional[float]:
131167
"""
132168
:Returns: Top margin of the page
133169
"""
134170
return self._margin.get('top', None)
135171

136172
@margin_top.setter
137-
def margin_top(self, value):
173+
def margin_top(self, value: float) -> None:
138174
"""
139175
Allows you to set top margin of the page
140176
:Args:
@@ -146,14 +182,14 @@ def margin_top(self, value):
146182
self._print_options['margin'] = self._margin
147183

148184
@property
149-
def margin_left(self):
185+
def margin_left(self) -> Optional[float]:
150186
"""
151187
:Returns: Left margin of the page
152188
"""
153189
return self._margin.get('left', None)
154190

155191
@margin_left.setter
156-
def margin_left(self, value):
192+
def margin_left(self, value: float) -> None:
157193
"""
158194
Allows you to set left margin of the page
159195
:Args:
@@ -165,14 +201,14 @@ def margin_left(self, value):
165201
self._print_options['margin'] = self._margin
166202

167203
@property
168-
def margin_bottom(self):
204+
def margin_bottom(self) -> Optional[float]:
169205
"""
170206
:Returns: Bottom margin of the page
171207
"""
172208
return self._margin.get('bottom', None)
173209

174210
@margin_bottom.setter
175-
def margin_bottom(self, value):
211+
def margin_bottom(self, value: float) -> None:
176212
"""
177213
Allows you to set bottom margin of the page
178214
:Args:
@@ -184,14 +220,14 @@ def margin_bottom(self, value):
184220
self._print_options['margin'] = self._margin
185221

186222
@property
187-
def margin_right(self):
223+
def margin_right(self) -> Optional[float]:
188224
"""
189225
:Returns: Right margin of the page
190226
"""
191227
return self._margin.get('right', None)
192228

193229
@margin_right.setter
194-
def margin_right(self, value):
230+
def margin_right(self, value: float) -> None:
195231
"""
196232
Allows you to set right margin of the page
197233
:Args:
@@ -203,14 +239,14 @@ def margin_right(self, value):
203239
self._print_options['margin'] = self._margin
204240

205241
@property
206-
def shrink_to_fit(self):
242+
def shrink_to_fit(self) -> Optional[bool]:
207243
"""
208244
:Returns: Value set for shrinkToFit
209245
"""
210246
return self._print_options.get('shrinkToFit', None)
211247

212248
@shrink_to_fit.setter
213-
def shrink_to_fit(self, value):
249+
def shrink_to_fit(self, value: bool) -> None:
214250
"""
215251
Allows you to set shrinkToFit
216252
:Args:
@@ -221,14 +257,14 @@ def shrink_to_fit(self, value):
221257
self._print_options['shrinkToFit'] = value
222258

223259
@property
224-
def page_ranges(self):
260+
def page_ranges(self) -> Optional[List[str]]:
225261
"""
226262
:Returns: value set for pageRanges
227263
"""
228264
return self._print_options.get('pageRanges', None)
229265

230266
@page_ranges.setter
231-
def page_ranges(self, value):
267+
def page_ranges(self, value: List[str]) -> None:
232268
"""
233269
Allows you to set pageRanges for the print command
234270
:Args:
@@ -238,11 +274,11 @@ def page_ranges(self, value):
238274
raise ValueError('Page ranges should be a list')
239275
self._print_options['pageRanges'] = value
240276

241-
def __validate_num_property(self, property_name, value):
277+
def __validate_num_property(self, property_name: str, value: float) -> None:
242278
"""
243279
Helper function to validate some of the properties
244280
"""
245-
if not isinstance(value, float) and not isinstance(value, int):
281+
if not isinstance(value, (int, float)):
246282
raise ValueError(f'{property_name} should be an integer or a float')
247283

248284
if value < 0:

py/selenium/webdriver/remote/webdriver.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import pkgutil
2323
import sys
24-
from typing import Dict, List, Union
24+
from typing import Dict, List, Optional, Union
2525

2626
import warnings
2727

@@ -48,6 +48,7 @@
4848
from selenium.webdriver.common.timeouts import Timeouts
4949
from selenium.webdriver.common.html5.application_cache import ApplicationCache
5050
from selenium.webdriver.support.relative_locator import RelativeBy
51+
from selenium.webdriver.common.print_page_options import PrintOptions
5152

5253

5354
_W3C_CAPABILITY_NAMES = frozenset([
@@ -906,7 +907,7 @@ def minimize_window(self) -> None:
906907
"""
907908
self.execute(Command.MINIMIZE_WINDOW)
908909

909-
def print_page(self, print_options=None) -> str:
910+
def print_page(self, print_options: Optional[PrintOptions] = None) -> str:
910911
"""
911912
Takes PDF of the current page.
912913
The driver makes a best effort to return a PDF based on the provided parameters.

0 commit comments

Comments
 (0)