cookiecutter-hypermodern-python
cookiecutter-hypermodern-python copied to clipboard
Add .editorconfig
https://editorconfig.org/
Adam Johnson mentioned a good .editorconfig
file in a tweet.
Thanks!
For reference,
# https://editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
[*.py]
indent_size = 4
Rationale in his blog post at https://adamj.eu/tech/2022/01/14/set-up-editorconfig-for-your-django-project/.
Should we cater to any of these special cases?
- There are other file extensions with Python(-like) code, most notably *.pyi (type stubs), *.pyw (scripts on Windows without an attached console window), and *.pyx (Cython).
- reST files commonly use 3 spaces for indentation.
- Makefiles are tab-indented.
- Some Markdown processors interpret two trailing spaces as a linebreak.
Are there more?
FWIW, here's a version implementing 1 to 3 from above:
# https://editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
[*.rst]
indent_size = 3
[*.{py,pyi,pyw,pyx}]
indent_size = 4
[Makefile]
indent_style = tab
Good point on those other sort-of Python files.
As the complexity of the .editorconfig
files grows, generated projects might want...tooling! pre-commit
has a hook for checking .editorconfig
files.
I don't think EditorConfig is urgent, but when it goes in, it kind of makes sense to include the tooling-tooling.
I'd love to see a .editorconfig
file in this repo. I've used this cookiecutter to make a couple Python packages recently and adding a .editorconfig
file is one of the first additions I always make.
FYI that pre-commit hook checks that @pauleveritt linked have some issues with indentation that I discovered recently. They don't attempt to tell the difference between identation and alignment so they raise errors when the alignment doesn't match up with the indentation.
For example an rst file with this bullet point would raise an error (unless the indentation happened to be set to 2
for rst
):
- This is a bullet point which has a lot of text and then
wraps onto a second line, which is a fairly common practice.
I'm in the planning stages on a separate EditorConfig validator that only validates indentation in Python files (but validates all other settings in the other files).
Oh and personally I'd leave out Makefile
from the .editorconfig
unless the project will have a makefile.
If you're afraid of the blanket "use spaces for all", you could break down indentation per file extension. Likewise with trim_trailing_whitespace
(if you're concerned about markdown files with trailing whitespace).
I would also add toml
, since there's a pyproject.toml
file which could be edited manually:
# https://editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
[*.{py,pyi,pyw,pyx,toml}]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
[*.rst]
indent_style = space
indent_size = 3
trim_trailing_whitespace = true
Thanks for chiming in @treyhunner !
I like the idea of configuring indentation only for the file extensions we actually use. With MyST in place, that should boil down to *.{py,toml}
(four spaces) and *.{yml,yaml,json}
(two spaces).
As for the hard line breaks in Markdown: Those can be realized using the alternative backslash syntax, and our pre-commit hooks remove them already. So I'd tend to make this a global setting. (I'll admit it, I'm not a fan of significant trailing whitespace...)