Giter Site home page Giter Site logo

plinss / flake8-noqa Goto Github PK

View Code? Open in Web Editor NEW
38.0 5.0 6.0 63 KB

flake8 plugin to validate #noqa comments - Mirror of https://gitlab.linss.com/open-source/flake8/flake8-noqa

License: GNU Lesser General Public License v3.0

Python 100.00%
flake8 flake8-extension flake8-plugin

flake8-noqa's People

Contributors

frenzymadness avatar maresb avatar plinss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

flake8-noqa's Issues

Sometimes noqa is not on the Exact Same Line

Hello!

I have run into a tricky problem, where flake checks relating to docstrings and docstring formatting are raised inside multiline comments.

My environment approximates the packages below. I am using other libraries to make my problem easier to spot, but it does not strictly depend on them.

darglint                      ==1.8.1 # (DAR)
flake8                        ==4.0.1
flake8-noqa                   ==1.2.3 # (NQA)
flake8-docstrings             ==1.6.0 # (D)

example.py

"""Hello There."""

import asyncio


def foo(spam: str, task: asyncio.Task):
    """Does a thing.                   <-- Note that this is line 7.

    If task had an exception, raise it here.

    :param spam: String to print.
    :param task: Exception to raise.
    """  # noqa: D401, DAR401          <-- Note noqa line here, following docstring.
    print(spam)
    e = task.exception()
    raise e

As written, I get the output:

(venv) $ flake8 --docstring-style=sphinx --max-line-length=100 example.py 
example.py:13:10: NQA102 "# noqa: D401, DAR401          " has no matching violations

With the noqa in place, Flake8 is able to suppress D401 and DAR401 (coincidental codes) that relate to exceptions and docstring formatting. The lint errors are "produced" on line 7, you can see this if you remove the noqa. Flake must be performing some logic to detect the noqa at the end of a multiline comment, since it would be bad-form to put the noqa literally inside the docstring.

Would you be able to perform the same check so that I can keep my noqa after the docstring?

Superfluous and incorrect `NQA102` errors

Hi! First of all, thanks for this great plugin. I tried setting it up right away, but am running into some issues. Specifically, it shows me NQA102 (flake8-noqa) "# noqa: B009" has no matching violations errors for files that doesn't even have any noqa statements inside.

This is what flake8 gives me:

[python] ../.gradle/python/bin/python -m flake8 .
         ./workspace/test_views.py:24:55: NQA102 (flake8-noqa) "# noqa: B010" has no matching violations
         ./backend/scheduler.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/urls.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/tools/contexthelper.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/decorators/test_decorators.py:13:52: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/decorators/test_decorators.py:23:56: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/decorators/test_decorators.py:26:64: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/decorators/__init__.py:13:52: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/decorators/__init__.py:23:56: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/decorators/__init__.py:26:64: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/bigquery/replicators/__init__.py:24:55: NQA102 (flake8-noqa) "# noqa: B010" has no matching violations
         ./backend/components/bigquery/replicators/SQLReplicator.py:24:55: NQA102 (flake8-noqa) "# noqa: B010" has no matching violations
         ./backend/components/bigquery/replicators/IncrementalReplicator.py:24:55: NQA102 (flake8-noqa) "# noqa: B010" has no matching violations
         ./backend/components/mysql/backend.py:24:55: NQA102 (flake8-noqa) "# noqa: B010" has no matching violations
         ./backend/components/mysql/jdbcwrapper.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/mysql/__init__.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/mysql/replicators/FullReplicator.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/mysql/replicators/__init__.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/pythonblock.py:13:52: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/pythonblock.py:23:56: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/pythonblock.py:26:64: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/pythonblock.py:169:58: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/pythonblock.py:170:58: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/util.py:13:52: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/util.py:23:56: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/util.py:26:64: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/util.py:169:58: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/util.py:170:58: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/constants.py:13:52: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/constants.py:23:56: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/constants.py:26:64: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/constants.py:169:58: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/constants.py:170:58: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/__init__.py:13:52: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/__init__.py:23:56: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/__init__.py:26:64: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/__init__.py:169:58: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tools/__init__.py:170:58: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./backend/components/notebook/tests/test_python_block.py:81:18: NQA102 (flake8-noqa) "# noqa: PT001" has no matching violations
         ./backend/components/notebook/tests/__init__.py:81:18: NQA102 (flake8-noqa) "# noqa: PT001" has no matching violations
         ./backend/exception/__init__.py:81:18: NQA102 (flake8-noqa) "# noqa: PT001" has no matching violations
         ./layer/gql/__init__.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./layer/gql/types.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./layer/gql/schema.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations
         ./layer/gql/test/client.py:151:59: NQA102 (flake8-noqa) "# noqa: B009" has no matching violations

But here are all files with B009:

