Skip to content

Add ruff as the linter and formatter #527

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

Merged
merged 13 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .git-blame-ignore-revs

This file was deleted.

18 changes: 18 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Ruff

on:
push:
branches:
- master
pull_request:
workflow_dispatch:

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
src: "./oioioi"
version: 0.11.12
18 changes: 0 additions & 18 deletions .pep8rc

This file was deleted.

8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.12
hooks:
- id: ruff-check
files: ^oioioi/
args: [ --fix, --exit-non-zero-on-fix ]
- id: ruff-format
138 changes: 0 additions & 138 deletions .pylintrc

This file was deleted.

3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ RUN rm /bin/sh && ln -s /bin/bash /bin/sh && \
RUN sed -i -e "s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/" /etc/locale.gen && \
locale-gen

COPY ./entrypoint_checks.sh /entrypoint_checks.sh
RUN chmod +x /entrypoint_checks.sh && chown oioioi /entrypoint_checks.sh

# Installing python dependencies
USER oioioi

Expand Down
52 changes: 31 additions & 21 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,6 @@ you are connected to after using docker exec -it “web” /bin/bash. The defaul
docker compose -f docker-compose-dev.yml exec "web" ../oioioi/test.sh
docker compose -f docker-compose-dev.yml exec "web" ../oioioi/test.sh oioioi/{name_of_the_app}/

Running static code analysis tools locally (requires Docker)
~~~~~~~~~~~~~~~~~~~~~~~

The static code analysis tools currently in use for python code are black, isort, pep8 and pylint.
All of them can be run locally using the `run_static.sh` shell script.
In order for the script to work the `web` container from docker-compose-dev.yml needs to be running.
The docker image for the project needs to be rebuild if you are migrating from and older Dockerfile version (rebuild the image if you are getting error messages that isort or black are not installed).
Commands for building the image and starting up the containers are listed in the paragraphs above.

When running all tools at once or when running pep8 and pylint independently only the recently modified files (files modified in the most recent commit or staged changes) will be processed.

To run all tools at once::

./run_static.sh

To run one of the tools::

./run_static.sh black
./run_static.sh isort
./run_static.sh pylint
./run_static.sh pep8

Script toolbox for Docker (development)
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -121,6 +100,37 @@ Developer environment can be easily set up by running::

For system requirements check `easy_toolbox.py`.

Running static code analysis tools locally
~~~~~~~~~~~~~~~~~~~~~~~
.. _ruff: https://github.com/astral-sh/ruff

The static code analysis tool (linter and formatter) currently in use for Python code is `ruff`_.

It can can be run locally using the::

./easy_toolbox.py ruff

In order for the script to work the `web` container from `docker-compose-dev.yml` needs to be running.

To allow ruff to fix the errors, add the `--fix` flag::

./easy_toolbox.py ruff --fix

To avoid having to run this command every time before committing, you can install `pre-commit` along with ruff::

pip install -r requirements_static.txt

Then simply run the following command::

pre-commit install

From now on, ruff will run automatically before each commit - just keep in mind
that ruff will only run on the files you have modified.
Copy link
Contributor

Choose a reason for hiding this comment

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

You could add suggestion that it is possible run on all files using: pre-commit run --all-files

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.


If you want to check all files, not just the modified ones, you can run::

pre-commit run --all-files

Manual installation (deprecated)
~~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 3 additions & 1 deletion easy_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
("up", "Run all SIO2 containers", "up -d"),
("down", "Stop and remove all SIO2 containers", "down"),
("wipe", "Stop all SIO2 containers and DESTROY all data", "down -v", True),
("run", "Run django server and webpack",
("run", "Run django server and webpack",
'{exec} web conc -n js,py -c yellow,green -k "npm --prefix ../oioioi run -s watch" "python3 manage.py runserver 0.0.0.0:8000"'),
("stop", "Stop all SIO2 containers", "stop"),
("bash", "Open command prompt on web container.", "{exec} web bash"),
Expand All @@ -45,6 +45,8 @@
'{exec} web bash -c "echo CAPTCHA_TEST_MODE=True >> settings.py"'),
("npm", "Run npm command.", "{exec} web npm --prefix ../oioioi {extra_args}"),
("eslint", "Run javascript linter.", "{exec} web npm --prefix ../oioioi run lint"),
("ruff", "Run Ruff, a linter and formatter. You can add `--fix` to automatically fix some errors.","{exec} -w /sio2/oioioi web bash -c 'ruff check . {extra_args} ; echo ; ruff format .'",
)
]

longest_command_arg = max([len(command[0]) for command in RAW_COMMANDS])
Expand Down
36 changes: 0 additions & 36 deletions entrypoint_checks.sh

This file was deleted.

2 changes: 1 addition & 1 deletion oioioi/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# pylint: disable=unused-import
# Important. This import is to register signal handlers. Do not remove it.
import oioioi.base.signal_handlers
import oioioi.base.signal_handlers # noqa: F401
from oioioi.base.captcha_check import captcha_check
from oioioi.base.setup_check import setup_check
from oioioi.contests.models import Contest
Expand Down
2 changes: 1 addition & 1 deletion oioioi/pa/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from oioioi.contests.models import ProblemInstance

# pylint: disable=unused-import
from oioioi.pa.score import PAScore # Registers the PA score type
from oioioi.pa.score import PAScore # noqa: F401 # Registers the PA score type
from oioioi.participants.models import RegistrationModel

check_django_app_dependencies(__name__, ['oioioi.participants'])
Expand Down
2 changes: 1 addition & 1 deletion oioioi/portals/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from mptt.exceptions import InvalidMove

# pylint: disable=W0611
import oioioi.portals.handlers
import oioioi.portals.handlers # noqa: F401
from oioioi.base.main_page import register_main_page_view
from oioioi.base.menu import account_menu_registry
from oioioi.base.permissions import enforce_condition, is_superuser, not_anonymous
Expand Down
2 changes: 1 addition & 1 deletion oioioi/problems/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
# instead of views.py to avoid circular imports. We still import
# it here to use it in urls.py.
from oioioi.problems.problem_site import (
problem_site_statement_zip_view,
problem_site_statement_zip_view, # noqa: F401
problem_site_tab_registry,
)
from oioioi.problems.problem_sources import problem_sources
Expand Down
Loading
Loading