Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding prettier formatting #660

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cjntaylor
Copy link
Contributor

@cjntaylor cjntaylor commented Dec 28, 2023

Lets handle the yaml formatting changes properly 馃槈

Adding prettier for consistent formatting of non-python files. Ruff format only applies to python files, however, other files in the repo still have similar concerns (consistency, whitespace thrashing, etc). Prettier addresses this concern in the same way as ruff; minimal configurability to prevent bikeshedding.

Prettier is written in javascript but is a generalized tool, handling several other source types (markdown, yaml, json, etc). While a tool in the python ecosystem would be preferrable, AFAIK no such tool exists. This appears to be why pre-commit itself directly hosts a prettier integration.

@cjntaylor cjntaylor changed the title Pre commit prettier Adding prettier formatting Dec 28, 2023
@cjntaylor
Copy link
Contributor Author

I'll leave this up for posterity, on the off-chance you're interested. It's water under the bridge to me. I've stripped the unnecessary mypy changes so it merges cleanly from master.

Feel free to close

@agronholm
Copy link
Owner

Doesn't prettier support reading its configuration from pyproject.toml?

@cjntaylor
Copy link
Contributor Author

Doesn't prettier support reading its configuration from pyproject.toml?

I feel like I've seen this before, but I can't find any reference to it. Have you come across something; I would also much prefer that (I'm a big fan of single config file).

@agronholm
Copy link
Owner

No, I haven't actually come across prettier itself before this.

@cjntaylor
Copy link
Contributor Author

Ah, no worries.

Unfortunately this is the only tool I've found that works consistently for these other file formats, but it comes from the javascript/node world, so it's not particularly designed with python in mind. From my basic searching, I haven't yet found anything that would put the config internal to pyproject.toml; it supports a number of different files but not pyproject: https://prettier.io/docs/en/configuration.html

I've always used the TOML file variants when working with python, since the language standardized it's main config format on TOML (at least as far as I'm concerned now that PEP621 is final). Don't merge for now - I'll see if there's a way to accomplish this without too much effort.

@cjntaylor cjntaylor marked this pull request as draft December 28, 2023 23:27
@cjntaylor
Copy link
Contributor Author

I looked into it more. It's possible to point prettier at pyproject.toml with a "--config" argument, but it spews warnings about unrecognized keys. The config settings would also have to be top level (not in a tool section), so I don't think it's a good solution.

It is possible to pass the few configuration parameters as arguments themselves instead of using a config. However, this only works in the context of pre-commit and would be inconsistent with any other tool that also uses prettier. I gave this an attempt and the results were also not stellar - you just end up with different configuration files.

So for now, I think these additional config files are the best general solution that seems to work everywhere, including within pre-commit. It has given me an idea to see about fixing this more generically for python, including pyproject.toml support - but I wouldn't wait if you think this is useful (that'll take a bit)

@cjntaylor cjntaylor marked this pull request as ready for review December 29, 2023 00:30
Adding prettier for consistent formatting of non-python files. Ruff format only applies to python files, however, other files in the repo still have similar concerns (consistency, whitespace thrashing, etc). Prettier addresses this concern in the same way as ruff; minimal configurability to prevent bikeshedding.

All non-conformant files have been formatted to pass the pre-commit hook. Very few changes were needed; several yaml files were modified to have consistent whitespace and indentation, including the pre-commit configuration file.

Modern text editors and IDEs (efficiently) support prettier via plugin; this is recommended for local formatting. A cli tool is available, however, prettier itself is written in javascript so it is only accessible via npm/yarn (`npx prettier`/`yarn dlx prettier`). While this is unfortunate, there are very few tools that format general source files, and none within the python ecosystem.

Pre-commit does address this capability gap indirectly. It can be run via `pre-commit run prettier`, either with the `--all-files` flag or by adding a modified file to your working tree. While suboptimal, this does allow local formatting/validation if needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants