Skip to content

Commit

Permalink
Replace black, flake8, pyupgrade with ruff for Python linting and for…
Browse files Browse the repository at this point in the history
…matting (#3248)
  • Loading branch information
flodolo authored Jun 3, 2024
1 parent a19ec1f commit d650739
Show file tree
Hide file tree
Showing 127 changed files with 176 additions and 333 deletions.
15 changes: 0 additions & 15 deletions .flake8

This file was deleted.

36 changes: 5 additions & 31 deletions .github/workflows/py-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ on:
workflow_dispatch:

jobs:
flake8:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -29,33 +29,7 @@ jobs:
run: |
pip install -U pip
pip install -r requirements/lint.txt
- name: flake8
run: flake8 pontoon

black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install Dependencies
run: |
pip install -U pip
pip install -r requirements/lint.txt
- name: black
run: black --check pontoon

pyupgrade:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install Dependencies
run: |
pip install -U pip
pip install -r requirements/lint.txt
- name: pyupgrade
run: pyupgrade --py311-plus *.py `find pontoon -name \*.py`
- name: ruff lint
run: ruff check pontoon
- name: ruff format
run: ruff format --check pontoon
29 changes: 9 additions & 20 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,22 @@ To run the required linters on the Python and the Javascript code at once you ca
Python code conventions
=======================

Our Python code is automatically formatted using `black <https://black.readthedocs.io/en/stable/>`_.
Our Python code is automatically formatted using `ruff <https://docs.astral.sh/ruff/>`_.
We enforce that in our Continuous Integration, so you will need to run
black on your code before sending it for review.
ruff on your code before sending it for review.

You can run black locally either as an
`add-on in your code editor <https://black.readthedocs.io/en/stable/editor_integration.html>`_,
or as a `git pre-hook commit <https://black.readthedocs.io/en/stable/version_control_integration.html>`_.
You can run ruff locally either as an
`add-on in your code editor <https://docs.astral.sh/ruff/integrations/#vs-code-official>`_,
or as a `git pre-hook commit <https://docs.astral.sh/ruff/integrations/#pre-commit>`_.
Alternatively, you can format your code using:

.. code-block:: shell
$ make black
$ make ruff
We also require Python code to use newer syntax of Python. You can upgrade automatically by running:

.. code-block:: shell
$ make pyupgrade
Additionally, we use a linter to verify that imports are correct. You can run it with:

.. code-block:: shell
$ make flake8
In the rare case when you cannot fix a flake8 error, use ``# noqa`` to make the linter
ignore that error. Note that in most cases, it is better to fix the issues than ignoring them.
In the rare case when you cannot fix an error, use ``# noqa`` to make the linter
ignore that error (see `documentation <https://docs.astral.sh/ruff/linter/#error-suppression>`_).
Note that in most cases, it is better to fix the issues than ignoring them.


Javascript code conventions
Expand Down
32 changes: 11 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SITE_URL ?= http://localhost:8000
USER_ID?=1000
GROUP_ID?=1000

.PHONY: build build-translate build-tagadmin build-server server-env setup run clean shell ci test test-translate test-tagadmin test-server jest pytest format lint types eslint prettier check-prettier flake8 pyupgrade check-pyupgrade black check-black dropdb dumpdb loaddb sync-projects requirements
.PHONY: build build-translate build-tagadmin build-server server-env setup run clean shell ci test test-translate test-tagadmin test-server jest pytest format lint types eslint prettier check-prettier ruff check-ruff dropdb dumpdb loaddb sync-projects requirements

help:
@echo "Welcome to Pontoon!\n"
Expand All @@ -34,12 +34,9 @@ help:
@echo " eslint Runs a code linter on the JavaScript code"
@echo " prettier Runs the Prettier formatter"
@echo " check-prettier Runs a check for format issues with the Prettier formatter"
@echo " flake8 Runs the flake8 style guides on all Python code"
@echo " pyupgrade Upgrades all Python code to newer syntax of Python"
@echo " check-pyupgrade Runs a check for outdated syntax of Python with the pyupgrade formatter"
@echo " black Runs the black formatter on all Python code"
@echo " check-black Runs a check for format issues with the black formatter"
@echo " dropdb Completely remove the postgres container and its data"
@echo " ruff Runs the ruff formatter on all Python code"
@echo " check-ruff Runs a check for format issues with the ruff formatter"
@echo " dumpdb Create a postgres database dump with timestamp used as file name"
@echo " loaddb Load a database dump into postgres, file name in DB_DUMP_FILE"
@echo " sync-projects Runs the synchronization task on all projects"
Expand Down Expand Up @@ -99,9 +96,9 @@ test-server: pytest
pytest:
"${DC}" run ${run_opts} --rm server pytest --cov-report=xml:pontoon/coverage.xml --cov=. $(opts)

format: prettier pyupgrade black
format: prettier ruff

lint: types eslint check-prettier flake8 check-pyupgrade check-black
lint: types eslint check-prettier check-ruff

types:
npm run types -w translate
Expand All @@ -115,20 +112,13 @@ prettier:
check-prettier:
npm run check-prettier

flake8:
"${DC}" run --rm server flake8 pontoon/
ruff:
"${DC}" run --rm server ruff check --fix pontoon/
"${DC}" run --rm server ruff format pontoon/

pyupgrade:
"${DC}" run --rm server pyupgrade --exit-zero-even-if-changed --py311-plus *.py `find pontoon -name \*.py`

check-pyupgrade:
"${DC}" run --rm server pyupgrade --py311-plus *.py `find pontoon -name \*.py`

black:
"${DC}" run --rm server black pontoon/

check-black:
"${DC}" run --rm server black --check pontoon
check-ruff:
"${DC}" run --rm server ruff check pontoon
"${DC}" run --rm server ruff format --check pontoon

dropdb:
"${DC}" down --volumes postgresql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down
1 change: 0 additions & 1 deletion pontoon/actionlog/migrations/0002_auto_20200703_0709.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Migration(migrations.Migration):

dependencies = [
("actionlog", "0001_squashed_0002_auto_20200123_1843"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def delete_action_logs_for_pretranslations(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [
("actionlog", "0002_auto_20200703_0709"),
]
Expand Down
1 change: 1 addition & 0 deletions pontoon/allauth_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Because of concerns related to the security concerns it's a better to keep only selected
views and don't allow user to tamper with the state of an account.
"""

import importlib

from django.urls import path
Expand Down
4 changes: 2 additions & 2 deletions pontoon/base/management/commands/warmup_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def warmup_insights_cache(self):
path = reverse("pontoon.insights")
url = urljoin(SITE_URL, path)
keys = [
f"/pontoon.insights.views/team_pretranslation_quality",
f"/pontoon.insights.views/project_pretranslation_quality",
"/pontoon.insights.views/team_pretranslation_quality",
"/pontoon.insights.views/project_pretranslation_quality",
]
self.warmup_url(url, keys=keys)
self.stdout.write("Insights page warmed up.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0002_auto_20200322_1821.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,6 @@ def squashed_run_python(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [
("base", "0001_squashed_0154_auto_20200206_1736"),
("guardian", "__first__"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0002_auto_20200322_1821"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def make_projects_public(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [
("base", "0003_add_visibility_field_to_project"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0005_add_terminology_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def remove_terminology_project(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [
("base", "0004_make_old_projects_public_again"),
("terminology", "0002_term_entity"),
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0006_auto_20200703_0710.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0005_add_terminology_project"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0006_auto_20200703_0710"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0008_add_systran_locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0007_machinery_sources_translation"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def revert_system_user_email(apps, schema_editor):


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0010_comment_pinned.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0009_change_pontoon_users_emails"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0011_rename_download_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0010_comment_pinned"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0012_auto_20201020_1830.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0011_rename_download_prefix"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0013_transvision_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0012_auto_20201020_1830"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0014_auto_20210310_1945.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0013_transvision_remove"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0014_auto_20210310_1945"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0016_entity_key_required.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0015_userprofile_has_dismissed_addon_promotion"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0017_entity_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0016_entity_key_required"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0018_populate_entity_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def remove_entity_context(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [
("base", "0017_entity_context"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0019_alter_locale_cldr_plurals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0018_populate_entity_context"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0020_notification_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0019_alter_locale_cldr_plurals"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0021_alter_repository_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0020_notification_subscriptions"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0022_reviewed_suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("base", "0021_alter_repository_url"),
]
Expand Down
1 change: 0 additions & 1 deletion pontoon/base/migrations/0023_fuzzy_to_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def fuzzy_to_missing(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [
("base", "0022_reviewed_suggestions"),
]
Expand Down
Loading

0 comments on commit d650739

Please sign in to comment.