Skip to content

Commit

Permalink
[SymForce] Use ruff format instead of black
Browse files Browse the repository at this point in the history
Ruff is much faster than black, to the point where we can just forget
about disabling autoformat because of performance on large files.  On
one large example generated file, black takes 56s to format while ruff
takes 0.25s (much less than the rest of time spent doing codegen).

Ruff also replaces isort.

This is only using `ruff format`, not yet using `ruff lint` to replace
pylint.

Topic: sf-ruff-format
Reviewers: andrew-p,william-a,william-s,danny,eric,bradley,nathan,hayk
GitOrigin-RevId: ed3b2ee36c3d9ee4f13a80952da419c4bf744b8d
  • Loading branch information
aaron-skydio authored and nathan-skydio committed Dec 23, 2023
1 parent 61d16eb commit d7d24d6
Show file tree
Hide file tree
Showing 88 changed files with 487 additions and 483 deletions.
20 changes: 8 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@ FIND_CPP_FILES_TO_FORMAT=find . \
-and -not -path "./symforce/benchmarks/matrix_multiplication/gen/*" \
-regextype posix-extended -regex ".*\.(h|cc|tcc)"

# Format using black, isort, and clang-format
BLACK_CMD=$(PYTHON) -m black . --exclude "third_party/.*|build/.*|\.eggs/.*"
ISORT_CMD=$(PYTHON) -m isort \
--skip-glob="**/third_party" \
--extend-skip-glob="**/build" \
--extend-skip-glob="**/.eggs" \
.
# Format using ruff and clang-format
RUFF_FORMAT_CMD=ruff format .
RUFF_ISORT_CMD=ruff check . --select=I
format:
$(BLACK_CMD)
$(ISORT_CMD)
$(RUFF_FORMAT_CMD)
$(RUFF_ISORT_CMD) --fix
$(FIND_CPP_FILES_TO_FORMAT) | xargs $(CPP_FORMAT) -i

# Check formatting using black and clang-format - print diff, do not modify files
# Check formatting using ruff and clang-format - print diff, do not modify files
check_format:
$(BLACK_CMD) --check --diff
$(ISORT_CMD) --check --diff
$(RUFF_FORMAT_CMD) --check --diff
$(RUFF_ISORT_CMD) --diff
$(FIND_CPP_FILES_TO_FORMAT) | xargs $(CPP_FORMAT) --dry-run -Werror

# Check type hints using mypy
Expand Down
28 changes: 6 additions & 22 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ beautifulsoup4==4.12.2
# via
# furo
# nbconvert
black[jupyter]==21.12b0
# via symforce (setup.py)
bleach==6.1.0
# via nbconvert
breathe==4.35.0
Expand All @@ -46,10 +44,7 @@ charset-normalizer==3.3.0
clang-format==17.0.2
# via symforce (setup.py)
click==8.0.4
# via
# black
# pip-tools
# symforce (setup.py)
# via pip-tools
cmake==3.26.4
# via symforce (setup.py)
comm==0.1.4
Expand Down Expand Up @@ -105,15 +100,11 @@ importlib-resources==6.1.0
ipykernel==6.25.2
# via symforce (setup.py)
ipython==8.12.3
# via
# black
# ipykernel
# via ipykernel
ipython-genutils==0.2.0
# via symforce (setup.py)
isort==5.12.0
# via
# pylint
# symforce (setup.py)
# via pylint
jedi==0.19.1
# via ipython
jinja2==3.0.3
Expand Down Expand Up @@ -176,9 +167,7 @@ mpmath==1.3.0
mypy==0.910
# via symforce (setup.py)
mypy-extensions==0.4.4
# via
# black
# mypy
# via mypy
myst-parser==2.0.0
# via symforce (setup.py)
nbclient==0.8.0
Expand Down Expand Up @@ -223,8 +212,6 @@ pandocfilters==1.5.0
# via nbconvert
parso==0.8.3
# via jedi
pathspec==0.11.2
# via black
pexpect==4.8.0
# via ipython
pickleshare==0.7.5
Expand All @@ -237,7 +224,6 @@ pkgutil-resolve-name==1.3.10
# via jsonschema
platformdirs==3.11.0
# via
# black
# jupyter-core
# pylint
plotly==5.17.0
Expand Down Expand Up @@ -291,6 +277,8 @@ rpds-py==0.10.4
# via
# jsonschema
# referencing
ruff==0.1.7
# via symforce (setup.py)
scipy==1.10.1
# via symforce (setup.py)
six==1.16.0
Expand Down Expand Up @@ -343,13 +331,10 @@ tenacity==8.2.3
# via plotly
tinycss2==1.2.1
# via nbconvert
tokenize-rt==5.2.0
# via black
toml==0.10.2
# via mypy
tomli==1.2.3
# via
# black
# build
# pylint
# pyproject-hooks
Expand Down Expand Up @@ -388,7 +373,6 @@ types-setuptools==68.2.0.0
typing-extensions==4.8.0
# via
# astroid
# black
# ipython
# mypy
# pylint
Expand Down
6 changes: 2 additions & 4 deletions docs/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ When debugging a specific test, the use of `ipdb <https://pypi.org/project/ipdb/
*************************************************
Formatting
*************************************************
Symforce uses the `Black <https://github.com/psf/black>`_ formatter for Python code. To quote the authors:
Symforce uses the `Ruff <https://github.com/astral-sh/ruff>`_ formatter for Python code.

`Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from nagging about formatting. You will save time and mental energy for more important matters.`

Running ``make format`` will format the entire codebase. It's recommended to develop with `VSCode <https://code.visualstudio.com/>`_ and integrate black.
Running ``make format`` will format the entire codebase. It's recommended to develop with `VSCode <https://code.visualstudio.com/>`_ and integrate black or ruff.

*************************************************
Templates
Expand Down
1 change: 1 addition & 0 deletions gen/python/sym/ops/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions gen/python/sym/ops/atan_camera_cal/camera_ops.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions gen/python/sym/ops/double_sphere_camera_cal/camera_ops.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gen/python/sym/ops/linear_camera_cal/camera_ops.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions gen/python/sym/ops/polynomial_camera_cal/camera_ops.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions gen/python/sym/ops/pose2/group_ops.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d7d24d6

Please sign in to comment.