-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[py] fix some mypy warns about types #16551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
selenium/webdriver/common/virtual_authenticator.py:116: error: Incompatible return value type (got "Optional[str]", expected "str") [return-value]
…afety selenium/webdriver/remote/webdriver.py:1062: error: Incompatible return value type (got "tuple[None, None]", expected "tuple[Any, WebSocketConnection]") [return-value] selenium/webdriver/remote/webdriver.py:1095: error: Incompatible return value type (got "None", expected "Script") [return-value] selenium/webdriver/remote/webdriver.py:1117: error: Incompatible return value type (got "None", expected "Network") [return-value] selenium/webdriver/remote/webdriver.py:1138: error: Incompatible return value type (got "None", expected "Browser") [return-value] selenium/webdriver/remote/webdriver.py:1149: error: Incompatible return value type (got "None", expected "Session") [return-value] selenium/webdriver/remote/webdriver.py:1170: error: Incompatible return value type (got "None", expected "BrowsingContext") [return-value] selenium/webdriver/remote/webdriver.py:1195: error: Incompatible return value type (got "None", expected "Storage") [return-value] selenium/webdriver/remote/webdriver.py:1218: error: Incompatible return value type (got "None", expected "Permissions") [return-value] selenium/webdriver/remote/webdriver.py:1238: error: Incompatible return value type (got "None", expected "WebExtension") [return-value] selenium/webdriver/remote/webdriver.py:1261: error: Incompatible return value type (got "None", expected "Emulation") [return-value] selenium/webdriver/remote/webdriver.py:1285: error: Incompatible return value type (got "None", expected "Input") [return-value]
…initialization selenium/webdriver/chromium/webdriver.py:61: error: Argument "browser_name" to "ChromiumRemoteConnection" has incompatible type "Optional[str]"; expected "str" [arg-type] selenium/webdriver/chromium/webdriver.py:62: error: Argument "vendor_prefix" to "ChromiumRemoteConnection" has incompatible type "Optional[str]"; expected "str" [arg-type]
selenium/webdriver/webkitgtk/options.py:67: error: Incompatible types in assignment (expression has type "bool", target has type "str") [assignment]
…tributes selenium/webdriver/safari/options.py:68: error: Incompatible types in assignment (expression has type "_SafariOptionsDescriptor", variable has type "bool") [assignment] selenium/webdriver/safari/options.py:71: error: Incompatible types in assignment (expression has type "_SafariOptionsDescriptor", variable has type "bool") [assignment] selenium/webdriver/safari/options.py:74: error: Incompatible types in assignment (expression has type "_SafariOptionsDescriptor", variable has type "bool") [assignment]
|
@cgoldberg I've edited the most basic type warnings and added type hints where they were obvious. Please check the RDL file; is the type specification there correct? |
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:
|
|||||||||
|
|
||
| @property | ||
| def rp_id(self) -> str: | ||
| def rp_id(self) -> Optional[str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will _rp_id ever not be a string? If it might be None, we should define the return as: str | None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_rp_id can be None if rp_id is not in the dictionary and is assigned the default value None.
| rp_id = data.get("rpId", None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it might be None, we should define the return as: str | None
why not Optional[str] , isn't it equal to str | None ?
User description
🔗 Related Issues
relates to #15697
💥 What does this PR do?
This pull request introduces several improvements and refactorings across multiple modules to enhance type safety, code clarity, and error handling. The most significant changes include updating type annotations to use modern Python syntax, replacing deprecated imports, and adding explicit error checks.
Type annotations and code clarity improvements:
common/devtools/pdl.pyfunctions (assignType,createItem,parse, andloads) to use explicit types and modern Python syntax, improving readability and static analysis. [1] [2] [3] [4]py/selenium/webdriver/remote/webdriver.pyto use explicitOptional[...]type annotations for better clarity and type checking.rp_idproperty inpy/selenium/webdriver/common/virtual_authenticator.pytoOptional[str], reflecting that it may beNone.py/selenium/webdriver/safari/options.pyto avoid potential issues with descriptor objects.py/selenium/webdriver/webkitgtk/options.pyand updated the type ofbrowser_optionsfor consistency. [1] [2]Error handling enhancements:
browser_nameandvendor_prefixin the Chromium WebDriver initialization, raisingValueErrorif they are not specified.🔧 Implementation Notes
💡 Additional Considerations
🔄 Types of changes
PR Type
Bug fix, Enhancement
Description
Fix mypy type warnings across multiple modules
Add explicit type annotations to function signatures
Validate browser_name and vendor_prefix in ChromiumDriver
Update property return types to reflect Optional values
Replace deprecated imports with modern Python syntax
Diagram Walkthrough
File Walkthrough
pdl.py
Add type annotations and modernize importscommon/devtools/pdl.py
import collectionswithfrom collections import OrderedDictfrom typing import AnyimportassignType(),createItem(),parse(), andloads()functionscollections.OrderedDict()calls to use importedOrderedDict()webdriver.py
Add Optional type annotations to BiDi attributespy/selenium/webdriver/remote/webdriver.py
Optionaltype annotations to all BiDi-related instancevariables
_websocket_connection,_script,_network,_browser,_bidi_session,_browsing_context,_storage,_webextension,_permissions,_emulation,_input, and_devtoolswith proper type hintsoptions.py
Add type annotation to browser_options variablepy/selenium/webdriver/webkitgtk/options.py
from typing import Anyimportdict[str, Any]tobrowser_optionsvariable
webdriver.py
Add parameter validation for ChromiumDriverpy/selenium/webdriver/chromium/webdriver.py
browser_nameandvendor_prefixparameters
ValueErrorif either parameter isNoneduring initializationvirtual_authenticator.py
Fix rp_id property return type annotationpy/selenium/webdriver/common/virtual_authenticator.py
rp_idproperty return type fromstrtoOptional[str]Noneoptions.py
Remove type annotations from descriptor assignmentspy/selenium/webdriver/safari/options.py
automatic_inspection,automatic_profiling, anduse_technology_preview