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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misses comments within multi-line strings and comments #26

Closed
PeterJCLaw opened this issue Jun 20, 2023 · 3 comments
Closed

Misses comments within multi-line strings and comments #26

PeterJCLaw opened this issue Jun 20, 2023 · 3 comments

Comments

@PeterJCLaw
Copy link

PeterJCLaw commented Jun 20, 2023

Flake8 will happily complain about e.g: these long lines:

# Culpa est debitis quae et qui rerum. Deserunt unde et id ut voluptatum labore ut voluptate.
x = """
   Culpa est debitis quae et qui rerum. Deserunt unde et id ut voluptatum labore ut voluptate.
"""

and adding a noqa marker to it will silence the error:

# Culpa est debitis quae et qui rerum. Deserunt unde et id ut voluptatum labore ut voluptate.  # noqa
x = """
   Culpa est debitis quae et qui rerum. Deserunt unde et id ut voluptatum labore ut voluptate.  # noqa
"""

yet flake8-noqa (with flake8 demo.py --noqa-require-code) won't complain about the noqa comments not having codes.

The inverse is also true -- noqa comments in comments and multi-line strings which are unnecessary are also overlooked.

My use-case was in tests for a flake8 plugin, where I've got code snippets in multi-line strings. In this case, having noqa comments within the strings does actually make sense, though I realise that may not always be the case.

@PeterJCLaw
Copy link
Author

It's not clear to me what that right solution is here. Obviously having such comments within multi-line strings is somewhat unlikely (and potentially undesirable) and users may not want (or be able) to modify their strings to add the relevant codes.

Given that and my unusual use case (code within the strings) this may be something that's better just documented as a limitation/known behaviour rather than actually changing?

@plinss
Copy link
Owner

plinss commented Jun 20, 2023

A comment inside a multiline string isn't really as a comment, it's just part of the string. I'm somewhat surprised flake8 respects the # noqa inside the string, and I consider that a bug in flake8.

You can put a # noqa comment after a multiline string and both flake8 and flake8-noqa treat it as covering the entire string.

e.g.

x = """
   Culpa est debitis quae et qui rerum. Deserunt unde et id ut voluptatum labore ut voluptate.
"""  # noqa

works as expected.

@PeterJCLaw
Copy link
Author

Huh, thanks for the tip about putting the comment after the string -- that didn't occur to me.

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

No branches or pull requests

2 participants