Skip to content

Add ignore-regex option to comments rule#720

Open
peschmae wants to merge 1 commit intoadrienverge:masterfrom
peschmae:feat/comment-ignore-regex
Open

Add ignore-regex option to comments rule#720
peschmae wants to merge 1 commit intoadrienverge:masterfrom
peschmae:feat/comment-ignore-regex

Conversation

@peschmae
Copy link

Fixes #545

This adds a ignore-regex flag to the comments rule.

If a comment matches the regex, require-starting-space as well as min-spaces-from-content are skipped.

The regex is only matched against the comment itself, and not against the full line.

@peschmae peschmae force-pushed the feat/comment-ignore-regex branch from cb94c17 to fd1f8ca Compare March 11, 2025 15:44
@coveralls
Copy link

Coverage Status

coverage: 99.826% (+0.001%) from 99.825%
when pulling fd1f8ca on peschmae:feat/comment-ignore-regex
into d5b6c4a on adrienverge:master.

Copy link
Owner

@adrienverge adrienverge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @peschmae, thanks for contributing!

This looks good, but I have a few requests.

  1. First, I disagree with myself of 2021 when I proposed ignore-regex as the option name. Indeed, other rules don't use "regex" in their option names when their content should be a regex. Similarly, rules don't tell "option-…-string" when their value should be a string.

    Instead, I propose ignore-comments. This will allow adding ignore-lines in the future, if one day we want to silent errors based on the preceding YAML line contents, in case of inline comment:

    part of the line that should match the regex  # comment
  2. Can you reword Fixes #545 to Partially fixes #545 ? Because the original request is about silenting both the comments and line-length rules, so in my opinion the issue #545 should remain open.

  3. Finally, write a good commit message (basically: the commit title should be comments: … and the message could be what you wrote in the first PR message).

Comment on lines +111 to +115
# If the comment matched the ignore-regex, we don't perform any checks
if (len(conf['ignore-regex']) > 0 and
any(re.search(r, str(comment))
for r in conf['ignore-regex'])):
return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • You can remove the comment: the code is self-explainatory.
  • if len(conf['ignore-regex']) > 0 is not needed, because any([]) is false.
  • Can you please add an empty line afterwards, like 7 lines below ↓, for logical separation?
Suggested change
# If the comment matched the ignore-regex, we don't perform any checks
if (len(conf['ignore-regex']) > 0 and
any(re.search(r, str(comment))
for r in conf['ignore-regex'])):
return
if any(re.search(r, str(comment)) for r in conf['ignore-regex']):

Comment on lines +29 to +31
* ``ignore-regex`` is used to exclude specific comments from this check.
Only the comment itself will be matched to the regex, not the whole line on
inline comments.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • There are multiple syntax for regexes, here it's PCRE regexes. We mention it in key-ordering and quoted-strings documentations, so let's do it here too 👍
  • I have a proposal that I find slightly clearer about inline comments:
Suggested change
* ``ignore-regex`` is used to exclude specific comments from this check.
Only the comment itself will be matched to the regex, not the whole line on
inline comments.
* ``ignore-comments`` is a list of PCRE regexes to exclude specific comments
from this check. For inline comments, only the comment itself will be matched
to the regex, not the whole line.

What do you think?

Comment on lines +78 to +90
#. With ``comments: {ignore-regex: [^#cloud-config]}``

the following code snippet would **PASS**:
::

#cloud-config

#. With ``comments: {ignore-regex: [^#noqa]}``

the following code snippet would **PASS**:
::

enabled: true #noqa
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to:

  • merge both examples to ease the reader understanding (both cases pass),
  • give an example where the regex matches a part of the comment.

This would yield:

Suggested change
#. With ``comments: {ignore-regex: [^#cloud-config]}``
the following code snippet would **PASS**:
::
#cloud-config
#. With ``comments: {ignore-regex: [^#noqa]}``
the following code snippet would **PASS**:
::
enabled: true #noqa
#. With ``comments: {ignore-regex: [^#cloud-config, ^#noqa]}``
the following code snippet would **PASS**:
::
#cloud-config
import lib #noqa: E402

(This doesn't make sense to fix a Cloud-init file with Python code, but at least it's short and readable.)

Comment on lines +248 to +249
'#but not this\n', conf,
problem1=(3, 2))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check close-but-not-exact matches:

Suggested change
'#but not this\n', conf,
problem1=(3, 2))
'#but not this\n'
'# ignore this\n', conf,
problem1=(3, 2),
problem2=(4, 2))

@binaryDiv
Copy link

This would be a very helpful feature! Is there any news on it?

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.

Feature: support config rule comments.ignore-regex

4 participants