Best practice for code style

Lately I worked on a project that enforces flake8 conformance. And I learned to embrace it and think it is a good idea to use it for my own projects, too.

What is flake8

flake8 is a code analyzer that check if the code conforms to the pep8 rules.


As usual, there are some rules that I don’t like (e.g. line length). But thankfully, it is possible to configure this. This is done by adding a file .flake8 in the root of your repo, or even in its parent folder, if you have a lot repos.
Mine looks like this:

ignore = W191 E501 E722 W503

And if you use Visual Studio Code (it has extension for flake8 and shows the results inline, I find that very useful, (but if anyone knows an other IDE that can do that, too?).
this is my settings file (.vscode/settings.json):

	"editor.insertSpaces": false,
	"editor.formatOnSave": false,
	"editor.formatOnType": false,
	"flake8.args": [

But flake8 can be run from the command line, too.

Why all that

All this is mostly about code formatting, readability and such. But there are a few things that flake8 can find that are real bugs. The most important is F821. But F841 is also useful.

Automatic Code Formatting

I found one tool that is highly configureable and thous the most useful for me: autopep8.

I use this prompt in the terminal:

autopep8 --in-place --select E221,E226,E225,E201,E202,E302,E305,E251,E261,W292,W293 path/to/

or with the -r argument followed by a path to a folder. That will process all files in that folder.

autopep8 --in-place --select E221,E226,E225,E201,E202,E302,E305,E251,E261,W292,W293 -r path/to/folder

This will only tough issues with the peps in the command. Feel free do add/remove things.

Other very widely used code formatters are

  • Black

and quite new, but incredibly fast and rising in popularity:

  • Ruff

Both can also be set in VSCode to execute on file save, which is amazing, as it feels like a rubberband snapping your code-edits into tidyness right away. With Ruff (written in Rust) you can also ditch Pylance. Black is not very customizable, which is a deliberate descision by the developers and works well for most people. Ruff has more options to be customized, but the default is perfect for most users.

Thank you for posting your thoughts.

I agree that a fixed code style is a good thing, especially for collaborative projects (and possible collaborative projects like open source code).

The black formatter seems to be the standard across the usual Python suspects these days: fontTools, ufo2ft, fontmake, …

I know of black. But I’m too stubborn to let go of tabs :wink: and sometimes a list is nicer to be on one line, or all items one its own, and black will always reformat this.

I also hate spaces instead of tabs, but funnily since using auto formatters, I don’t care at all anymore. I still hit my tab key for indentation and just let go. Ease of mind. :love_you_gesture::melting_face::love_you_gesture:

Black only formats lists to multiple lines, if you add a trailing comma (which most of us usually do). But if you really need a one-line-list, remove the last comma.
If the list exceeds a certain length though, it will always wrap. But I find that a good style.

One thing I forgot to mention is to only import what’s needed. So not from GlyphsApp import Glyphs. Or import vanilla and then use self.w = vanilla.Window().