Skip to content

[py] Fix: Mypy type annotation errors #15841

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 4 commits into from
Jun 2, 2025

Conversation

ShauryaDusht
Copy link
Contributor

@ShauryaDusht ShauryaDusht commented Jun 1, 2025

User description

🔗 Related Issues

Fixes some issues in #15697

💥 What does this PR do?

This PR fixes several type annotation errors reported by Mypy in the following files:

  • py/selenium/webdriver/common/bidi/storage.py
  • py/selenium/webdriver/common/options.py
  • py/selenium/webdriver/common/virtual_authenticator.py

It improves static typing correctness, which will help catch bugs early and improve code readability and maintainability.

🔧 Implementation Notes

Fixed a few type hints for the above files.

💡 Additional Considerations

Will submit more small PRs to fix type annotations across the codebase.

🔄 Types of changes

  • Cleanup (type annotation fixes, no behavior changes)

PR Type

Bug fix


Description

  • Fix Mypy type annotation errors in three modules

  • Update type hints for class attributes and methods

  • Improve static typing for dictionary and Enum usage


Changes walkthrough 📝

Relevant files
Bug fix
storage.py
Add and correct type hints for Mypy compliance                     

py/selenium/webdriver/common/bidi/storage.py

  • Added explicit type hints for dictionaries and method returns
  • Ensured string conversion for certain fields in from_dict
  • Updated method signatures for better static typing
  • Improved code clarity for Mypy compatibility
  • +7/-8     
    options.py
    Specify type for mobile_options attribute                               

    py/selenium/webdriver/common/options.py

  • Changed mobile_options attribute to have explicit dict type
  • Improved type safety for attribute initialization
  • +1/-1     
    virtual_authenticator.py
    Simplify Enum member type annotations                                       

    py/selenium/webdriver/common/virtual_authenticator.py

  • Removed redundant type annotations from Enum members
  • Simplified Enum class definitions for Mypy compatibility
  • +6/-6     

    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 Jun 1, 2025

    CLA assistant check
    All committers have signed the CLA.

    @selenium-ci selenium-ci added C-py Python Bindings B-devtools Includes everything BiDi or Chrome DevTools related labels Jun 1, 2025
    Copy link
    Contributor

    qodo-merge-pro bot commented Jun 1, 2025

    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

    Potential Bug

    The from_dict method adds string conversion for 'name' and 'domain' fields but doesn't handle the case where these values could be None after conversion. This might cause issues if code elsewhere expects these fields to never be empty strings.

    name = str(data.get("name") or ""),
    value=value,
    domain = str(data.get("domain") or ""),
    Type Initialization

    The mobile_options attribute is now initialized as an empty dictionary with type annotation, but there might be code elsewhere expecting it to be None initially. Verify that this change doesn't break existing functionality.

    self.mobile_options: dict[str, str] = {}
    self._ignore_local_proxy = False

    Copy link
    Contributor

    qodo-merge-pro bot commented Jun 1, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Fix inconsistent spacing
    Suggestion Impact:The commit implemented the suggestion by removing spaces around equal signs in parameter assignments for 'name' and 'domain' parameters, and also fixed a similar spacing issue on line 18

    code diff:

    -            name = str(data.get("name") or ""),
    +            name=str(data.get("name") or ""),
                 value=value,
    -            domain = str(data.get("domain") or ""),
    +            domain=str(data.get("domain") or ""),

    Remove the unnecessary spaces around the equal signs in the parameter
    assignments. This inconsistent spacing style differs from the rest of the
    codebase and violates PEP 8 style guidelines for Python.

    py/selenium/webdriver/common/bidi/storage.py [90-92]

    -name = str(data.get("name") or ""),
    -domain = str(data.get("domain") or ""),
    +name=str(data.get("name") or ""),
    +domain=str(data.get("domain") or ""),

    [Suggestion processed]

    Suggestion importance[1-10]: 3

    __

    Why: The suggestion correctly identifies inconsistent spacing around equals signs in parameter assignments. While this is a minor style issue that doesn't affect functionality, it improves code consistency and PEP 8 compliance.

    Low
    Fix spacing in assignment
    Suggestion Impact:The commit fixed the spacing issue by removing the extra space before the equal sign in the type-annotated assignment on line 17, exactly as suggested. Additionally, similar spacing issues were fixed in other assignments.

    code diff:

    -        result: dict[str, Any]  = {
    +        result: dict[str, Any] = {

    Remove the extra space before the equal sign. There are two spaces between the
    type annotation and the equal sign, which is inconsistent with Python style
    guidelines.

    py/selenium/webdriver/common/bidi/storage.py [251]

    -result: dict[str, Any]  = {
    +result: dict[str, Any] = {

    [Suggestion processed]

    Suggestion importance[1-10]: 3

    __

    Why: The suggestion correctly identifies an extra space before the equals sign in the type-annotated assignment. This is a minor style inconsistency that should be fixed for better code formatting.

    Low

    No more code suggestions

    @cgoldberg cgoldberg removed the B-devtools Includes everything BiDi or Chrome DevTools related label Jun 2, 2025
    @cgoldberg cgoldberg changed the title Fix: Mypy type annotation errors [py] Fix: Mypy type annotation errors Jun 2, 2025
    @cgoldberg cgoldberg self-requested a review June 2, 2025 01:02
    @cgoldberg
    Copy link
    Contributor

    The linter failed on your code: https://github.com/SeleniumHQ/selenium/actions/runs/15376689600/job/43272844202?pr=15841

    You can fix the formatting automatically by running ./scripts/format.sh or tox -c py/tox.ini -e linting.

    Once you update your branch, I will give it a review. Thanks!

    @ShauryaDusht
    Copy link
    Contributor Author

    Hi, I have fixed the formatting issues. Please provide a review. Thanks!

    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.

    LGTM 👍

    Thank for the contribution!

    @cgoldberg cgoldberg merged commit 449a0b7 into SeleniumHQ:trunk Jun 2, 2025
    17 of 18 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    4 participants