Skip to content

[py] Add autoflake linter and update linting dependencies #15643

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 18 commits into from
Apr 21, 2025

Conversation

cgoldberg
Copy link
Contributor

@cgoldberg cgoldberg commented Apr 19, 2025

User description

💥 What does this PR do?

This PR adds autoflake to the Python linting environment in py/tox.ini. It uses pyflakes, which is already run by flake8, but this uses a slightly different configuration and fixes errors in place (flake8 runs more linters but doesn't auto-fix any errors).

It is enabled in the linting and linting-ci tox environments, so it will run locally as well as CI.

More info: https://pypi.org/project/autoflake/

This PR also contains:

  • updates all dependencies used for linting to latest versions
  • minor cleanup in tox.ini and pyproject.toml
  • move flake8 configuration from tox.ini to pyproject.toml (using the flake8-pyproject package)
  • fixes linting violations in conftest.py
  • adds linting for all tests
  • fixes linting violations in selenium/webdriver/support/expected_conditions.py

🔄 Types of changes

  • New feature

PR Type

Enhancement, Other


Description

  • Added autoflake to Python linting environment for unused imports/variables.

  • Configured autoflake in pyproject.toml with specific settings.

  • Updated tox.ini to include autoflake in the linting and linting-ci tasks.

  • Improved comments and formatting in tox.ini for clarity.


Changes walkthrough 📝

Relevant files
Configuration changes
pyproject.toml
Added autoflake configuration to pyproject.toml                   

py/pyproject.toml

  • Added autoflake configuration section.
  • Enabled options like ignore-pass-after-docstring and
    remove-unused-variables.
  • Set autoflake to operate recursively and in-place.
  • +9/-0     
    tox.ini
    Integrated autoflake into tox linting tasks                           

    py/tox.ini

  • Added autoflake to linting dependencies and commands.
  • Updated comments to reflect new linting tools and behavior.
  • Improved formatting and clarity of existing linting commands.
  • +16/-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.
  • @selenium-ci selenium-ci added the C-py Python Bindings label Apr 19, 2025
    Copy link
    Contributor

    qodo-merge-pro bot commented Apr 19, 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

    Copy link
    Contributor

    qodo-merge-pro bot commented Apr 19, 2025

    PR Code Suggestions ✨

    Latest suggestions up to 28ee4f0

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix flake8 configuration location

    The flake8 configuration should be in the tox.ini file or .flake8 file, not in
    pyproject.toml. The flake8-pyproject package is added as a dependency, but
    flake8 doesn't natively support configuration in pyproject.toml.

    py/pyproject.toml [159-165]

    +# Either move this configuration to tox.ini or .flake8, or ensure flake8-pyproject is properly installed and configured
     [tool.flake8]
     exclude = "selenium/webdriver/common/devtools"
     # Disable E501 once line length is better handled
     extend-ignore = ["E501", "E203"]
     # This does nothing for now as E501 is ignored
     max-line-length = 120
     min-python-version = "3.9"
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: This is a valid concern about flake8 configuration in pyproject.toml. The PR adds flake8-pyproject as a dependency in tox.ini, which is needed for flake8 to read from pyproject.toml. However, highlighting this potential issue is valuable to ensure proper configuration.

    Medium
    • Update

    Previous suggestions

    ✅ Suggestions up to commit 7f14e27
    CategorySuggestion                                                                                                                                    Impact
    General
    Add missing autoflake option
    Suggestion Impact:The suggestion was directly implemented in the commit by adding the 'remove-all-unused-imports = true' line to the autoflake configuration in pyproject.toml, exactly as suggested.

    code diff:

    +remove-all-unused-imports = true

    Consider adding the remove-all-unused-imports option to the autoflake
    configuration to ensure all unused imports are removed, which aligns with the
    PR's goal of adding autoflake for linting.

    py/pyproject.toml [149-156]

     [tool.autoflake]
     exclude = "selenium/webdriver/common/devtools"
     ignore-pass-after-docstring = true
     in-place = true
     quiet = true
     recursive = true
     remove-duplicate-keys = true
     remove-unused-variables = true
    +remove-all-unused-imports = true

    [Suggestion has been applied]

    Suggestion importance[1-10]: 6

    __

    Why: The suggestion adds the 'remove-all-unused-imports' option to autoflake configuration, which aligns with the PR's goal of enhancing linting capabilities. This is a relevant improvement that would make the autoflake tool more effective at removing unused imports.

    Low
    Fix inconsistent return type annotation

    The return type annotation is incorrect. The function returns a callable that
    produces either a WebElement or False, but the Union order is reversed from the
    standard pattern used in other similar functions in this file.

    py/selenium/webdriver/support/expected_conditions.py [197-199]

     def visibility_of_element_located(
         locator: Tuple[str, str],
    -) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
    +) -> Callable[[WebDriverOrWebElement], Union[WebElement, Literal[False]]]:
    Suggestion importance[1-10]: 5

    __

    Why: The suggestion correctly identifies an inconsistency in the return type annotation. Other similar functions in the file use Union[WebElement, Literal[False]] pattern, while this function uses the reverse order. Fixing this improves code consistency.

    Low
    ✅ Suggestions up to commit ac67486
    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix incorrect return type

    The return type annotation is incorrect. The function returns a callable that
    produces either a WebElement or False, but the current type annotation uses a
    Literal[False] which is too restrictive. It should use a more general boolean
    type.

    py/selenium/webdriver/support/expected_conditions.py [197-199]

     def visibility_of_element_located(
         locator: Tuple[str, str],
    -) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
    +) -> Callable[[WebDriverOrWebElement], Union[bool, WebElement]]:
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies a type annotation issue. Using Union[bool, WebElement] is more accurate than Union[Literal[False], WebElement] since it better represents the function's actual return behavior.

    Medium
    Fix flake8 configuration
    Suggestion Impact:The commit partially implemented the suggestion by adding quotes around the Python version value (3.9 -> "3.9"), but did not change the parameter name from hyphenated to underscore format

    code diff:

    -min-python-version = 3.9
    +min-python-version = "3.9"

    The flake8-pyproject package is added as a dependency in tox.ini, but the
    configuration in pyproject.toml might not work as expected. Flake8 traditionally
    doesn't read from pyproject.toml natively - it requires the flake8-pyproject
    plugin to be properly configured.

    py/pyproject.toml [158-164]

     [tool.flake8]
     exclude = "selenium/webdriver/common/devtools"
     # Disable E501 once line length is better handled
     extend-ignore = ["E501", "E203"]
     # This does nothing for now as E501 is ignored
     max-line-length = 120
    -min-python-version = 3.9
    +min_python_version = "3.9"

    [Suggestion has been applied]

    Suggestion importance[1-10]: 5

    __

    Why: The suggestion correctly identifies a configuration issue with flake8 in pyproject.toml. The parameter should be min_python_version with an underscore instead of min-python-version with a hyphen, and the value should be a string "3.9" rather than a number.

    Low
    ✅ Suggestions up to commit 0134992
    CategorySuggestion                                                                                                                                    Impact
    General
    Remove only_modified isort option
    Suggestion Impact:The suggestion to remove the 'only_modified = true' option was implemented. The line was removed as suggested, though additional changes were also made by adding 'quiet = true' and 'skip = "selenium/webdriver/common/devtools"' options.

    code diff:

    -only_modified = true
    +quiet = true
    +skip = "selenium/webdriver/common/devtools"

    The only_modified option in isort configuration will limit isort to only process
    modified files. This could lead to inconsistent formatting across the codebase.
    For a linting tool in a CI environment, it's better to ensure all files follow
    the same standard.

    py/pyproject.toml [138-142]

     [tool.isort]
     # isort is a common python tool for keeping imports nicely formatted.
     # Automatically keep imports alphabetically sorted, on single lines in
     # PEP recommended sections (https://peps.python.org/pep-0008/#imports)
     # files or individual lines can be ignored via `# isort:skip|# isort:skip_file`.
     force_single_line = true
     profile = "black"
     py_version = 39
    -only_modified = true
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion correctly identifies a potential issue with the added "only_modified" option, which could lead to inconsistent formatting across the codebase. Removing this option would ensure consistent formatting standards are applied to all files.

    Medium
    Maintain original package versions

    The PR is adding autoflake as a linter but also changing package versions in
    tox.ini. This contradicts the PR title which only mentions adding autoflake.
    Consider keeping the original package versions to maintain the focus of this PR.

    py/tox.ini [6-8]

     deps =
    -  validate-pyproject==0.24.1
    -  packaging==25.0
    +  validate-pyproject==0.23
    +  packaging==24.2
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies that changing package versions is outside the scope of a PR focused on adding autoflake. Keeping the original versions would maintain the PR's focus and reduce potential issues from unrelated changes.

    Medium

    @cgoldberg cgoldberg changed the title [py] Add autoflake linter [py] Add autoflake linter and update linting dependencies Apr 21, 2025
    @cgoldberg cgoldberg merged commit aa2bdb9 into SeleniumHQ:trunk Apr 21, 2025
    17 checks passed
    @cgoldberg cgoldberg deleted the py-autoflake-formatting branch April 21, 2025 17:23
    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.

    2 participants