From c8d537c7ab9edf7412f81c65e1fb6291f503559c Mon Sep 17 00:00:00 2001 From: ascender1729 Date: Sun, 30 Mar 2025 10:28:46 +0530 Subject: [PATCH] Add Python best practices with flake8 and pyupgrade --- .flake8 | 22 ++++++++++++++ pyupgrade.ini | 4 +++ requirements.txt | 6 ++++ scripts/check_code_quality.py | 55 +++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 .flake8 create mode 100644 pyupgrade.ini create mode 100755 scripts/check_code_quality.py diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..4d18c26e --- /dev/null +++ b/.flake8 @@ -0,0 +1,22 @@ +[flake8] +max-line-length = 88 +extend-ignore = E203, W503 +exclude = + .git, + __pycache__, + build, + dist, + *.egg-info, + .eggs, + .tox, + .venv, + venv, + .env, + .pytest_cache, + .coverage, + htmlcov, + .mypy_cache, + .ruff_cache +per-file-ignores = + __init__.py: F401 +max-complexity = 10 \ No newline at end of file diff --git a/pyupgrade.ini b/pyupgrade.ini new file mode 100644 index 00000000..3f064eff --- /dev/null +++ b/pyupgrade.ini @@ -0,0 +1,4 @@ +[pyupgrade] +py3-plus = True +py39-plus = True +keep-runtime-typing = True \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index b8680cf3..e2602db1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,3 +23,9 @@ networkx aiofiles httpx vllm>=0.2.0 +flake8>=6.1.0 +flake8-bugbear>=23.3.12 +flake8-comprehensions>=3.12.0 +flake8-simplify>=0.19.3 +flake8-unused-arguments>=0.0.4 +pyupgrade>=3.15.0 diff --git a/scripts/check_code_quality.py b/scripts/check_code_quality.py new file mode 100755 index 00000000..fdfc3ccc --- /dev/null +++ b/scripts/check_code_quality.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +import subprocess +import sys +from pathlib import Path + +def run_command(command: list[str], cwd: Path) -> bool: + """Run a command and return True if successful.""" + try: + result = subprocess.run( + command, + cwd=cwd, + capture_output=True, + text=True, + check=True + ) + return True + except subprocess.CalledProcessError as e: + print(f"Error running {' '.join(command)}:") + print(e.stdout) + print(e.stderr, file=sys.stderr) + return False + +def main(): + """Run all code quality checks.""" + root_dir = Path(__file__).parent.parent + success = True + + # Run flake8 + print("\nRunning flake8...") + if not run_command(["flake8", "swarms", "tests"], root_dir): + success = False + + # Run pyupgrade + print("\nRunning pyupgrade...") + if not run_command(["pyupgrade", "--py39-plus", "swarms", "tests"], root_dir): + success = False + + # Run black + print("\nRunning black...") + if not run_command(["black", "--check", "swarms", "tests"], root_dir): + success = False + + # Run ruff + print("\nRunning ruff...") + if not run_command(["ruff", "check", "swarms", "tests"], root_dir): + success = False + + if not success: + print("\nCode quality checks failed. Please fix the issues and try again.") + sys.exit(1) + else: + print("\nAll code quality checks passed!") + +if __name__ == "__main__": + main() \ No newline at end of file