Closed heuristicus closed 1 year ago
I appreciate the enthusiasm, but I'm hesitant to introduce non-standard tools, even if they are widely-used. The ROS linter is already capable of handling the most egregious of formatting problems.
Looking at the changes you made, besides adding the formatter itself, most of the changes seem to be consistently using double-quotes instead of single-quotes for strings, and reducing the maximum line width from the 120 characters the ROS linter is already using.
I have to admit that I'm not familiar with roslint. Looking into it a little, adding roslint_python
to a package's CMakeLists just provides a new catkin command that can be run. In the case of this repository it would require running something like catkin roslint_spot_driver
. This command applies pycodestyle
to the listed files, which does not actually apply any formatting, but just outputs information about what does not conform to the python code style. That means that it's up to you to manually change each of these lines.
The key with the change I'm suggesting here is that the black formatter will automatically apply changes which conform to most of the PEP8 formatting guidelines (except line length, although that can be configured if desired). This takes manual effort out of the formatting process.
As mentioned in #56, I think having a standard formatting scheme would help with consistency in the code here. There is more work to be done beyond this simple formatting application, but this would be a good start.
Summary
I have applied the
black
code formatter to thesrc
andscripts
directories of the driver.You can find the documentation for the formatter at https://black.readthedocs.io/en/latest/index.html. It is used by numerous large python projects to apply consistent formatting. Since there are so few options, it means that there are not many things to argue about in terms of the details of formatting.
The formatter guarantees that the formatted code is exactly the same as the input code by comparing the AST before and after the formatting. If the AST would be changed by the formatting, the formatting fails.
Installation and usage
Installation on ubuntu is just
You can then run on a directory or specific file with
In this repository, the command would be
There are instructions at https://black.readthedocs.io/en/latest/integrations/editors.html to add the formatter to your editor, making it very simple to run.
Github action
In addition to applying formatting here, I have added a github action which will trigger on pushes and pull requests, and give a :x: or :heavy_check_mark: next to the PR or commit, indicating whether the code would be changed by running the formatter. This allows us to ensure that the formatting scheme is maintained by PRs.
Git pre-commit hook
You can also make it so that
git
will reject attempts to make a commit where the code would be changed by applying the formatter. To do this, you can use a pre-commit hook. In all git repositories on your machine you will have a directory.git/hooks
, which by default contains a lot of sample files.The minimal hook that you need to reject commits is
You can use this short script to install this into your repo (this will overwrite any existing pre-commit hook you have)
With that script in place, whenever you commit you will see
Formatting is correct, the commit goes through as normal after the check
Formatting is not correct, the commit will not go through