Skip to content

feat: get rid of toml and packaging and improve performance #89

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 6 commits into from
Jul 6, 2025

Conversation

shenxianpeng
Copy link
Collaborator

@shenxianpeng shenxianpeng commented Jul 6, 2025

Summary by CodeRabbit

  • Documentation
    • Added descriptive docstrings to key functions for improved clarity and maintainability.
  • Chores
    • Updated dependency management to leverage the standard library for configuration parsing on Python 3.11+, reducing external package requirements and enhancing compatibility.
    • Simplified version resolution logic for tool dependencies, improving reliability of version matching.
    • Adjusted test suite to reflect updated version resolution behavior.
    • Modified test success criteria to align with updated failure count expectations.

@shenxianpeng shenxianpeng added the enhancement New feature or request label Jul 6, 2025
Copy link

codecov bot commented Jul 6, 2025

Codecov Report

Attention: Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.

Project coverage is 95.48%. Comparing base (90a453d) to head (0a4c0ba).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
cpp_linter_hooks/util.py 84.61% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #89      +/-   ##
==========================================
- Coverage   97.01%   95.48%   -1.53%     
==========================================
  Files           3        3              
  Lines         134      133       -1     
==========================================
- Hits          130      127       -3     
- Misses          4        6       +2     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

coderabbitai bot commented Jul 6, 2025

"""

Walkthrough

The cpp_linter_hooks/util.py module was updated to replace the external toml library with the standard library tomllib for parsing pyproject.toml. Version resolution logic was simplified by removing complex version parsing and matching. Default version fallback behavior was adjusted to allow None. Docstrings were added to several key functions. The pyproject.toml dependencies were modified to conditionally require tomli only for Python versions below 3.11. The test suite and test scripts were updated to reflect these changes, including patching tomllib.load and adjusting expected version resolution results, as well as modifying a test failure count check in the test runner script.

Changes

File Change Summary
cpp_linter_hooks/util.py Replaced toml with tomllib (falling back to tomli); simplified version resolution logic; adjusted default version assignment to allow None; added docstrings to main functions
pyproject.toml Removed unconditional toml and packaging dependencies; added conditional tomli dependency for Python < 3.11
tests/test_util.py Updated tests to patch tomllib.load instead of toml.load; changed expected version resolution to first matching version instead of latest
testing/run.sh Changed test failure count check from 9 to 10 failures for success condition

Sequence Diagram(s)

No sequence diagram generated since the changes primarily involve dependency updates, simplification of internal logic, test adjustments, and documentation additions without altering control flow.
"""

Warning

Review ran into problems

🔥 Problems

GitHub checks timed out after 90 seconds. Some checks were still in progress when the timeout was reached. Consider increasing the GitHub Checks - Timeout Ms value in your CodeRabbit configuration to allow more time for checks to complete.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between db90443 and 0a4c0ba.

