-
-
Notifications
You must be signed in to change notification settings - Fork 11
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow formatting with ruff instead of black #58
Comments
It preserves currently the black formatting (https://15r10nk.github.io/inline-snapshot/#code-generation point 5). I think the problem is that it does not detect that your code is formatted with "black style". Maybe your files are part of this 1%, or there are some black or ruff options which are responsible for the different formatting. Maybe this info helps you. The other option for me would be to preform the same check and formatting with ruff, which I do with black. |
|
The problem is that ruff formats differently than black in your case (double/single quotes). inline-snapshot looks at the source before it changes anything and thinks "this is not formatted with black". It re-formats only the snapshots with black (this causes the double quotes in your snapshots) and skips the reformatting of the whole file. I think you have [tool.ruff.format]
quote-style = "single" in your config. I will think about a way to handle the formatting better. Maybe a What would you prefer? |
OK, I see now how this happens: def test_example():
foo = [dict.fromkeys(range(6), 'abcdefghijkl') for _ in range(2)]
assert foo == snapshot([
{
0: "abcdefghijkl",
1: "abcdefghijkl",
2: "abcdefghijkl",
3: "abcdefghijkl",
4: "abcdefghijkl",
5: "abcdefghijkl",
},
{
0: "abcdefghijkl",
1: "abcdefghijkl",
2: "abcdefghijkl",
3: "abcdefghijkl",
4: "abcdefghijkl",
5: "abcdefghijkl",
},
]) as a result of only formatting the snapshot. I do think that could be improved so that the result is still nice for people who are not using any kind of formatter. Specifically, I think you should black/ruff-format the snapshot call node (not just the contents) and then indent that by the amount of indentation in the containin statement (4 spaces in this case).
I'm not sure I understand the difference between the two options. If both are integrated, there will probably need to be a config option to pick one. You could maybe default to using the one that seems closest to the file's current formatting. If it's a tie, I would suggest choosing ruff just because it's faster. |
Formating source ranges is not supported by black psf/black#134 (comment) I can fix the indentation and make it look acceptable if you are not using black for your whole file. But the indentation affects the formatting and this solution will probably never be perfect. Detecting if a file is formatted and preserving this formatting is easier. This is what inline-snapshot currently does with black. You can format your example with black before you fix your snapshot. Inline-snapshot will format the whole file after the fix and preserve the formatting. I think I can implement the same behavior for ruff formatted files. |
To be clear, for the case of formatting just the snapshot, I'm only talking about it for the sake of interest and because I imagine you'd like it to work better for users not using black/ruff. I'm happy with just formatting the whole file with ruff. I'm not suggesting formatting source ranges. I'm saying instead of using black to format |
BTW, it would be nice to use inline-snapshot completely without Black since it pulls in several the dependencies. Wouldn't it be reasonable to tell users that whatever code formatter they use, it should be installed for this to work? Would probably require a major bump, but then again it's "just" a test dependency so no service should go down because of that. Maybe add |
@hynek, you propose to ship inline-snapshot without a default formatter, if I understand you correct. The problem is that But it might be a good idea to provide only a |
Yeah, I personally would be fine with Black being the default, but not being auto-installed. And that I’m able to overwrite the default in |
Like @hynek I'd prefer to be in control. I use Ruff and my config file is located in a subfolder anyway, so running Ruff automatically won't find the config file, and will use different formatting options (the default ones). Having a config option to specify the command to run would be ideal: [tool.inline-snapshot]
format_command = "ruff format --config=config/ruff.toml {}" ...where Though maybe you're currently running Black from Python, not with a subprocess, so this might complicate things. Not critical to me, I'm even happy to just run Ruff myself after tests were changed 🙂 |
I like the idea. This gives the user the maximal control about the formatting. |
still time until xmaaaassss! 🎁🎄🎁🎅😇 |
Every time I fix snapshots, I immediately have to press a few more keys to reformat with ruff. It'd be lovely if it was already formatted 'correctly'.
The text was updated successfully, but these errors were encountered: