Skip to content
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

chore(linters): Introduce ruff and fix issues #831

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

MaxymVlasov
Copy link
Collaborator

@MaxymVlasov MaxymVlasov commented Feb 27, 2025

TBD:
After @webknjaz will approve config, split to 4 PRs:

  1. Config
  2. Automatic ruff fixes
  3. Manual fixes
  4. Unrelated + disable git blame to s.2

Copy link

coderabbitai bot commented Feb 27, 2025

📝 Walkthrough

Summary by CodeRabbit

  • Chores

    • Introduced automated linting and formatting to ensure a more consistent and reliable codebase.
  • Refactor

    • Enhanced command-line error messaging and streamlined argument parsing, providing clearer feedback during execution.

Walkthrough

This pull request introduces a new pre-commit configuration for Python linting by integrating Ruff as both a linter and formatter. A new ruff.toml configuration file is added to define linting and formatting preferences. In addition, documentation improvements, better error handling, and formatting adjustments are applied in several CLI-related modules and corresponding tests. These changes maintain the core functionality while enhancing code clarity and consistency.

Changes

File(s) Change Summary
.pre-commit-config.yaml Added a new repository entry for Ruff pre-commit hooks. The configuration defines two hooks: ruff (with --fix) and ruff-format (with an exclusion for /terraform_docs_replace(_test)?\.py$/).
ruff.toml Introduced a new configuration file for Ruff with settings for Python 3.9, a 79-character line length, linting rules, formatting preferences, per-file ignores, and specific linter options.
src/pre_commit_terraform/_cli.py Updated the invoke_cli_app function by enhancing documentation (including return type and exception details) and refining error handling output with added # noqa comments.
src/pre_commit_terraform/_cli_parsing.py Reformatted the attach_subcommand_parsers_to function for improved readability and expanded the initialize_argument_parser docstring to include a detailed return type description.
src/pre_commit_terraform/_errors.py Adjusted indentation for class inheritance in PreCommitTerraformRuntimeError and added a # noqa: N818 FIXME comment to the PreCommitTerraformExit class declaration for naming conventions.
tests/pytest/_cli_test.py Modified test parameterization by removing a commented-out case, updated method signatures in CustomCmdStub with lint suppression comments, and refined exception testing using regex raw strings.

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer
    participant Git as Git
    participant PC as Pre-commit Config
    participant Ruff as Ruff Linter
    participant Formatter as Ruff Formatter

    Dev->>Git: Initiates commit
    Git->>PC: Trigger pre-commit hooks
    PC->>Ruff: Run "ruff" hook with --fix
    Ruff-->>PC: Return linting and fix results
    PC->>Formatter: Run "ruff-format" hook (skip excluded files)
    Formatter-->>PC: Return formatting results
    PC-->>Git: Proceed with commit if checks pass
Loading
sequenceDiagram
    participant User as User
    participant ArgParser as Argument Parser
    participant CLI as CLI Application
    participant ErrHnd as Error Handler

    User->>ArgParser: Provide CLI arguments
    ArgParser-->>CLI: Return parsed arguments
    CLI->>CLI: Execute invoke_cli_app
    alt Error Occurs
        CLI->>ErrHnd: Print error message (with # noqa comment)
        ErrHnd-->>CLI: Raise PreCommitTerraformExit
        CLI-->>User: Return exit code
    else Successful Execution
        CLI-->>User: Return success code
    end
Loading

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Free

📥 Commits

Reviewing files that changed from the base of the PR and between 948a36d and 8384a66.

📒 Files selected for processing (1)
  • tests/pytest/_cli_test.py (3 hunks)

Note

🎁 Summarized by CodeRabbit Free

Your organization has reached its limit of developer seats under the Pro Plan. For new users, CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please add seats to your subscription by visiting https://app.coderabbit.ai/login.If you believe this is a mistake and have available seats, please assign one to the pull request author through the subscription management page using the link above.

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Feb 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.44%. Comparing base (9017b56) to head (460031a).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #831   +/-   ##
=======================================
  Coverage   98.44%   98.44%           
=======================================
  Files          28       28           
  Lines         578      578           
  Branches       17       17           
=======================================
  Hits          569      569           
  Misses          9        9           
Flag Coverage Δ
CI-GHA 98.44% <100.00%> (ø)
MyPy 91.43% <100.00%> (ø)
OS-Linux 94.71% <100.00%> (ø)
OS-Windows 100.00% <100.00%> (ø)
OS-macOS 100.00% <ø> (ø)
Py-3.10.11 100.00% <100.00%> (ø)
Py-3.10.16 100.00% <ø> (ø)
Py-3.11.11 100.00% <ø> (ø)
Py-3.11.9 100.00% <100.00%> (ø)
Py-3.12.9 100.00% <100.00%> (ø)
Py-3.13.2 98.44% <100.00%> (ø)
Py-3.9.13 100.00% <100.00%> (ø)
Py-3.9.21 100.00% <ø> (ø)
VM-macos-13 100.00% <ø> (ø)
VM-macos-14 100.00% <ø> (ø)
VM-ubuntu-24.04 100.00% <ø> (ø)
VM-ubuntu-latest 91.43% <100.00%> (ø)
VM-windows-2025 100.00% <100.00%> (ø)
pytest 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yermulnik

This comment was marked as resolved.

@MaxymVlasov

This comment was marked as resolved.

@yermulnik

This comment was marked as resolved.

@@ -65,35 +64,37 @@ def invoke_cli_app(self, parsed_cli_args: Namespace) -> ReturnCodeType:
[CustomCmdStub()],
)

assert ReturnCode.ERROR == invoke_cli_app(['sentinel'])
Copy link
Contributor

Choose a reason for hiding this comment

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

Urgh..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

lol. Didn't know that ruff able to replace code in this way

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, some of the Ruff's linting rules also support autofixing feature. This is what happens when you pass --fix. It's fine, just keep it separate.

@webknjaz
Copy link
Contributor

4 PRs:

unrelated is a separate PR. blame ignore is a separate PR. Plus I noticed reshuffling logic which is another one. So it's like 6 PRs.

@webknjaz
Copy link
Contributor

Also, plan for adding https://wemake-python-styleguide.rtfd.io on top after all this.

from subprocess import CalledProcessError
from argparse import ArgumentParser
from argparse import Namespace
from subprocess import CalledProcessError # noqa: S404. We invoke cli tools
Copy link
Contributor

Choose a reason for hiding this comment

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

Subprocess interaction should probably be quarantined in a dedicated module where these noqas would be allowed and nowhere else. That module should implement wrappers that the rest of the project would call. This design allows keeping all potentially dangerous code in one place that you can be extra careful about.

@MaxymVlasov MaxymVlasov requested a review from webknjaz March 25, 2025 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants