Skip to content

[py] Raise TypeError when creating webdriver.Remote() without options #15619

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 13 commits into from
Apr 19, 2025

Conversation

cgoldberg
Copy link
Contributor

@cgoldberg cgoldberg commented Apr 12, 2025

User description

🔗 Related Issues

Fixes: #15618

💥 What does this PR do?

This PR changes the WebDriver class in py/selenium/webdriver/remote/webdriver.py so that it raises an error if you initialize it with without the options keywoard argument.

All driver classes subclass WebDriver. You can instantiate all of them without supplying the options arg, except for webdriver.Remote.

The following 2 code examples:

from selenium import webdriver
driver = webdriver.Remote()
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
driver = webdriver.Remote(options)

... both result in the error:

AttributeError: 'NoneType' object has no attribute 'to_capabilities'

After this change, both of those code examples will result in a better error:

TypeError: missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)

Relevant tests for this were added.

This shouldn't affect any other driver classes.

🔄 Types of changes

  • Bug fix (backwards compatible)

PR Type

Bug fix, Tests


Description

  • Raise TypeError when webdriver.Remote() is called without options.

  • Add test cases to validate the new exception behavior.

  • Improve error messaging for missing options argument in webdriver.Remote.


Changes walkthrough 📝

Relevant files
Bug fix
webdriver.py
Add `TypeError` for missing `options` in `webdriver.Remote()`

