Skip to content

Commit

Permalink
Bug 1811850 - [lint] Replace flake8 linter with ruff, r=linter-review…
Browse files Browse the repository at this point in the history
…ers,sylvestre

Ruff is a very fast linter implemented in Rust and it can act as a drop-in
replacement for flake8. When running the same set of rules across all files
in mozilla-central (without mozlint), flake8 takes 900 seconds whereas ruff
takes 0.9 seconds.

Ruff also implements rules from other popular Python linters such as pylint,
isort and pyupgrade. There are even plans to implement feature parity with
black in the future. Ultimately, it can become our one stop shop for all Python
linting and formatting.

This stack will swap out all our Python lint tools for ruff (excluding black
for now).

Differential Revision: https://phabricator.services.mozilla.com/D172313
  • Loading branch information
ahal committed Mar 17, 2023
1 parent 1edefc1 commit 8a4d48a
Show file tree
Hide file tree
Showing 29 changed files with 469 additions and 633 deletions.
130 changes: 0 additions & 130 deletions .flake8

This file was deleted.

3 changes: 3 additions & 0 deletions .hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ _OPT\.OBJ/
# Unit test
\.pytest_cache/

# Ruff
\.ruff_cache/

# Ignore files created when running a reftest.
^lextab.py$

Expand Down
16 changes: 5 additions & 11 deletions docs/code-quality/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ In this document, we try to list these all tools.
- Meta bug
- More info
- Upstream
* - Flake8
- Yes (with `autopep8 <https://github.com/hhatto/autopep8>`_)
- `bug 1155970 <https://bugzilla.mozilla.org/show_bug.cgi?id=1155970>`__
- :ref:`Flake8`
- http://flake8.pycqa.org/
* - ruff
- Yes
- `bug 1811850 <https://bugzilla.mozilla.org/show_bug.cgi?id=1811850>`__
- :ref:`ruff`
- https://github.com/charliermarsh/ruff
* - black
- Yes
- `bug 1555560 <https://bugzilla.mozilla.org/show_bug.cgi?id=1555560>`__
Expand All @@ -105,12 +105,6 @@ In this document, we try to list these all tools.
- `bug 1623024 <https://bugzilla.mozilla.org/show_bug.cgi?id=1623024>`__
- :ref:`pylint`
- https://www.pylint.org/
* - Python 2/3 compatibility check
-
- `bug 1496527 <https://bugzilla.mozilla.org/show_bug.cgi?id=1496527>`__
- :ref:`Python 2/3 compatibility check`
-


.. list-table:: Rust
:widths: 20 20 20 20 20
Expand Down
46 changes: 0 additions & 46 deletions docs/code-quality/lint/linters/flake8.rst

This file was deleted.

44 changes: 44 additions & 0 deletions docs/code-quality/lint/linters/ruff.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Ruff
====

`Ruff <https://github.com/charliermarsh/ruff>`_ is an extremely fast Python
linter and formatter, written in Rust. It can process all of mozilla-central in
under a second, and implements rule sets from a large array of Python linters
and formatters, including:

* flake8 (pycodestyle, pyflakes and mccabe)
* isort
* pylint
* pyupgrade
* and many many more!

Run Locally
-----------

The mozlint integration of ruff can be run using mach:

.. parsed-literal::
$ mach lint --linter ruff <file paths>
Configuration
-------------

Ruff is configured in the root `pyproject.toml`_ file. Additionally, ruff will
pick up any ``pyproject.toml`` or ``ruff.toml`` files in subdirectories. The
settings in these files will only apply to files contained within these
subdirs. For more details on configuration discovery, see the `configuration
documentation`_.

For a list of options, see the `settings documentation`_.

Sources
-------

* `Configuration (YAML) <https://searchfox.org/mozilla-central/source/tools/lint/ruff.yml>`_
* `Source <https://searchfox.org/mozilla-central/source/tools/lint/python/ruff.py>`_

