Add noqa skips in otherwise empty lines to the next non-empty line #4567
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #3935
So what it does:
traverse_yaml
to flatten lists within there (these are required for a ruamel CommentMap, please don't ask any further, afaik these are entirely undocumented)#noqa
and inserting them into thelintable
, postprocessing is done on thelintable
.# noqa
is the start of the line, minus some whitespace/indentation.# noqa
on that line, the skip is added to the next non-empty line.This is something required for me in order to suppress errors on yaml multiline strings, as reported by the issue referenced.
In general I think the solution is quite hacky, so this can also be seen as a proof of concept. The whole flattening thing and how ruamel works with comments is quite weird. As in the context of
_get_rule_skips_from_yaml
only a map of{lineno: rule_to_skip}
is what we're interested in, it might be easier to just go over the content of a file, check each line for_noqa_comment_re
match. The whole tree structure ofCommentMap
is annoying to traverse, a simple iterator over all comments would do it (don't know if that's inpyyaml
?) and would be more accurate for comments not attached to objects in a line.