py/selenium/webdriver/remote/webdriver.py

  • Added a check for None in options argument.
  • Raised TypeError with a descriptive error message when options is
    missing.
  • +5/-1     
    Tests
    remote_tests.py
    Add tests for missing `options` in `webdriver.Remote()`   

    py/test/selenium/webdriver/remote/remote_tests.py

  • Added test cases to ensure TypeError is raised when options is
    missing.
  • Verified error message matches expected output.
  • +32/-0   

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

    qodo-merge-pro bot commented Apr 12, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit 4c7622f)

    Here are some key observations to aid the review process:

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

    Condition Order

    The new code adds a check for None, but then has an elif for list type. Consider whether the order of these conditions is optimal, as a None value would never reach the list check.

    if options is None:
        raise TypeError(
            "missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)"
        )
    elif isinstance(options, list):

    @selenium-ci selenium-ci added C-py Python Bindings and removed Review effort 1/5 labels Apr 12, 2025
    Copy link
    Contributor

    qodo-merge-pro bot commented Apr 12, 2025

    PR Code Suggestions ✨

    Latest suggestions up to 4932e43

    CategorySuggestion                                                                                                                                    Impact
    General
    Improve error message accuracy

    The error message is misleading since options is not actually a keyword-only
    argument in the function signature. Consider rephrasing the error message to be
    more accurate about the parameter requirement.

    py/selenium/webdriver/remote/webdriver.py [227-230]

     if options is None:
         raise TypeError(
    -        "missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)"
    +        "The 'options' argument is required and cannot be None (must be an instance of driver `options.Options` class)"
         )
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies that the error message is technically inaccurate since 'options' is not defined as a keyword-only argument in the function signature. The improved error message is clearer and more accurately describes the actual requirement without using incorrect terminology.

    Medium
    • More

    Previous suggestions

    Suggestions up to commit 925ce2d
    CategorySuggestion                                                                                                                                    Impact
    General
    Fix misleading error message

    The error message is misleading since options is not a keyword-only argument in
    the function signature. Consider updating the error message to accurately
    reflect that options is a required argument but not keyword-only.

    py/selenium/webdriver/remote/webdriver.py [227-230]

     if options is None:
         raise TypeError(
    -        "missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)"
    +        "missing required argument: 'options' (instance of driver `options.Options` class)"
         )
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies that the error message is technically inaccurate since 'options' is not defined as a keyword-only argument in the function signature. Fixing this improves the clarity and accuracy of the error message, which is important for developers using the library.

    Medium
    ✅ Suggestions up to commit 859dd30
    CategorySuggestion                                                                                                                                    Impact
    General
    Fix typo in function name
    Suggestion Impact:The commit directly implemented the suggestion by fixing the typo in the test function name, changing 'test_remote_webdriver_requires_options_paraeter' to 'test_remote_webdriver_requires_options_parameter'

    code diff:

    -def test_remote_webdriver_requires_options_paraeter():
    +def test_remote_webdriver_requires_options_parameter():

    Fix the typo in the test function name. "paraeter" should be "parameter".

    py/test/selenium/webdriver/remote/remote_tests.py [25]

    -def test_remote_webdriver_requires_options_paraeter():
    +def test_remote_webdriver_requires_options_parameter():

    [Suggestion has been applied]

    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies a typo in the test function name where "paraeter" should be "parameter". This is a valid improvement for code readability and maintainability, ensuring consistent and correct naming conventions.

    Medium
    ✅ Suggestions up to commit 4c7622f
    CategorySuggestion                                                                                                                                    Impact
    General
    Improve error message accuracy

    The error message is misleading since options is not actually a keyword-only
    argument in the function signature. Consider updating the error message to be
    more accurate about the parameter requirement.

    py/selenium/webdriver/remote/webdriver.py [227-230]

     if options is None:
         raise TypeError(
    -        "missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)"
    +        "The 'options' argument cannot be None. It must be an instance of a driver `options.Options` class"
         )
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion correctly identifies that the error message is technically inaccurate since 'options' is not defined as a keyword-only argument in the function signature. The improved error message is clearer and more accurate about the actual requirement, which would prevent confusion for users.

    Medium
    General
    Rename test function
    Suggestion Impact:The function was renamed as suggested, but with a typo: 'paraeter' instead of 'parameter'

    code diff:

    -def test_remote_webdriver_requires_options_kwarg():
    +def test_remote_webdriver_requires_options_paraeter():

    The test function name test_remote_webdriver_requires_options_kwarg() implies
    that options is a keyword-only argument, but in the implementation it's a
    regular parameter. Consider renaming the test function to better reflect what's
    being tested.

    py/test/selenium/webdriver/remote/remote_tests.py [25-32]

    -def test_remote_webdriver_requires_options_kwarg():
    +def test_remote_webdriver_requires_options_parameter():
         msg = "missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)"
         with pytest.raises(TypeError, match=re.escape(msg)):
             webdriver.Remote()
         with pytest.raises(TypeError, match=re.escape(msg)):
             webdriver.Remote(None)
         with pytest.raises(TypeError, match=re.escape(msg)):
             webdriver.Remote(options=None)

    [Suggestion has been applied]

    Suggestion importance[1-10]: 5

    __

    Why: The suggestion correctly identifies a mismatch between the test function name and the error message. The error message refers to 'options' as a "keyword-only argument" while the function name implies it's a keyword argument, but the implementation treats it as a regular parameter. Renaming the function improves clarity and consistency.

    Low
    Suggestions up to commit 49b52fe
    CategorySuggestion                                                                                                                                    Impact
    General
    Improve error message accuracy

    The error message is misleading since options is not actually a keyword-only
    argument in the function signature. Consider updating the error message to be
    more accurate about the parameter requirement.

    py/selenium/webdriver/remote/webdriver.py [227-230]

     if options is None:
         raise TypeError(
    -        "missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)"
    +        "The 'options' argument is required and cannot be None (must be an instance of driver `options.Options` class)"
         )
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies that the error message is technically inaccurate since 'options' is not defined as a keyword-only argument in the function signature. The improved error message is clearer and more accurately describes the actual requirement without the misleading terminology.

    Medium
    General
    Add positive test case

    The test should include a positive case to verify that the Remote class works
    correctly when valid options are provided. This ensures the error is only raised
    for missing/None options.

    py/test/selenium/webdriver/remote/remote_tests.py [27-32]

     with pytest.raises(TypeError, match=re.escape(msg)):
         webdriver.Remote()
     with pytest.raises(TypeError, match=re.escape(msg)):
         webdriver.Remote(None)
     with pytest.raises(TypeError, match=re.escape(msg)):
         webdriver.Remote(options=None)
     
    +# Positive test case with mock options
    +mock_options = type('MockOptions', (), {'to_capabilities': lambda self: {}})()
    +remote = webdriver.Remote(options=mock_options)
    +assert remote is not None
    +
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion adds a valuable positive test case to complement the existing negative tests. This improves test coverage by verifying that the Remote class works correctly with valid options, not just that it fails appropriately with invalid ones. This is good testing practice to ensure complete functionality.

    Medium
    Possible issue
    Validate options type

    The error handling only checks for None but doesn't validate that the provided
    options is actually an instance of an Options class. Add validation to ensure
    the options parameter is of the correct type.

    py/selenium/webdriver/remote/webdriver.py [227-230]

     if options is None:
         raise TypeError(
             "missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)"
         )
    +elif not hasattr(options, 'to_capabilities') and not isinstance(options, list):
    +    raise TypeError("'options' must be an instance of driver `options.Options` class or a list")
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion adds important type validation to ensure the options parameter is either a list or has a to_capabilities method. This prevents potential runtime errors when accessing to_capabilities on invalid option types, improving robustness and providing clearer error messages.

    Medium

    @cgoldberg cgoldberg marked this pull request as draft April 13, 2025 16:27
    @cgoldberg
    Copy link
    Contributor Author

    Converted this to a draft... it still needs some work.

    @titusfortner
    Copy link
    Member

    curious, what isn't working with this?

    @cgoldberg
    Copy link
    Contributor Author

    I just looked again and I think it's alright.

    I second guessed myself because of that AI code review bot :)

    The error message should be more accurate since options is not a keyword-only
    argument in the function signature. It's a regular positional/keyword argument.

    But it actually is a keyword-only argument.

    I'll test this out one more time then merge it.

    @cgoldberg cgoldberg changed the title [py] raise exception when creating webdriver.Remote() without options [py] Raise exception when creating webdriver.Remote() without options Apr 17, 2025
    @cgoldberg cgoldberg force-pushed the py-remote-options-kwarg branch from ddba36f to d7741b6 Compare April 18, 2025 01:58
    @cgoldberg cgoldberg marked this pull request as ready for review April 18, 2025 21:35
    @cgoldberg cgoldberg changed the title [py] Raise exception when creating webdriver.Remote() without options [py] Raise TypeError when creating webdriver.Remote() without options Apr 19, 2025
    @cgoldberg cgoldberg merged commit 0686d8f into SeleniumHQ:trunk Apr 19, 2025
    17 checks passed
    @cgoldberg cgoldberg deleted the py-remote-options-kwarg branch April 19, 2025 03:39
    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.

    [🐛 Bug]: [py] confusing error when creating webdriver.Remote() without options
    3 participants