JBKahn / flake8-print

flake8
MIT License
119 stars 22 forks source link

Putting back "# noqa" #2

Closed joncle closed 9 years ago

joncle commented 10 years ago

You could (although I haven't tested it, and I'm afraid don't have time to do a PR) tokenize the source file to identify lines with # noqa comments:

import tokenize
import ast

with open('somefile') as fin:
    source = fin.read()
    parsed = ast.parse(source)

tokens = tokenize.generate_tokens(lambda L=iter(source.splitlines()): next(L))
noqa = {
    token[2][0] for token in tokens 
    if token[0] == tokenize.COMMENT and token[1].endswith('noqa')
}

Then carry on with parsed as you are, but exclude messages whose line is present in noqa...

JBKahn commented 10 years ago

oooo, I have a test tomorrow but I'll try this out on Tuesday. Great idea....identity the lines with noqa and skip them. wow. now I feel like an idiot.

JBKahn commented 10 years ago

Also, thanks for pointing out ast.

I was trying to come up with something as robust without having to read the file twice.

JBKahn commented 9 years ago

Fixed in master, pushed as 1.4. Works. stdin has cases that don't work the same given the same file, but mostly good.