feanil/pycodestyle to ruff (#38076)

* 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 <noreply@anthropic.com>

* feat: add ruff Makefile target

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* style: Apply suggestions from code review

Remove unnecessary ignores.

Co-authored-by: Braden MacDonald <braden@opencraft.com>

* 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 <noreply@anthropic.com>
Co-authored-by: Braden MacDonald <braden@opencraft.com>
This commit is contained in:
Feanil Patel
2026-03-04 12:33:06 -05:00
committed by GitHub
parent b5a9fff969
commit 467495cb22
9 changed files with 30 additions and 40 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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,
)

View File

@@ -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

View File

@@ -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).

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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