platform/backend/components/notebook/pythonblock.py:        kernel["load"] = getattr(storage_proxy, "load")  # noqa: B009
platform/backend/components/notebook/pythonblock.py:        kernel["save"] = getattr(storage_proxy, "save")  # noqa: B009
platform/backend/decorators/test_decorators.py:        self.assertTrue(getattr(func, "is_rest"))  # noqa: B009
platform/backend/decorators/test_decorators.py:        self.assertTrue(getattr(func, "is_exported"))  # noqa: B009
platform/backend/decorators/test_decorators.py:            parameter_label, getattr(func, "parameter_label")  # noqa: B009
platform/backend/scheduler.py:        backend_build_method = getattr(backend, "build")  # noqa: B009

Any ideas?

LICENSE vs. COPYING.LESSER inconsistency

The 1.3.0 sdist at PyPI shows this as the License-File:

$ grep License-File PKG-INFO 
License-File: LICENSE
$

While at the same time it claims the project license is LGPL v3:

$ grep License: PKG-INFO 
License: GNU Lesser General Public License v3
$

But the LICENSE file contains GPL v3, not LGPL v3:

$ head -n2 LICENSE 
                    GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007
$

The LGPL v3 is actually in the COPYING.LESSER file:

$ head -n2 COPYING.LESSER 
                   GNU LESSER GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007
$

Incorrect NQA102 error

I have a library file that I am checking with vulture. For a function that I define in there, I get the following error from vulture:

my-lib.py:12: unused function 'method-name' (60% confidence)

I can silence this with noqa VT103. But then flake8-noqa will complain:

NQA102 "# noqa: VT103" has no matching violations

Can't detect useless noqa in some cases

Hi!

Thanks for your flake8 extension, already saved me quite some time!

I've met an issue though: it fails to detect that the "# noqa: E501" is not necessary in some cases:

# OK, detects E501 is useless: line + comment <= 79
my_array1 = ['lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng string'],  # noqa: E501

# Fails to detect E501 is useless: line + comment > 79
my_array2 = ['lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng string'],  # noqa: E501
my_array2 = ['lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng string'],  # noqa: E501

# Fails to detect E501 is useless: the closing square bracket
my_array3 = [
    ['lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng string'],  # noqa: E501
]  # noqa: E501

My flake8 command-line:

flake8 test_file.py

Support flake8==7.0.0

flake8 7 is out so pyproject.toml should probably change from
'flake8>=3.8.0,<7.0',
to
'flake8>=3.8.0,<8.0',

as the breaking changes in flake8==7.0.0 are pretty minor.

I would consider removing the upper bound as well. Flake8 breaking changes are often pretty minor and often don't break anything to do with extensions. And each time there is a new flake8 version, it takes weeks for all the extensions to update their upper bounds for flake8.

https://iscinumpy.dev/post/bound-version-constraints/

Issue with picking up noqa issues after other comment

