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

Simplify the code formatting script #8910

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

SpecLad
Copy link
Contributor

@SpecLad SpecLad commented Jan 8, 2025

Motivation and context

After #8611 and #8866, it's no longer necessary to apply the formatters to each module separately. So don't!

In addition, update the black exclusion list in pyproject.toml to only contain files that were not listed in format_python_code.sh. This way, formatting will automatically be enforced for all new files.

For a few of the files reformatting only creates a small diff, so don't even bother excluding them - reformat them instead.

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • [ ] I have created a changelog fragment
  • [ ] I have updated the documentation accordingly
  • [ ] I have added tests to cover my changes
  • [ ] I have linked related issues (see GitHub docs)
  • [ ] I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • Chores

    • Updated Python code formatting script to simplify formatting process
    • Enhanced configuration for isort and black tools
    • Updated documentation for code formatting guidelines
  • Style

    • Refactored code formatting in multiple test generator files to improve readability
    • Standardized string quote usage in management command files

After cvat-ai#8611 and cvat-ai#8866, it's no longer necessary to apply the formatters to
each module separately. So don't!

In addition, update the black exclusion list in `pyproject.toml` to only contain
files that were _not_ listed in `format_python_code.sh`. This way, formatting
will automatically be enforced for all new files.

For a few of the files reformatting only creates a small diff, so don't even
bother excluding them.
Copy link
Contributor

coderabbitai bot commented Jan 8, 2025

Walkthrough

This pull request involves a series of code formatting and standardization changes across multiple Python files in the CVAT project. The modifications primarily focus on improving code readability by restructuring conditional expressions, updating string quotation styles, and enhancing the Python code formatting script. The changes are mostly cosmetic, targeting the consistency and clarity of the codebase without altering the underlying functionality of the code.

Changes

File Change Summary
cvat/apps/engine/management/commands/syncperiodicjobs.py Converted string literals from single to double quotes
cvat/apps/engine/rules/tests/generators/*_test.gen.rego.py Reformatted organization dictionary assignment in get_data functions from single-line to multi-line conditional expressions
dev/format_python_code.sh Simplified code formatting approach by using current directory and resolving all configs
pyproject.toml Extended black tool's exclusion patterns for more granular file formatting control
site/content/en/docs/contributing/coding-style.md Updated instructions for Python code formatting

Poem

🐰 Formatting code with rabbit's delight,
Double quotes dancing, lines shining bright
Readability leaps, clarity springs
In CVAT's codebase, precision sings!
A cleaner script, a happier dev 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

sonarqubecloud bot commented Jan 8, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
70.4% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Copy link
Contributor

@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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b594b1c and 173c53b.

📒 Files selected for processing (13)
  • cvat/apps/engine/management/commands/syncperiodicjobs.py (4 hunks)
  • cvat/apps/engine/rules/tests/generators/annotationguides_test.gen.rego.py (2 hunks)
  • cvat/apps/engine/rules/tests/generators/cloudstorages_test.gen.rego.py (1 hunks)
  • cvat/apps/engine/rules/tests/generators/comments_test.gen.rego.py (1 hunks)
  • cvat/apps/engine/rules/tests/generators/issues_test.gen.rego.py (1 hunks)
  • cvat/apps/engine/rules/tests/generators/jobs_test.gen.rego.py (1 hunks)
  • cvat/apps/engine/rules/tests/generators/projects_test.gen.rego.py (1 hunks)
  • cvat/apps/engine/rules/tests/generators/server_test.gen.rego.py (1 hunks)
  • cvat/apps/engine/rules/tests/generators/tasks_test.gen.rego.py (1 hunks)
  • cvat/apps/engine/rules/tests/generators/users_test.gen.rego.py (2 hunks)
  • dev/format_python_code.sh (1 hunks)
  • pyproject.toml (1 hunks)
  • site/content/en/docs/contributing/coding-style.md (1 hunks)
✅ Files skipped from review due to trivial changes (7)
  • cvat/apps/engine/rules/tests/generators/jobs_test.gen.rego.py
  • cvat/apps/engine/rules/tests/generators/cloudstorages_test.gen.rego.py
  • cvat/apps/engine/rules/tests/generators/comments_test.gen.rego.py
  • cvat/apps/engine/rules/tests/generators/tasks_test.gen.rego.py
  • cvat/apps/engine/rules/tests/generators/server_test.gen.rego.py
  • cvat/apps/engine/management/commands/syncperiodicjobs.py
  • cvat/apps/engine/rules/tests/generators/annotationguides_test.gen.rego.py
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build
  • GitHub Check: testing
  • GitHub Check: generate_github_pages
  • GitHub Check: Analyze (python)
  • GitHub Check: Linter
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (7)
cvat/apps/engine/rules/tests/generators/users_test.gen.rego.py (2)

Line range hint 56-74: LGTM! Well-structured rule evaluation logic.

The implementation is clean and efficient, using filter operations to progressively narrow down the applicable rules.


85-93: LGTM! Improved readability with multi-line formatting.

The conditional expression is now more readable while maintaining the same logic.

cvat/apps/engine/rules/tests/generators/projects_test.gen.rego.py (1)

115-123: LGTM! Consistent formatting improvement.

The conditional expression formatting matches the style used in other files, improving readability.

cvat/apps/engine/rules/tests/generators/issues_test.gen.rego.py (1)

129-137: LGTM! Consistent formatting improvement.

The conditional expression formatting matches the style used in other files, improving readability.

dev/format_python_code.sh (1)

18-19: LGTM! Simplified approach to code formatting.

The change to run formatters on the current directory (.) is a good simplification that ensures all files are covered without maintaining a list of paths. This aligns well with the PR objectives.

Let's verify the impact of this change:

✅ Verification successful

Simplified formatting approach is safe to use

The change to use . is safe because:

  • Black's exclusion patterns in pyproject.toml are path-based and will continue to work correctly
  • The exclusions use absolute paths from the repository root ('^/'), ensuring consistent behavior
  • The excluded paths are marked as temporary ("TODO: get rid of these"), so this change aligns with the project's direction
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check which Python files would be affected by the new formatting approach
# that weren't covered before

# First, list all Python files
echo "Python files in the repository:"
fd '\.py$'

# Then, check which ones are excluded by black
echo -e "\nFiles excluded by black:"
${PYTHON:-python3} -m black --extend-exclude "$(grep -A 100 '^extend-exclude = """' pyproject.toml | grep -B 100 '^"""' | tail -n +2)" --verbose . 2>&1 | grep "excluded"

Length of output: 23996


Script:

#!/bin/bash

# First, let's get the black configuration and see what files would be formatted
echo "Files that would be formatted by black:"
python3 -m black --verbose --check . 2>&1 | grep "would reformat" || true

echo -e "\nFiles that would be skipped by black:"
python3 -m black --verbose --check . 2>&1 | grep "excluded" || true

# Also get the content of pyproject.toml to verify the exclusion patterns
echo -e "\nBlack configuration from pyproject.toml:"
cat pyproject.toml | grep -A 50 "\[tool.black\]" | grep -B 50 "^$" || true

Length of output: 484


Script:

#!/bin/bash

# Find pyproject.toml first
echo "Looking for pyproject.toml:"
fd pyproject.toml

# Show full black output
echo -e "\nFull black check output:"
python3 -m black --check --verbose . 2>&1 || true

# If pyproject.toml exists, show its content
echo -e "\nContent of pyproject.toml (if found):"
fd pyproject.toml -x cat {}

Length of output: 3346

site/content/en/docs/contributing/coding-style.md (1)

14-14: LGTM! Clear and concise documentation update.

The documentation change effectively communicates the new simplified approach to code formatting.

pyproject.toml (1)

Line range hint 16-16: Verify Python version compatibility.

The black configuration targets Python 3.9. Please verify this aligns with the project's minimum supported Python version.

Let's check the project's Python version requirements:

✅ Verification successful

Black's Python 3.9 target version is compatible with project requirements

The project requires Python >= 3.9 as specified in cvat-cli's setup.py, which aligns perfectly with Black's Python 3.9 target version.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check Python version requirements in the project
echo "Setup.py Python version requirement:"
fd 'setup\.py$' -x grep -l "python_requires" {} \; -x cat {}

echo -e "\nDocumented Python version requirement:"
fd -e md -e rst -x grep -l "python" {} \; -x grep -l "version" {} \; -x cat {}

Length of output: 68486

pyproject.toml Show resolved Hide resolved
@SpecLad SpecLad requested a review from zhiltsov-max January 8, 2025 15:05
Comment on lines +18 to +19
${BLACK} .
${ISORT} --resolve-all-configs .
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a little bit worried that these commands now don't have any reference to CVAT specifically, so starting them from a directory outside of CVAT repo may have unexpected consequences. This doesn't look like a big problem, but maybe add a check that the current directory is a path inside a CVAT repo?

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.

2 participants