From 467495cb22e6b5fe57278db244db9bd5b213877e Mon Sep 17 00:00:00 2001 From: Feanil Patel Date: Wed, 4 Mar 2026 12:33:06 -0500 Subject: [PATCH] feanil/pycodestyle to ruff (#38076) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add ruff and configure it to match current pycodestyle rules Adds ruff to testing requirements and configures it in pyproject.toml to enforce the same E/W rules that pycodestyle 2.8.x was enforcing. Two additional rules (E714, E721) that pycodestyle 2.8.x did not enforce are explicitly ignored for now and can be cleaned up in a follow-up. Part of the migration from pycodestyle → ruff. Co-Authored-By: Claude Sonnet 4.6 * feat: add ruff Makefile target Co-Authored-By: Claude Sonnet 4.6 * feat: add ruff to quality CI workflow alongside pycodestyle Runs ruff alongside pycodestyle so we can validate parity before removing pycodestyle in the next commit. Co-Authored-By: Claude Sonnet 4.6 * feat: remove pycodestyle, replaced by ruff - Remove pycodestyle from requirements - Remove pycodestyle version constraint (pinned to <2.9.0 due to a false positive E275 bug that is no longer relevant) - Remove [pycodestyle] config from setup.cfg (config now lives in pyproject.toml under [tool.ruff]) - Remove pycodestyle Makefile target - Remove make pycodestyle from quality CI workflow Co-Authored-By: Claude Sonnet 4.6 * style: Apply suggestions from code review Remove unnecessary ignores. Co-authored-by: Braden MacDonald * style: Fix a style isusue and remove ignores. Most of these ignores are unnecessary as we're passing them now and we want to check them in the future. E714 only had one fixable violation so we just fixed it. * style: Update ruff config and workflows * Update the call to `ruff` so that it outputs in a github friendly manner. * Remove ruff exclusions that are already covered by .gitignore which ruff respects. * chore: Recompile requirements. Update the requirements to drop pycodestyle and add ruff. --------- Co-authored-by: Claude Sonnet 4.6 Co-authored-by: Braden MacDonald --- .github/workflows/quality-checks.yml | 2 +- Makefile | 4 ++-- .../contentstore/views/tests/test_block.py | 2 +- pyproject.toml | 21 ++++++++++++++++++ requirements/constraints.txt | 5 ----- requirements/edx/development.txt | 6 ++--- requirements/edx/testing.in | 2 +- requirements/edx/testing.txt | 6 ++--- setup.cfg | 22 ------------------- 9 files changed, 30 insertions(+), 40 deletions(-) diff --git a/.github/workflows/quality-checks.yml b/.github/workflows/quality-checks.yml index 3fd680b237..77f094ba3d 100644 --- a/.github/workflows/quality-checks.yml +++ b/.github/workflows/quality-checks.yml @@ -78,7 +78,7 @@ jobs: PIP_SRC: ${{ runner.temp }} TARGET_BRANCH: ${{ github.base_ref }} run: | - make pycodestyle + ruff check --output-format=github . make xsslint make pii_check make check_keywords diff --git a/Makefile b/Makefile index 2ca7fbc584..bd70a15cd7 100644 --- a/Makefile +++ b/Makefile @@ -171,8 +171,8 @@ xsslint: ## check xss for quality issuest --config=scripts.xsslint_config \ --thresholds=scripts/xsslint_thresholds.json -pycodestyle: ## check python files for quality issues - pycodestyle . +ruff: ## check python files with ruff + ruff check . ## Re-enable --lint flag when this issue https://github.com/openedx/edx-platform/issues/35775 is resolved pii_check: ## check django models for pii annotations diff --git a/cms/djangoapps/contentstore/views/tests/test_block.py b/cms/djangoapps/contentstore/views/tests/test_block.py index 846c62d243..31f5244e2f 100644 --- a/cms/djangoapps/contentstore/views/tests/test_block.py +++ b/cms/djangoapps/contentstore/views/tests/test_block.py @@ -3506,7 +3506,7 @@ class TestXBlockInfo(ItemTest): self.validate_xblock_info_consistency( child_response, has_child_info=( - not child_response.get("child_info", None) is None + child_response.get("child_info", None) is not None ), course_outline=course_outline, ) diff --git a/pyproject.toml b/pyproject.toml index 60a8d20b23..8dc968170c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -205,6 +205,27 @@ norecursedirs = ". .* *.egg build conf dist node_modules test_root cms/envs lms/ python_classes = [] python_files = ["tests.py", "test_*.py", "tests_*.py", "*_tests.py", "__init__.py"] +[tool.ruff] +line-length = 120 +exclude = [ + "migrations", + "test_root/staticfiles", +] + +[tool.ruff.lint] +select = ["E", "W"] +ignore = [ + "E402", # module level import not at top of file + "E501", # line too long (pylint enforces this at 120 chars while ignoring trailing comments) + "E722", # do not use bare 'except' + "E731", # do not assign a lambda expression + "E741", # ambiguous variable name. TODO: fix the ~9 issues and re-enable this. + # The rules below were added in pycodestyle 2.9.x but we were pinned to 2.8.x, + # so they represent new findings. Keeping them ignored here to make this an + # equivalent swap; they can be cleaned up in a follow-up. + "E721", # use `isinstance()` for type comparisons +] + [tool.isort] indent = " " line_length = 120 diff --git a/requirements/constraints.txt b/requirements/constraints.txt index ee608faee3..7678425d51 100644 --- a/requirements/constraints.txt +++ b/requirements/constraints.txt @@ -78,11 +78,6 @@ openai<=0.28.1 # Issue for unpinning: https://github.com/openedx/edx-platform/issues/35267 path<16.12.0 -# Date: 2022-08-03 -# pycodestyle==2.9.0 generates false positive error E275. -# Constraint can be removed once the issue https://github.com/PyCQA/pycodestyle/issues/1090 is fixed. -pycodestyle<2.9.0 - # Date: 2021-08-25 # At the time of writing this comment, we do not know whether py2neo>=2022 # will support our currently-deployed Neo4j version (3.5). diff --git a/requirements/edx/development.txt b/requirements/edx/development.txt index 8d9e2a1f64..bc6dc67a57 100644 --- a/requirements/edx/development.txt +++ b/requirements/edx/development.txt @@ -1570,10 +1570,6 @@ pycasbin==2.8.0 # -r requirements/edx/testing.txt # casbin-django-orm-adapter # openedx-authz -pycodestyle==2.8.0 - # via - # -c requirements/constraints.txt - # -r requirements/edx/testing.txt pycountry==26.2.16 # via # -r requirements/edx/doc.txt @@ -1899,6 +1895,8 @@ rsa==4.9.1 # -r requirements/edx/doc.txt # -r requirements/edx/testing.txt # google-auth +ruff==0.15.4 + # via -r requirements/edx/testing.txt rules==3.5 # via # -r requirements/edx/doc.txt diff --git a/requirements/edx/testing.in b/requirements/edx/testing.in index 14a0c781da..275460512f 100644 --- a/requirements/edx/testing.in +++ b/requirements/edx/testing.in @@ -29,7 +29,7 @@ httpretty # Library for mocking HTTP requests, used in many test import-linter # Tool for making assertions about which modules can import which others isort # For checking and fixing the order of imports mock # Deprecated alias to standard library `unittest.mock` -pycodestyle # Checker for compliance with the Python style guide (PEP 8) +ruff # Fast Python linter and formatter polib # Library for manipulating gettext translation files, used to test paver i18n commands pyquery # jQuery-like API for retrieving fragments of HTML and XML files in tests pytest # Testing framework diff --git a/requirements/edx/testing.txt b/requirements/edx/testing.txt index 8467a577e8..8f848de526 100644 --- a/requirements/edx/testing.txt +++ b/requirements/edx/testing.txt @@ -1199,10 +1199,6 @@ pycasbin==2.8.0 # -r requirements/edx/base.txt # casbin-django-orm-adapter # openedx-authz -pycodestyle==2.8.0 - # via - # -c requirements/constraints.txt - # -r requirements/edx/testing.in pycountry==26.2.16 # via -r requirements/edx/base.txt pycparser==3.0 @@ -1452,6 +1448,8 @@ rsa==4.9.1 # via # -r requirements/edx/base.txt # google-auth +ruff==0.15.4 + # via -r requirements/edx/testing.in rules==3.5 # via # -r requirements/edx/base.txt diff --git a/setup.cfg b/setup.cfg index 7e1ab370a0..e69de29bb2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,22 +0,0 @@ -[pycodestyle] -# error codes: https://pycodestyle.readthedocs.io/en/latest/intro.html#error-codes -# E501: line too long -# E265: block comment should start with '# ' -# We ignore this because pep8 used to erroneously lump E266 into it also. -# We should probably fix these now. -# E266: too many leading '#' for block comment -# We have lots of comments that look like "##### HEADING #####" which violate -# this rule, because they don't have a space after the first #. However, -# they're still perfectly reasonable comments, so we disable this rule. -# W602: deprecated form of raising exception -# We do this in a few places to modify the exception message while preserving -# the traceback. See this blog post for more info: -# http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html -# It's a little unusual, but we have good reasons for doing so, so we disable -# this rule. -# E203: whitespace before ':' -# We ignore this because of black formatter defaults -# E305,E402,E722,E731,E741,E743,W503,W504: errors and warnings added since pep8/pycodestyle -# 1.5.7 that we haven't cleaned up yet -ignore=E203,E265,E266,E305,E402,E501,E722,E731,E741,E743,W503,W504,W602 -exclude=migrations,.git,.pycharm_helpers,.tox,test_root/staticfiles,node_modules