Add support for ignoring some warnings

Bug #907742 reported by Adi Roiban
18
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Pyflakes
Won't Fix
Wishlist
Unassigned
pocket-lint
Fix Released
Low
Adi Roiban

Bug Description

It would be nice if pyflakes would provide an option for ignoring some warnings.

For example I have this code:

try:
    from setproctitle import setproctitle
except ImportError:
    setproctitle = lambda t: None # pyflakes:ignore

And pyflakes will complain that setproctitle is redefined.
Maybe this is a bad code and this kind of things should be never ignored, but I just want to know if such a feature is wanted in pyflakes.

Here is a workaround for implementing pyflakes:ignore

class PocketLintPyFlakesChecker(PyFlakesChecker):
    '''PocketLint checker for pyflakes.

    This is here to work around some of the pyflakes problems.

    Beside the AST tree, it is also initialized with the plain text content
    of the file.
    '''

    def __init__(self, tree, filename='(none)', text=None):
        self.text = text
        if self.text:
            self.text = self.text.split('\n')
        super(PocketLintPyFlakesChecker, self).__init__(
            tree=tree, filename=filename)

    def report(self, messageClass, *args, **kwargs):
        text_lineno = args[0] - 1
        if self.text[text_lineno].find('pyflakes:ignore') >= 0:
            return
        self.messages.append(messageClass(self.filename, *args, **kwargs))

Related branches

Revision history for this message
Jean-Paul Calderone (exarkun) wrote :

I'd rather fix the bugs in pyflakes that lead to spurious warnings.

Curtis Hovey (sinzui)
Changed in pocket-lint:
status: New → Triaged
importance: Undecided → Low
Revision history for this message
Buck Evan (bukzor) wrote :

We have some questionable-but-necessary code which uses globals().update(). The of course causes pyflakes to freak out about undefined variables. I'd like to be able to ignore those warnings, instead of ignoring pyflakes altogether.

Curtis Hovey (sinzui)
Changed in pocket-lint:
milestone: none → 0.5.31
Curtis Hovey (sinzui)
Changed in pocket-lint:
assignee: nobody → Adi Roiban (adiroiban)
status: Triaged → Fix Committed
Revision history for this message
Michel Albert (esz-michel) wrote :

@sinzui: Can you link to the changeset? I am curious to see *how* this was implemented. Sorry if the link is already available somewhere, but I could noit find it :\

I would really like this feature as well, but more fine-grained as proposed. For example, pylint allows you to disable a specific error-code (see http://www.logilab.org/card/pylint_manual#messages-control). Or, the Java @SuppressWarnings annotation also allows you to ignore specific warnings.

I find the proposed solution to simply ignore pyflakes altogether is a bit like a "except Exception": You might miss other errors ;)

Revision history for this message
Curtis Hovey (sinzui) wrote :

Adi extended PyFlakesChecker to accept the raw text of the module being checked. The report() method looks of the line number of warning/error and in the text and looks for "# pyflakes:ignore". The report returns early if the comment is present.
    https://code.launchpad.net/~adiroiban/pocket-lint/907742/+merge/102882

Revision history for this message
Adi Roiban (adiroiban) wrote :

Hi,

Right now, pyflakes messages do not have a ID for each message class. In pylint each message class has its own ID.

I agree that this all or nothing filter can lead to other troubles. I will check to see if pyflakes developers would like to accept a patch to pyflakes to add IDs for each message class, and then we can improve the current pocketlint filter.

Revision history for this message
Buck Evan (bukzor) wrote :

Alternative solution: use a pylintrc that is only roughly as picky as pyflakes.

Curtis Hovey (sinzui)
Changed in pocket-lint:
status: Fix Committed → Fix Released
Revision history for this message
Glyph Lefkowitz (glyph) wrote :

There's already a bug for automatically detecting the entirely detectable class of error in the example here, isn't there? It would be so much nicer to do that than to encourage users to drop these comment turds into their code.

Changed in pyflakes:
importance: Undecided → Wishlist
Revision history for this message
Jean-Paul Calderone (exarkun) wrote :

More than one:

  https://bugs.launchpad.net/pyflakes/+bug/885140
  https://bugs.launchpad.net/pyflakes/+bug/800691
  https://bugs.launchpad.net/pyflakes/+bug/916264

Probably others. Fixing these real problems would be a great win for all pyflakes users.

Revision history for this message
Florent (florent.x) wrote :

Issues 800691 and 916264 and many others were fixed with Pyflakes 0.6.1.

If you *really* want to use comments to disable some checks, try Flake8.

But the recommended path is to open an issue if it looks like a limitation of Pyflakes.

Changed in pyflakes:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.