From 0a7775860c1fe10ecc21a4037d0fef0a5123481d Mon Sep 17 00:00:00 2001 From: Patrick Decat Date: Fri, 1 Sep 2023 00:29:59 +0200 Subject: [PATCH] feat(dev): use ruff for python linting (#355) --- .github/workflows/pr-python-checks.yml | 9 ++++++-- backend/.pre-commit-config.yaml | 30 +++++++++++++++----------- backend/pyproject.toml | 14 ++++++++++++ backend/requirements/dev.txt | 1 + backend/setup.cfg | 8 ------- 5 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 backend/pyproject.toml delete mode 100644 backend/setup.cfg diff --git a/.github/workflows/pr-python-checks.yml b/.github/workflows/pr-python-checks.yml index 896235410630..792fe4d46b32 100644 --- a/.github/workflows/pr-python-checks.yml +++ b/.github/workflows/pr-python-checks.yml @@ -11,7 +11,7 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v3 - + - name: Set up Python uses: actions/setup-python@v4 with: @@ -20,7 +20,7 @@ jobs: cache-dependency-path: | backend/requirements/default.txt backend/requirements/dev.txt - - run: | + - run: | python -m pip install --upgrade pip pip install -r backend/requirements/default.txt pip install -r backend/requirements/dev.txt @@ -30,6 +30,11 @@ jobs: cd backend mypy . + - name: Run ruff + run: | + cd backend + ruff . + - name: Check import order with reorder-python-imports run: | cd backend diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml index 1d37994a642e..01ba6fe6b849 100644 --- a/backend/.pre-commit-config.yaml +++ b/backend/.pre-commit-config.yaml @@ -23,10 +23,16 @@ repos: - id: autoflake args: [ '--remove-all-unused-imports', '--remove-unused-variables', '--in-place' , '--recursive'] - # We would like to have a mypy pre-commit hook, but due to the fact that - # pre-commit runs in it's own isolated environment, we would need to install + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.0.286 + hooks: + - id: ruff + + # We would like to have a mypy pre-commit hook, but due to the fact that + # pre-commit runs in it's own isolated environment, we would need to install # and keep in sync all dependencies so mypy has access to the appropriate type - # stubs. This does not seem worth it at the moment, so for now we will stick to + # stubs. This does not seem worth it at the moment, so for now we will stick to # having mypy run via Github Actions / manually by contributors # - repo: https://github.com/pre-commit/mirrors-mypy # rev: v1.1.1 @@ -38,15 +44,15 @@ repos: # # with requirements/dev.txt + requirements/default.txt # additional_dependencies: [ # alembic==1.10.4, - # types-beautifulsoup4==4.12.0.3, - # types-html5lib==1.1.11.13, - # types-oauthlib==3.2.0.9, - # types-psycopg2==2.9.21.10, - # types-python-dateutil==2.8.19.13, - # types-regex==2023.3.23.1, - # types-requests==2.28.11.17, - # types-retry==0.9.9.3, + # types-beautifulsoup4==4.12.0.3, + # types-html5lib==1.1.11.13, + # types-oauthlib==3.2.0.9, + # types-psycopg2==2.9.21.10, + # types-python-dateutil==2.8.19.13, + # types-regex==2023.3.23.1, + # types-requests==2.28.11.17, + # types-retry==0.9.9.3, # types-urllib3==1.26.25.11 # ] # # TODO: add back once errors are addressed - # # args: [--strict] \ No newline at end of file + # # args: [--strict] diff --git a/backend/pyproject.toml b/backend/pyproject.toml new file mode 100644 index 000000000000..a9cf3650e134 --- /dev/null +++ b/backend/pyproject.toml @@ -0,0 +1,14 @@ +[tool.mypy] +plugins = "sqlalchemy.ext.mypy.plugin" +mypy_path = "$MYPY_CONFIG_FILE_DIR" +explicit_package_bases = true +disallow_untyped_defs = true + +[tool.ruff] +ignore = [] +line-length = 130 +select = [ + "E", + "F", + "W", +] diff --git a/backend/requirements/dev.txt b/backend/requirements/dev.txt index b88bf65d3700..d5755e63d06a 100644 --- a/backend/requirements/dev.txt +++ b/backend/requirements/dev.txt @@ -3,6 +3,7 @@ mypy-extensions==1.0.0 mypy==1.1.1 pre-commit==3.2.2 reorder-python-imports==3.9.0 +ruff==0.0.286 types-beautifulsoup4==4.12.0.3 types-html5lib==1.1.11.13 types-oauthlib==3.2.0.9 diff --git a/backend/setup.cfg b/backend/setup.cfg deleted file mode 100644 index 11e5d3a70446..000000000000 --- a/backend/setup.cfg +++ /dev/null @@ -1,8 +0,0 @@ -[mypy] -plugins = sqlalchemy.ext.mypy.plugin -mypy_path = $MYPY_CONFIG_FILE_DIR -explicit_package_bases = True -disallow_untyped_defs = True - -[flake8] -max-line-length = 88