Skip to content

[py] correct type annotations of default-None params #15341

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

Merged
merged 8 commits into from
May 14, 2025

Conversation

DeflateAwning
Copy link
Contributor

@DeflateAwning DeflateAwning commented Feb 25, 2025

User description

Corrected type hints/annotations, mostly of arguments with default values of None.

Also added a few entirely-missing type hints, mostly for strings.

Motivation and Context

Pyright type checker was throwing errors in many of these cases.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

None of the other checklist items are required.

Recommend a squash merge.


PR Type

Bug fix, Enhancement


Description

  • Corrected type annotations for parameters with default None values.

  • Added missing type hints, primarily for strings and lists.

  • Improved type safety by using Optional and explicit type declarations.

  • Enhanced code clarity and compatibility with type checkers like Pyright.


Changes walkthrough 📝

Relevant files
Bug fix
18 files
service.py
Updated type hints for `log_output` parameter.                     
+1/-1     
webdriver.py
Added `Optional` type hints for `options` and `service`. 
+4/-2     
service.py
Corrected type hints for executable_path, log_output, and
driver_path_env_key.
+3/-3     
webdriver.py
Added `Optional` type hints for multiple parameters.         
+5/-3     
wheel_actions.py
Updated type hint for `source` parameter.                               
+4/-1     
service.py
Corrected type hints for multiple parameters including `log_output`.
+3/-3     
service.py
Updated type hints for executable_path, log_output, and
driver_path_env_key.
+3/-3     
webdriver.py
Added `Optional` type hints for `options` and `service`. 
+4/-2     
service.py
Corrected type hints for executable_path, log_output, and
driver_path_env_key.
+3/-3     
webdriver.py
Added `Optional` type hints for `options` and `service`. 
+3/-2     
service.py
Updated type hints for executable_path, log_output, and
driver_path_env_key.
+3/-3     
webdriver.py
Added `Optional` type hints for `options` and `service`. 
+4/-2     
remote_connection.py
Added `Optional` type hint for `_client_config`.                 
+1/-1     
service.py
Corrected type hints for `executable_path` and `driver_path_env_key`.
+2/-2     
webdriver.py
Added `Optional` type hints for `options` and `service`. 
+4/-2     
event_firing_webdriver.py
Updated type hint for `execute_script` method.                     
+1/-1     
webdriver.py
Added `Optional` type hint for `service` parameter.           
+2/-1     
webdriver.py
Added `Optional` type hint for `service` parameter.           
+2/-1     
Enhancement
2 files
options.py
Added type hints for `arguments` and `add_argument` methods.
+3/-2     
webdriver.py
Enhanced type hints for `execute` and `execute_script` methods.
+3/-2     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @CLAassistant
    Copy link

    CLAassistant commented Feb 25, 2025

    CLA assistant check
    All committers have signed the CLA.

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Type Safety

    The execute() method's params parameter type was changed from 'dict' to 'dict[str, Any]'. Verify this doesn't break existing code that passes dictionaries with non-string keys.

    def execute(self, driver_command: str, params: dict[str, Any]) -> dict[str, Any]:
        """Sends a command to be executed by a command.CommandExecutor.

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 25, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Learned
    best practice
    Use explicit None checks instead of implicit truthiness checks when validating nullable parameters
    Suggestion Impact:Changed the source parameter check from 'if not source' to 'if source is None' for more explicit null checking

    code diff:

    -        if not source:
    +        if source is None:

    Add a null validation check for the source parameter at the start of the
    init method to make the code more defensive and prevent potential
    NullReferenceExceptions. The current code implicitly handles None but an
    explicit check would be clearer.

    py/selenium/webdriver/common/actions/wheel_actions.py [25-28]

     def __init__(self, source: Optional[WheelInput] = None):
    -    if not source:
    +    if source is None:
             source = WheelInput("wheel")
         super().__init__(source)

    [Suggestion has been applied]

    Suggestion importance[1-10]: 6
    Low
    • Update

    @cgoldberg
    Copy link
    Contributor

    Is this still relevant?

    @DeflateAwning
    Copy link
    Contributor Author

    Yes of coures it is still relevant. It's type hints. Not sure why it's taken so long to deal with

    @cgoldberg
    Copy link
    Contributor

    Not sure why it's taken so long to deal with

    It's a community project run by volunteers who put in a lot of effort and get paid nothing. We are pretty diligent with reviewing contributions, but not everything gets immediate attention.

    @DeflateAwning
    Copy link
    Contributor Author

    Let me know if I can help expedite things! There's no logic changes or anything, so it shouldn't take much to review

    @cgoldberg
    Copy link
    Contributor

    @DeflateAwning,

    This is causing test failures.

    I can reproduce when using your branch:

    >>> from selenium import webdriver
    >>> driver = webdriver.Chrome()
    >>> print(driver.title)
    Traceback (most recent call last):
      File "<python-input-2>", line 1, in <module>
        print(driver.title)
              ^^^^^^^^^^^^
      File "/home/cgoldberg617/code/selenium/py/selenium/webdriver/remote/webdriver.py", line 487, in title
        return self.execute(Command.GET_TITLE).get("value", "")
               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
    TypeError: WebDriver.execute() missing 1 required positional argument: 'params'
    

    Can you take a look?

    Copy link
    Contributor

    @cgoldberg cgoldberg left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    fix TypeError

    @DeflateAwning
    Copy link
    Contributor Author

    My bad, tiny mistake in my haste. Fixed! Can you please re-run CI?

    @cgoldberg
    Copy link
    Contributor

    CI failed, but it wasn't related to these changes... We have a flaky test. I'm running it again.

    Copy link
    Contributor

    @cgoldberg cgoldberg left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    @DeflateAwning
    CI is green and this looks good to go.

    Thanks for the contribution!

    @cgoldberg cgoldberg merged commit ff55efd into SeleniumHQ:trunk May 14, 2025
    18 of 19 checks passed
    @DeflateAwning
    Copy link
    Contributor Author

    Thanks a lot! Really appreciate it!

    @DeflateAwning DeflateAwning deleted the fix-type-hints branch May 14, 2025 00:18
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    4 participants