📒 Files selected for processing (2)
  • testing/run.sh (1 hunks)
  • tests/test_util.py (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Run benchmarks
🔇 Additional comments (3)
testing/run.sh (1)

32-32: Verify the updated expected failure count

The check in testing/run.sh was updated from expecting 9 failures to 10:

  • File: testing/run.sh
  • Line 32: changed from
  • if [ $failed_cases -eq 9 ]; then
  • if [ $failed_cases -eq 10 ]; then

Please confirm that this new failure count is intentional given the changes to your version‐resolution logic. If it is, update any related documentation (e.g. README.md) to reflect the new expected number of failures.

tests/test_util.py (2)

115-115: LGTM! Correctly updated TOML library mocks.

The patches have been correctly updated from toml.load to tomllib.load, aligning with the PR objective of removing the external toml dependency in favor of the standard library tomllib.

Also applies to: 139-139, 152-152


164-168: Validate first-match version resolution

The _resolve_version helper uses next(…) over the sorted version list, so it returns the first matching entry (e.g. for "18" it yields the earliest "18.x"). The tests in tests/test_util.py (around lines 174–180 for clang-format and 191–197 for clang-tidy) assert exactly this “first available” behavior:

  • util.py: lines 119–124
  • tests/test_util.py:
    • test_resolve_version_clang_format (≈174–180)
    • test_resolve_version_clang_tidy (≈191–197)

In contrast, typical Python dependency resolution picks the latest patch release within the minor version. Please confirm whether the current “first-match” strategy is intentional. If you’d prefer to align with “latest patch” semantics, we should update _resolve_version to select the last matching element and adjust these test expectations accordingly.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
cpp_linter_hooks/util.py (1)

149-158: Return type is inconsistent with surrounding helpers

_install_tool’s body returns shutil.which(tool) (a str or None), whereas callers expect a Path.
Either cast with Path() here or update callers + type hints. Updating here keeps the public surface coherent:

-        return shutil.which(tool)
+        found = shutil.which(tool)
+        return Path(found) if found else None
🧹 Nitpick comments (5)
cpp_linter_hooks/util.py (5)

13-23: Docstring omits parameter / return details

The one-liner is helpful but doesn’t specify that the function returns an Optional[str] and what None means. Consider expanding to a short Google/NumPy style block with Args / Returns so callers know the semantics.


111-133: Clarify “partial-version” behaviour in docstring

_resolve_version supports major‐only and major.minor inputs. Stating that explicitly will spare readers a deep dive into the code.


135-147: Mention clang-tidy special-case in docstring

Because the second line of clang-tidy --version is parsed, the docstring should indicate that it expects LLVM-style multi-line output and may return None if parsing fails.


161-173: Docstring could highlight side-effect (installation)

Explicitly noting that the function may perform a network install via pip will help downstream consumers decide whether they can call this in restricted environments.


198-206: Docstring hides fallback behaviour

ensure_installed logs a warning but still returns the tool name when installation fails. That subtlety is worth a sentence in the docstring so users don’t assume success.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 90a453d and 89c1672.

📒 Files selected for processing (1)
  • cpp_linter_hooks/util.py (6 hunks)

@shenxianpeng shenxianpeng changed the title add docstring to util feat: get rid of toml and packaging packges Jul 6, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (2)
cpp_linter_hooks/util.py (2)

138-146: Installation path builds an invalid requirement when version is None

When _resolve_install() falls back to None, the current code calls:

subprocess.check_call([... f"{tool}=={version}"])

which becomes e.g. clang-format==None – an invalid specifier that fails pip resolution.

-def _install_tool(tool: str, version: str) -> Optional[Path]:
+def _install_tool(tool: str, version: Optional[str]) -> Optional[Path]:
@@
-        subprocess.check_call(
-            [sys.executable, "-m", "pip", "install", f"{tool}=={version}"]
-        )
+        requirement = f"{tool}=={version}" if version else tool
+        subprocess.check_call([sys.executable, "-m", "pip", "install", requirement])

Also update the signature’s type hint in the call sites.


164-174: Version-mismatch logic fires when user_version is None

if runtime_version and user_version not in runtime_version:

If user_version is None, the condition always evaluates True, forcing an unnecessary reinstall (and crashing later as described above).

-        if runtime_version and user_version not in runtime_version:
+        if runtime_version and user_version and user_version not in runtime_version:
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89c1672 and 2434cab.

📒 Files selected for processing (2)
  • cpp_linter_hooks/util.py (5 hunks)
  • pyproject.toml (1 hunks)
🧰 Additional context used
🪛 Pylint (3.3.7)
cpp_linter_hooks/util.py

[refactor] 124-124: Either all return statements in a function should return an expression, or none of them should.

(R1710)

🪛 GitHub Actions: CodSpeed
cpp_linter_hooks/util.py

[error] 17-17: AttributeError: 'PosixPath' object has no attribute 'read' in function get_version_from_dependency when calling tomllib.load. The code incorrectly passes a PosixPath object instead of a file object.

🔇 Additional comments (2)
cpp_linter_hooks/util.py (1)

4-4: Import compatibility for < 3.11

A back-port named tomli, not tomllib, is what the majority of projects depend on.
Double-check that the PyPI package tomllib you pin really mirrors the std-lib API and won’t clash with 3.11+ environments where tomllib is builtin.

pyproject.toml (1)

35-38: Verify the back-port choice

You now depend on

"tomllib; python_version < '3.11'",

Unlike the widespread tomli, the tomllib back-port is less battle-tested. Please ensure:

  1. It provides the same API surface as the std-lib module.
  2. It does not shadow/override the builtin on 3.11+.

Consider switching to tomli plus the canonical shim:

try:
    import tomllib  # py3.11+
except ModuleNotFoundError:  # <3.11
    import tomli as tomllib

Copy link

sonarqubecloud bot commented Jul 6, 2025

Copy link

codspeed-hq bot commented Jul 6, 2025

CodSpeed Performance Report

Merging #89 will degrade performances by 59.78%

Comparing patch-1 (0a4c0ba) with main (90a453d)

Summary

⚡ 39 improvements
❌ 5 regressions
✅ 18 untouched benchmarks
🆕 6 new benchmarks
⁉️ 6 dropped benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
test_run_clang_format_dry_run[args0-1] 2,382.5 µs 824.5 µs ×2.9
test_run_clang_format_invalid[args0-1] 2,391.7 µs 839.7 µs ×2.8
test_run_clang_format_invalid[args1-1] 2,630 µs 942 µs ×2.8
test_run_clang_format_invalid[args2-1] 2,646.5 µs 941.4 µs ×2.8
test_run_clang_format_invalid[args3-1] 2,563.6 µs 944 µs ×2.7
test_run_clang_format_invalid[args4-1] 2,556.9 µs 853.5 µs ×3
test_run_clang_format_invalid[args5-1] 2,542.7 µs 859.5 µs ×3
test_run_clang_format_valid[args0-expected_retval0] 2.6 ms 1 ms ×2.6
test_run_clang_format_valid[args1-expected_retval1] 2.8 ms 1.1 ms ×2.6
test_run_clang_format_valid[args2-expected_retval2] 2.8 ms 1.1 ms ×2.6
test_run_clang_format_valid[args3-expected_retval3] 2.7 ms 1.1 ms ×2.5
test_run_clang_format_valid[args4-expected_retval4] 2,678.3 µs 992.5 µs ×2.7
test_run_clang_format_valid[args5-expected_retval5] 2,672.9 µs 995.5 µs ×2.7
test_run_clang_format_verbose 2.6 ms 1 ms ×2.5
test_run_clang_format_verbose_error 2,478.5 µs 935.5 µs ×2.6
test_run_clang_tidy_invalid[args0-1] 1,044.4 µs 726.7 µs +43.72%
test_run_clang_tidy_invalid[args1-1] 1,089.8 µs 750.2 µs +45.26%
test_run_clang_tidy_invalid[args2-1] 1,084.3 µs 749.6 µs +44.65%
test_run_clang_tidy_invalid[args3-1] 1,084 µs 754.5 µs +43.67%
test_run_clang_tidy_invalid[args4-1] 1,085.4 µs 749.5 µs +44.81%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

@shenxianpeng shenxianpeng changed the title feat: get rid of toml and packaging packges feat: get rid of toml and packaging and improve performance Jul 6, 2025
@shenxianpeng shenxianpeng merged commit ddd7de8 into main Jul 6, 2025
14 of 17 checks passed
@shenxianpeng shenxianpeng deleted the patch-1 branch July 6, 2025 17:41
shenxianpeng added a commit that referenced this pull request Jul 6, 2025
shenxianpeng added a commit that referenced this pull request Jul 6, 2025
* fixup #89 to get latest version

* fix: update run.sh failure to 9

* Update cpp_linter_hooks/util.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant