Skip to content

Commit 09a4b24

Browse files
authored
[py] Upgrade type hints (#15784)
- Upgrade type hints to Python 3.9+ syntax throughout codebase - Replace legacy List, Dict, Tuple, etc. with built-in generics (e.g., list, dict, tuple) - Move type hints to use collections.abc where appropriate
1 parent 79a0939 commit 09a4b24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+201
-205
lines changed

py/selenium/common/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# under the License.
1717
"""Exceptions that may happen in all the webdriver code."""
1818

19-
from typing import Optional, Sequence
19+
from collections.abc import Sequence
20+
from typing import Optional
2021

2122
SUPPORT_MSG = "For documentation on this error, please visit:"
2223
ERROR_URL = "https://www.selenium.dev/documentation/webdriver/troubleshooting/errors"

py/selenium/types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
# under the License.
1717
"""Selenium type definitions."""
1818

19-
from typing import IO, Any, Iterable, Type, Union
19+
from collections.abc import Iterable
20+
from typing import IO, Any, Union
2021

2122
AnyKey = Union[str, int, float]
22-
WaitExcTypes = Iterable[Type[Exception]]
23+
WaitExcTypes = Iterable[type[Exception]]
2324

2425
# Service Types
2526
SubprocessStdAlias = Union[int, str, IO[Any]]

py/selenium/webdriver/chrome/service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# under the License.
1717

1818

19-
from typing import List, Mapping, Optional
19+
from collections.abc import Mapping
20+
from typing import Optional
2021

2122
from selenium.types import SubprocessStdAlias
2223
from selenium.webdriver.chromium import service
@@ -37,7 +38,7 @@ def __init__(
3738
self,
3839
executable_path: Optional[str] = None,
3940
port: int = 0,
40-
service_args: Optional[List[str]] = None,
41+
service_args: Optional[list[str]] = None,
4142
log_output: Optional[SubprocessStdAlias] = None,
4243
env: Optional[Mapping[str, str]] = None,
4344
**kwargs,

py/selenium/webdriver/chromium/options.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import base64
1919
import os
20-
from typing import BinaryIO, Dict, List, Optional, Union
20+
from typing import BinaryIO, Optional, Union
2121

2222
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2323
from selenium.webdriver.common.options import ArgOptions
@@ -29,9 +29,9 @@ class ChromiumOptions(ArgOptions):
2929
def __init__(self) -> None:
3030
super().__init__()
3131
self._binary_location: str = ""
32-
self._extension_files: List[str] = []
33-
self._extensions: List[str] = []
34-
self._experimental_options: Dict[str, Union[str, int, dict, List[str]]] = {}
32+
self._extension_files: list[str] = []
33+
self._extensions: list[str] = []
34+
self._experimental_options: dict[str, Union[str, int, dict, list[str]]] = {}
3535
self._debugger_address: Optional[str] = None
3636

3737
@property
@@ -68,7 +68,7 @@ def debugger_address(self, value: str) -> None:
6868
self._debugger_address = value
6969

7070
@property
71-
def extensions(self) -> List[str]:
71+
def extensions(self) -> list[str]:
7272
""":Returns: A list of encoded extensions that will be loaded."""
7373

7474
def _decode(file_data: BinaryIO) -> str:
@@ -117,7 +117,7 @@ def experimental_options(self) -> dict:
117117
""":Returns: A dictionary of experimental options for chromium."""
118118
return self._experimental_options
119119

120-
def add_experimental_option(self, name: str, value: Union[str, int, dict, List[str]]) -> None:
120+
def add_experimental_option(self, name: str, value: Union[str, int, dict, list[str]]) -> None:
121121
"""Adds an experimental option which is passed to chromium.
122122
123123
:Args:

py/selenium/webdriver/chromium/service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
from collections.abc import Mapping
1718
from io import IOBase
18-
from typing import List, Mapping, Optional
19+
from typing import Optional
1920

2021
from selenium.types import SubprocessStdAlias
2122
from selenium.webdriver.common import service
@@ -37,7 +38,7 @@ def __init__(
3738
self,
3839
executable_path: Optional[str] = None,
3940
port: int = 0,
40-
service_args: Optional[List[str]] = None,
41+
service_args: Optional[list[str]] = None,
4142
log_output: Optional[SubprocessStdAlias] = None,
4243
env: Optional[Mapping[str, str]] = None,
4344
driver_path_env_key: Optional[str] = None,
@@ -63,5 +64,5 @@ def __init__(
6364
**kwargs,
6465
)
6566

66-
def command_line_args(self) -> List[str]:
67+
def command_line_args(self) -> list[str]:
6768
return [f"--port={self.port}"] + self.service_args

py/selenium/webdriver/common/actions/action_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from typing import List, Optional, Union
18+
from typing import Optional, Union
1919

2020
from selenium.webdriver.remote.command import Command
2121

@@ -61,11 +61,11 @@ def get_device_with(self, name: str) -> Optional[Union["WheelInput", "PointerInp
6161
return next(filter(lambda x: x == name, self.devices), None)
6262

6363
@property
64-
def pointer_inputs(self) -> List[PointerInput]:
64+
def pointer_inputs(self) -> list[PointerInput]:
6565
return [device for device in self.devices if device.type == interaction.POINTER]
6666

6767
@property
68-
def key_inputs(self) -> List[KeyInput]:
68+
def key_inputs(self) -> list[KeyInput]:
6969
return [device for device in self.devices if device.type == interaction.KEY]
7070

7171
@property

py/selenium/webdriver/common/actions/input_device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
# under the License.
1717

1818
import uuid
19-
from typing import Any, List, Optional
19+
from typing import Any, Optional
2020

2121

2222
class InputDevice:
2323
"""Describes the input device being used for the action."""
2424

2525
def __init__(self, name: Optional[str] = None):
2626
self.name = name or uuid.uuid4()
27-
self.actions: List[Any] = []
27+
self.actions: list[Any] = []
2828

2929
def add_action(self, action: Any) -> None:
3030
""""""

py/selenium/webdriver/common/actions/interaction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
from typing import Dict, Union
17+
from typing import Union
1818

1919
KEY = "key"
2020
POINTER = "pointer"
@@ -41,5 +41,5 @@ def __init__(self, source, duration: float = 0) -> None:
4141
super().__init__(source)
4242
self.duration = duration
4343

44-
def encode(self) -> Dict[str, Union[str, int]]:
44+
def encode(self) -> dict[str, Union[str, int]]:
4545
return {"type": self.PAUSE, "duration": int(self.duration * 1000)}

py/selenium/webdriver/common/actions/pointer_input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from typing import Any, Dict, Optional, Union
18+
from typing import Any, Optional, Union
1919

2020
from selenium.common.exceptions import InvalidArgumentException
2121
from selenium.webdriver.remote.webelement import WebElement
@@ -66,7 +66,7 @@ def create_pause(self, pause_duration: Union[int, float] = 0) -> None:
6666
def encode(self):
6767
return {"type": self.type, "parameters": {"pointerType": self.kind}, "id": self.name, "actions": self.actions}
6868

69-
def _convert_keys(self, actions: Dict[str, Any]):
69+
def _convert_keys(self, actions: dict[str, Any]):
7070
out = {}
7171
for k, v in actions.items():
7272
if v is None:

py/selenium/webdriver/common/bidi/browser.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from typing import Dict, List
1918

2019
from selenium.webdriver.common.bidi.common import command_builder
2120

@@ -114,7 +113,7 @@ def is_active(self) -> bool:
114113
return self.active
115114

116115
@classmethod
117-
def from_dict(cls, data: Dict) -> "ClientWindowInfo":
116+
def from_dict(cls, data: dict) -> "ClientWindowInfo":
118117
"""Creates a ClientWindowInfo instance from a dictionary.
119118
120119
Parameters:
@@ -154,7 +153,7 @@ def create_user_context(self) -> str:
154153
result = self.conn.execute(command_builder("browser.createUserContext", {}))
155154
return result["userContext"]
156155

157-
def get_user_contexts(self) -> List[str]:
156+
def get_user_contexts(self) -> list[str]:
158157
"""Gets all user contexts.
159158
160159
Returns:
@@ -181,7 +180,7 @@ def remove_user_context(self, user_context_id: str) -> None:
181180
params = {"userContext": user_context_id}
182181
self.conn.execute(command_builder("browser.removeUserContext", params))
183182

184-
def get_client_windows(self) -> List[ClientWindowInfo]:
183+
def get_client_windows(self) -> list[ClientWindowInfo]:
185184
"""Gets all client windows.
186185
187186
Returns:

0 commit comments

Comments
 (0)