.. _pyproject.toml: https://searchfox.org/mozilla-central/source/pyproject.toml
.. _configuration documentation: https://beta.ruff.rs/docs/configuration/
.. _settings documentation: https://beta.ruff.rs/docs/settings/
127 changes: 127 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
[tool.ruff]
line-length = 99
# See https://beta.ruff.rs/docs/rules/ for a full list of rules.
select = [
"E", "W", # pycodestyle
"F", # pyflakes
]
ignore = [
# These should be triaged and either fixed or moved to the list below.
"E713", "E714", "W605",

# These are intentionally ignored (not necessarily for good reason).
"E741",

# These are handled by black.
"E1", "E4", "E5", "W2", "W5"
]
builtins = ["gdb"]
exclude = [
# These paths should be triaged and either fixed or moved to the list below.
"devtools/shared",
"dom/bindings/Codegen.py",
"dom/bindings/parser/WebIDL.py",
"dom/bindings/parser/tests/test_arraybuffer.py",
"dom/bindings/parser/tests/test_securecontext_extended_attribute.py",
"gfx/tests",
"ipc/ipdl/ipdl",
"layout/base/tests/marionette",
"layout/reftests/border-image",
"layout/reftests/fonts",
"layout/reftests/w3c-css",
"layout/style",
"media/libdav1d/generate_source.py",
"moz.configure",
"netwerk/dns/prepare_tlds.py",
"netwerk/protocol/http/make_incoming_tables.py",
"python/l10n/fluent_migrations",
"security/manager/ssl/tests/unit",
"servo/components/style",
"testing/condprofile/condprof/android.py",
"testing/condprofile/condprof/creator.py",
"testing/condprofile/condprof/desktop.py",
"testing/condprofile/condprof/runner.py",
"testing/condprofile/condprof/scenarii/heavy.py",
"testing/condprofile/condprof/scenarii/settled.py",
"testing/condprofile/condprof/scenarii/synced.p",
"testing/condprofile/condprof/helpers.py",
"testing/jsshell/benchmark.py",
"testing/marionette/mach_commands.py",
"testing/mozharness/docs",
"testing/mozharness/examples",
"testing/mozharness/external_tools",
"testing/mozharness/mach_commands.py",
"testing/mozharness/manifestparser",
"testing/mozharness/mozprocess",
"testing/mozharness/setup.py",
"testing/parse_build_tests_ccov.py",
"testing/runtimes/writeruntimes.py",
"testing/tools/iceserver/iceserver.py",
"testing/tools/websocketprocessbridge/websocketprocessbridge.py",
"toolkit/components/featuregates",
"toolkit/content/tests/chrome/file_about_networking_wsh.py",
"toolkit/library/build/dependentlibs.py",
"toolkit/locales/generate_update_locale.py",
"toolkit/mozapps",
"toolkit/moz.configure",
"toolkit/nss.configure",

# mako files are not really python files
"*.mako.py",

# These paths are intentionally excluded (not necessarily for good reason).
"build/moz.configure/*.configure",
"build/pymake/",
"browser/extensions/mortar/ppapi/",
"browser/moz.configure",
"dom/canvas/test/webgl-conf/checkout/closure-library/",
"editor/libeditor/tests/browserscope/",
"intl/icu/",
"ipc/chromium/src/third_party/",
"js/*.configure",
"gfx/angle/",
"gfx/harfbuzz",
"gfx/skia/",
"memory/moz.configure",
"mobile/android/*.configure",
"node_modules",
"python/mozbuild/mozbuild/test/configure/data",
"security/nss/",
"testing/marionette/harness/marionette_harness/runner/mixins",
"testing/marionette/harness/marionette_harness/tests",
"testing/mochitest/pywebsocket3",
"testing/mozharness/configs/test/test_malformed.py",
"testing/web-platform/tests",
"tools/lint/test/files",
"tools/crashreporter/*.configure",
".ycm_extra_conf.py",
]

[tool.ruff.per-file-ignores]
# These paths are intentionally excluded.
"ipc/ipdl/*" = ["F403", "F405"]
"layout/tools/reftest/selftest/conftest.py" = ["F811"]
# cpp_eclipse has a lot of multi-line embedded XML which exceeds line length
"python/mozbuild/mozbuild/backend/cpp_eclipse.py" = ["E501"]
"testing/firefox-ui/**/__init__.py" = ["F401"]
"testing/marionette/**/__init__.py" = ["F401"]
"testing/mochitest/tests/python/conftest.py" = ["F811"]
"testing/mozbase/manifestparser/tests/test_filters.py" = ["E731"]
"testing/mozbase/mozlog/tests/test_formatters.py" = ["E501"]
"testing/mozharness/configs/*" = ["E501"]
"**/*.configure" = ["F821"]
# These paths contain Python-2 only syntax.
"build/compare-mozconfig/compare-mozconfigs.py" = ["F821"]
"build/midl.py" = ["F821"]
"build/pgo/genpgocert.py" = ["F821"]
"config/MozZipFile.py" = ["F821"]
"config/check_source_count.py" = ["F821"]
"config/tests/unitMozZipFile.py" = ["F821"]
"ipc/pull-chromium.py" = ["F633"]
"js/src/**" = ["F633", "F821"]
"python/mozbuild/mozbuild/action/dump_env.py" = ["F821"]
"python/mozbuild/mozbuild/dotproperties.py" = ["F821"]
"python/mozbuild/mozbuild/testing.py" = ["F821"]
"python/mozbuild/mozbuild/util.py" = ["F821"]
"testing/mozharness/mozharness/mozilla/testing/android.py" = ["F821"]
"testing/mochitest/runtests.py" = ["F821"]
Loading

0 comments on commit 8a4d48a

Please sign in to comment.