Some weird behaviour when both flake8-noqa and flake8-bandit are installed, with a few of the flake8-noqa codes not being raised:

  • If I have a blanket noqa on a line with no violations NQA101 does not get flagged.
  • If I have a noqa with a made up random code (i.e., # noqa: ABC123) then NQA102 does get flagged.
  • But if I have a noqa with a genuine code (i.e., # noqa: S101 or # noqa: PD001) then NQA102 does not get flagged.

Obviously I have no idea if the issue lies within flake8-bandit or flake8-noqa, but there does seem to be some compatibility issue there.

Running flake8==3.9.2
I am running flake-noqa==1.2.3 (though issue still exists with 1.2.2, 1.2.0, 1.1.0, 1.0.0)
I am running flake8-bandit==3.0.0 (problem doesn't seem to happen with 2.0.0 and does with 2.0.1)
The fact the problem seems to appear with flake8-bandit 2.0.1 would lead me to believe the issue is with flake8-bandit, but the only change in that project from 2.0.0 -> 2.0.1 was some setup.py changes which I wouldn't have thought would cause any issues like this: tylerwince/flake8-bandit@v2.0.0...v2.0.1

I am also raising an issue in flake8-bandit about this given that I'm not sure which project is actually causing the problem

multiline string with trailing comma causes false positive

Having a multiline string as a positional argument inside a call with a trailing comma causes a false positive

call(
    '''\
    This super, long, long, long, long, long, long, long line is a false positive, the noqa does something!
    ''',  # noqa: E501
)

causes:

flake8 tt.py
tt.py:1:1: F821 undefined name 'call'
tt.py:4:11: NQA102 "# noqa: E501" has no matching violations

however when removing the noqa:

call(
    '''\
    This super, long, long, long, long, long, long, lon line is a false positive, the noqa does something
    ''',
)

it yields:

flake8 tt.py
tt.py:1:1: F821 undefined name 'call'
tt.py:3:80: E501 line too long (105 > 79 characters)

removing the trailing comma fixes the false positive.

Misses comments within multi-line strings and comments

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.

Allow ignoring T000 (TODO marker)

Given the following code:

# TODO: make this better # noqa: T000

When running flake8 with flake8-noqa and flake8-todo both installed the user is stuck having either the T000 warning about the TODO or the NQA102. I'm guessing this is related to the behaviour previously partially discussed in #26, though slightly different.

I've tried putting the noqa comment both before and after the TODO comment, neither of those works.

Is there a way to ignore `NQA` errors?

I run vulture to test my code. An error I get from vulture is VT103:

my-lib.py:12: unused function 'method-name' (60% confidence)

I can silence this with noqa VT103. But then flake8-noqa will complain:

NQA102 "# noqa: VT103" has no matching violations

If I try to silence NQA:

# noqa VT103,NQA102

then I get an error:

NQA102 "# noqa: VT103,NQA103" has no matching violations

Is there a way to instruct flake8-noqa to ignore a line?

flake8-noqa slow with VSCode

Hello,

I just started using flake8-noqa. I use VSCode and it's Flake8 extension, that points to my .flake8 config file.

I noted that even if I fix the issues pointed by the plugin, the editor is not updated, like the code is not scanned again and I need to close and reopen VSCode to see the errors disappear.

Could this issue related to the plugin?

Thank you!

Feature request: add option to warn about ignored codes

If a code is ignored by configuration, plugin should report as unnecessary noqa code (when option is set)

Option should be able to always require ignored codes, always reject ignored codes, or not care one way or another

Idea: "allowed_noqa" Config Option

Hello!

I think a great addition to this library would be an allowed_noqa config option (name fungible ๐Ÿ˜†), which allows a project lead to specify a global set of noqa comments that are allowed. This gives a dedicated place for notes and comments to developers regarding how those specific whitelisted noqa items can be used.

config.ini

allowed_noqa =
    # Relative Imports. Never for top-of-file imports in normal code. Only where relative imports
    # makes natural sense.
    ABS101
    # Missing a :raises: section. Addresses limitation by darglint. Never to suppress legitimate
    # lack of documentation. Only for `raise`-ing a variable.
    DAR401
    # Function name not lowercase. Never to purposefully violate naming standards. Only
    # for when overriding existing method names from library code.
    N802

All other noqa comments would be flagged by this plugin. I think this is useful because code review does not capture everything, and having a central location that explains the purpose of each noqa could be helpful.

If I'm missing a Flake feature that already does something like that- my bad and please let me know!

Gets confused with pydocstyle (Dxxx) violations

If a line has a Dxxx violation and you add a # noqa: Dxxx flake-noqa starts complaining that Dxxx isn't matched on that line.

This is due to the fact that pydocstyle parses the # noqa comment itself and never sends the violation report to flake8, so flake8-noqa stops seeing it.

Issue filed on pydocstyle to add a --disable-noqa equivalent, and will be followed by an issue on flake8-docstyle to invoke the option. Note the same issue also affects flake8 --disable-noqa, it still won't show violations reported by pydocstyle and blocked by # noqa comments regardless if this plugin is used or not.

Should the issues on pydocstyle and flake8-docstyle be rejected, will look for some kind of less-than-ideal workaround.

Feature/rule request: require reason/description

For example: # noqa: T201 is not allowed, but # noqa: T201: Print found; Printing information about logging destination is.
This is useful for people reading the code, so they don't have to look up what the code means.
This may be too disruptive to be enabled by default, so it can be added to extend_default_ignore (see flake8-bugbear)

Suggestion: deduplicate codes in example

For example:

from foo import bar, baz, quox  # noqa
print(quox)

results in an error like:

demo.py:1: NQA104 "# noqa" must have codes, e.g. "# noqa: F401, F401"

with F401 repeated for each occurrence.
However in practise only a single occurrence is needed to ignore the error.

Support flake8==5

Currently setup.py is limiting flake8 < 5
I would like to move to flakeu8==5.0.2.
Does anything (other than setup.py) need to change for this?

False positives when running with `--stdin-display-name`

Given the following file (blah.py):

print(
    'Lorem ipsum lorem ipsum Lorem ipsum lorem ipsum Lorem ipsum lorem ipsum Lorem ipsum lorem ipsum '  # noqa: E501
)

And the following .flake8:

[flake8]
max-line-length = 80
extend-select = NQA1
noqa-require-code = True

The following invocations result in two different outcomes:

$ flake8 blah.py


$ cat blah.py | flake8 --stdin-display-name blah.py -
blah.py:2:105: NQA102 "# noqa: E501" has no matching violations

Issue with noqa comment & commented out code w/ flake8-eradicate

Hello. I'm using flake8, flake8-noqa & flake8-eradicate which finds commented out code and gives "E800 | Found commented out code". Until flake8-noqa==1.2.5 it worked fine, but now I encounter weird behaviour.

Python version

$ python --version
Python 3.9.10

Dependencies:
flake8==4.0.1
flake8-eradicate==1.2.1
flake8-noqa==1.2.5

Current behaviour

code.py

# a = 5
a = 7

flake8 run

$ flake8 code.py
code.py:1:1: E800 Found commented out code

code.py

# a = 5  # noqa: E800
a = 7

flake8 run

$ flake8 code.py
code.py:1:1: NQA102 "# noqa: E800" has no matching violations

Expected behaviour

code.py

# a = 5
a = 7

flake8 run

$ flake8 code.py
code.py:1:1: E800 Found commented out code

code.py

# a = 5  # noqa: E800
a = 7

flake8 run -- no error

$ flake8 code.py

Can you please look into that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.