-
Notifications
You must be signed in to change notification settings - Fork 29
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
snakefmt comments parsing has unintended side-effects #85
Comments
Ok, I'll deactivate snakefmt on snakemake-wrappers again for now. |
Ok, so what you're saying is you would like
to be left as is and not turned into
This is also brings up something that has been low-key bugging me for a while. PEP8 specifies that inline comments should be separated from the statement by at least two spaces. For example, both
(i.e. the extra space between the statement and the comment). But
This also makes me realise @dlaehnemann that I am having a little difficulty recreating your issue. Could you please provide a concrete example we can use for testing purposes showing actual and expected results for a small snakefile? |
Here we go. Correct:
snakefmt:
as can be seen, the single line comments that should refer to the lines below are moved to the lines above, which changes their semantics. |
Yeah, sorry for the unspecific dump, yesterday. I had spent hours fixing up that pull request, so simply wanted to note the issue without spending more time on it. So I am also sorry for the brisk tone of that issue... @johanneskoester provided the most important recurring issue with the comment formatting, thanks for this more constructive approach! |
For this one, I am actually not 100% sure if this is feasible with a linter, as it goes against the PEP you mentioned above. But I nevertheless want to document it here: Wanted layout:
current snakefmt
|
Here is an example, where current snakefmt even breaks the rule syntax (by commenting out the wanted layout
current snakefmt
If reformatting this would be wanted, an alternative formatting could be:
|
Here, snakefmt thinks that the comments with a completely different indentation level do not belong to the indentation level they are at (and the resources spec that follows), but to the preceding inline comment. wanted format
current snakefmt
|
Similar to the previous example, but with commented out extra params, which is a good way to showcase possible further extra params in an example rule for a Snakemake wrapper. Here, snakefmt adds an extra whitespace that does not make sense for the use-case. wanted format
current snakefmt
|
@mbhall88 I think this list now gives a representative example for all the comment use cases I had to fix manually after running snakefmt on the |
Thank you for the enormous detective work @dlaehnemann and for the example @johanneskoester . We can now work with these concrete examples. we had clearly not given comment treatment a hard enough look, let's work on that now! |
Good to know, that these unexpected three hours yesterday were useful after all! :D And thanks for providing this, this will be incredibly useful for the snakemake wrappers and snakemake workflows projects, catching lots of unwanted stuff automatically in pull requests! So time invested here is definitely worth it in the long run! |
Thanks so much for this!! I'm amazed we've gotten this far without realising this. I guess the best approach is to leave comments as they are - unless they have less than 2-space from inline statement, in which case they will just have spacing added. |
Yeah, to start with, that is probably the easiest and cleanest solution. Should we at some point have any strong opinions about how comments should be formatted, we should use the above test cases to check that it keeps (most of) them as intended. But any such solution will probably have to reason about comment indentations (as in the |
I think we should just try and stick to what PEP8/ |
Coming back to this, I don't think it is trivial to leave the comment alignment. In fact for this situation
becomes
I would argue we should probably stick with that |
Jup, that's what I figured and why I added the caveat when posting this. It's a shame, because the alignment does make it a lot more readable, but for the formatter to see this and then keep it that way does sound to complicated to implement quickly. So I agree that this is not worth investing lots of energy. Maybe one workaround for such examples would be to include black's comment-based tag that turns off formatting for a certain region ( We will just have to remember to include this in the respective snakemake-wrappers before applying snakefmt again. Let me know once you're done with updating the comments handling, and I'd be glad to work on a new snakemake-wrappers PR to implement snakefmt over there, and will try to check up on all my lessons learned from last time around... :D |
Adding I'm working on all the other failing test cases you have introduced, will let you know when those are done :) its so helpful to have you testing this out on all these real-world files. |
Done, see issue #86 .
Sounds like a fun game of whack-a-mole. Enjoy, I guess? |
* Add PEP8 2-space inline comment spacing, with unit test * Remove leading space in multiple comments in parameters (snakemake#85) * Add 4 failing test cases taken from snakemake#85
I'd like to propose an alternative solution: Given:
Produce:
Given the inline comment documents the keyword (eg here Would you agree @dlaehnemann ? |
I definitely agree, that comment merging is difficult and thus I expect it to be error-prone. But moving each comment up one line could be similarly problematic. As you are pointing out, An alternative could be to clearly define an intended Snakemake commenting style and document this everywhere (docs of Snakemake, snakemake-wrappers, snakemake-workflows, etc.). We would then have to go through existing projects once, to implement this style there. But even then, I think having the formatter change comment layout (beyond what black would do) is probably going to be problematic. |
Could you expand on what you mean by this @dlaehnemann ? Perhaps @johanneskoester has a guiding opinion? |
Also I'm not sure there exists a valid Python statement where you have statement : #comment
statement #comment (Possibly because Python statements like |
Yeah, that was not really clear. Here's an example:
I also came up with much more extreme case, but I think we won't be able to define a universally useful behaviour for all the comment types out there. But I double-checked on the general python PEP 8 on comments, and maybe we could simply follow the very basic guidance there: This would mean that:
And then the formatter could either emit warnings, if it finds funny stuff that doesn't seem to conform to this, or changes this according to the above assumptions. So e.g.:
This would throw |
I see what you mean. For the example on the For your second example I agree issuing a warning would also make sense- i wouldn't be keen on changing the comment indent though. given how the parser is set up its a lot of work to output all the information (eg it is expected to describe |
So for a keyword like My second example, with the weird indentation, is actually a safe bet to adjust the indentation, based on PEP 8. Maybe also issue a warning that this was done. But no need to include too much info beyond the comment itself. If a line number can be issued, that's helpful, but otherwise the text of the comment itself should do -- this at least enables a quick search by the user. One change I would actually suggest, is to only adjust the spacing of inline comments, if they are below the two whitespaces distance from the code. This way, aligned inline comments across multiple lines would stay untouched and the formatting would nevertheless conform to PEP 8. Also, this would remove the need of introducing the |
I would only place it above the keyword if it occurs before the value: so
The inline comment stays, but
The comment goes above, with a warning
I would prefer issuing a warning only; PEP says Before
Again I would prefer to stick to what Sorry for being pedantic. Also at some level this comes down to opinions.. |
Branch @dlaehnemann would you like to run tip of dev on the snakemake-wrappers files and check if things look good now? If that's too traumatic given the issues that came up last time, I can give it a go, though you might spot more clearly what's improved since last time. Amyway, I think progress has definitely been made! |
Will do, I'll try it out ASAP. And simply report back here, if anything fishy still turns up, instead of fixing for hours... ;) |
* Add PEP8 2-space inline comment spacing, with unit test * Remove leading space in multiple comments in parameters (snakemake#85) * Add 4 failing test cases taken from snakemake#85
Hey @dlaehnemann any news on this? I've run it through a few of the wrappers and was broadly satisfied. I could push out a new release and rework any incoming issues as they come later on? |
Sorry @bricoletc , this got lost on the todo list. But I should find time at the end of the week. If you're generally satisfied with the results on some wrappers, a release would be nice -- this way we could switch snakefmt on for the whole snakemake wrappers repo immediately, if changing the formatting works as we want it. Otherwise, we'll just have to iterate to another release. So a release would be welcome! |
No problem! OK, release 0.2.5 is made 🚀 |
I just tried to get
snakefmt --check
to work for snakemake-wrappers: snakemake/snakemake-wrappers#270However, the wrappers sport a large variety of comments in the Snakefiles, with different arrangements that each make sense for different settings.
snakefmt
rearranges them all in a standardized way, but in the process messes up their arrangement, oftentimes severing the clear association between some rule keyword and the comment. Here's the commit with which I tried to revert and fix those wrong adjustments -- it provides a good sample of how people may want to use comments in Snakefiles:snakemake/snakemake-wrappers@c82e430
The text was updated successfully, but these errors were encountered: