The Linter class provides a type-safe wrapper around Ruff, Astral’s extremely fast Python linter and formatter written in Rust. Ruff combines the functionality of multiple tools (Flake8, Black, isort, and more) into a single, high-performance package.
from pyrig.rig.tools.linter import Linter# Check code for issuesLinter.I.check_args().run()# Check and automatically fix issuesLinter.I.check_fix_args().run()# Format codeLinter.I.format_args().run()
def format_args(self, *args: str) -> Args: """Construct ruff format arguments. Args: *args: Format command arguments. Returns: Args for 'ruff format'. """ return self.args("format", *args)# Usage - format entire projectLinter.I.format_args().run()# Format specific directoryLinter.I.format_args("src/").run()# Format specific filesLinter.I.format_args("src/main.py", "tests/test_main.py").run()# Check formatting without making changesLinter.I.format_args("--check").run()# Show diff instead of applying changesLinter.I.format_args("--diff").run()
# Check only specific rule categoriesLinter.I.check_args( "--select", "E,F,W", # pycodestyle errors, Pyflakes, warnings).run()# Ignore specific rulesLinter.I.check_args( "--ignore", "E501,W503" # line too long, line break before operator).run()# Enable specific rulesLinter.I.check_args( "--extend-select", "B,C90" # flake8-bugbear, McCabe complexity).run()
from pyrig.rig.tools.linter import Linter# Check returns non-zero if issues foundresult = Linter.I.check_args().run(check=False)if result.returncode != 0: print(f"Found {len(result.stdout.splitlines())} issues") print(result.stdout)else: print("No issues found")# Format returns non-zero if files would be reformattedresult = Linter.I.format_args("--check").run(check=False)if result.returncode != 0: print("Files need formatting") # Show which files need formatting Linter.I.format_args("--check", "--diff").run(check=False)
from pyrig.rig.tools.linter import Linterfrom pyrig.rig.tools.package_manager import PackageManager# Ensure ruff is installedPackageManager.I.add_dev_dependencies_args("ruff").run()# Run linter via uv run (ensures correct environment)PackageManager.I.run_args("ruff", "check").run()PackageManager.I.run_args("ruff", "format").run()# Or use the Linter wrapper directlyLinter.I.check_fix_args().run()Linter.I.format_args().run()
from pyrig.rig.tools.linter import Linterfrom pyrig.rig.tools.version_controller import VersionControllerdef cleanup_code(): """Clean up code before committing.""" print("Checking for uncommitted changes...") if not VersionController.I.has_unstaged_diff(): print("No changes to clean up") return print("Fixing linting issues...") result = Linter.I.check_fix_args().run(check=False) if result.returncode != 0: print("Some linting issues require manual attention") print("Formatting code...") Linter.I.format_args().run() print("Running final checks...") lint_result = Linter.I.check_args().run(check=False) format_result = Linter.I.format_args("--check").run(check=False) if lint_result.returncode == 0 and format_result.returncode == 0: print("✓ Code is clean!") # Commit the changes if VersionController.I.has_unstaged_diff(): VersionController.I.add_all_args().run() VersionController.I.commit_no_verify_args( msg="style: fix linting and formatting" ).run() else: print("✗ Some issues remain")cleanup_code()