[py] fix mypy errors with types and type hints#16827
[py] fix mypy errors with types and type hints#16827iampopovich wants to merge 7 commits intoSeleniumHQ:trunkfrom
Conversation
selenium/webdriver/remote/webdriver.py:1048: error: Item "None" of Module | None has no attribute "import_devtools" [union-attr]
selenium/webdriver/common/webdriver.py:42: error: "LocalWebDriver" has no attribute "service" [attr-defined]
…ection selenium/webdriver/common/bidi/cdp.py:303: error: Item "None" of "Any | None" has no attribute "util" [union-attr] selenium/webdriver/common/bidi/cdp.py:427: error: Item "None" of "Any | None" has no attribute "target" [union-attr]
…tion selenium/webdriver/remote/remote_connection.py:419: error: Item "None" of "ClientConfig | None" has no attribute "keep_alive" [union-attr]
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||||||
There was a problem hiding this comment.
Pull request overview
This pull request enhances type safety and error handling in the Selenium Python WebDriver by adding defensive validation checks and type annotations to prevent runtime errors when required modules or configurations are missing.
Key Changes:
- Added null checks for CDP version and module initialization before attempting operations
- Added validation that
command_executoris aRemoteConnectioninstance before accessing its properties - Added type annotation for the
serviceattribute inLocalWebDriver
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
py/selenium/webdriver/remote/webdriver.py |
Added null checks for CDP version in capabilities and validation that command_executor is RemoteConnection before using CDP/BiDi features |
py/selenium/webdriver/remote/remote_connection.py |
Added runtime check to ensure _client_config is initialized before making requests |
py/selenium/webdriver/common/webdriver.py |
Added type annotation and import for service attribute to clarify it should be set by subclasses |
py/selenium/webdriver/common/bidi/cdp.py |
Added checks to ensure devtools module is loaded before handling events or connecting sessions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if cdp is None: | ||
| raise WebDriverException("CDP module not loaded") |
There was a problem hiding this comment.
The check for whether the CDP module is loaded should occur immediately after calling import_cdp() on line 1035, before any processing that depends on the cdp module. Moving this check earlier would catch the issue sooner and prevent unnecessary processing of CDP version information when the module isn't available.
|
I added some of these changes to #16828 along with fixing the rest of the other type errors and some other fixes. I'm going to close this PR, but thanks for the help! |
User description
🔗 Related Issues
relates to #15697
💥 What does this PR do?
These changes help prevent runtime errors by providing clear error messages when required modules or configuration are missing. Additionally, there are minor enhancements to type annotations and imports.
Dependency and configuration validation:
cdp.py, raising aRuntimeErrorif not. [1] [2]remote_connection.py, added a check to ensure_client_configis initialized before making a request, raising aRuntimeErrorotherwise.remote/webdriver.py, added checks instart_devtoolsand_start_bidito ensure the CDP version is present in capabilities, the CDP module is loaded, and thatcommand_executoris aRemoteConnectioninstance, raising clear exceptions if any are missing. [1] [2]Codebase clarity and maintainability:
serviceattribute inLocalWebDriver, clarifying that it should be set by subclasses.🔧 Implementation Notes
This pull request introduces several defensive programming improvements to the Selenium WebDriver codebase, primarily focused on validating dependencies and configuration before proceeding with key operations. Optional arguments were not changed to not to break backward compatibility.
💡 Additional Considerations
🔄 Types of changes
PR Type
Bug fix, Enhancement
Description
Add runtime validation checks for CDP and BiDi module dependencies
Validate command_executor type and ClientConfig initialization before use
Add explicit error messages for missing CDP version in capabilities
Add Service type annotation to LocalWebDriver class
Diagram Walkthrough
File Walkthrough
cdp.py
Add devtools module validation in CDP methodspy/selenium/webdriver/common/bidi/cdp.py
_handle_event()to ensure devtools module isloaded before accessing util
connect_session()to ensure devtools module isloaded before accessing target
None
remote_connection.py
Add ClientConfig initialization validationpy/selenium/webdriver/remote/remote_connection.py
_request()method to validate _client_config isinitialized
accessing keep_alive
webdriver.py
Add comprehensive validation for CDP and BiDi supportpy/selenium/webdriver/remote/webdriver.py
start_devtools()with descriptiveWebDriverException
import_devtools()
instance for CDP support
instance for BiDi support
webdriver.py
Add Service type annotation to LocalWebDriverpy/selenium/webdriver/common/webdriver.py
super().__init__()