shortorian
shortorian

Reputation: 1182

Is there a way to make flake8 ignore a violation in a specific circumstance?

I'm using the pandas package in Python and I'm working in visual studio code. I regularly use queries on dataframes, which can reference variable names in the external scope like this

import pandas as pd
df = pd.DataFrame({'a': [1, 2], 'b': [10, 20]})
query_value = 20
print(df.query('b == @query_value'))

which would produce roughly this output

    a    b
1   2   20

Flake8 sees an F841 violation (local variable assigned but never used) in this case because query_value is referenced inside a string. Is there a way for me to configure flake8 (or a vscode extension specifically) to ignore F841 violations only when the variable name appears preceded by @ within a string somewhere in the file?

I know I can do # noqa: f841 to ignore violations in a line and ignore violations per-file with a command line argument, but these aren't what I want. I want to ignore F841 only when I've used a variable correctly in a pandas query.

Upvotes: 0

Views: 1138

Answers (1)

Nick ODell
Nick ODell

Reputation: 25454

There's a suggestion on the flake8 issue tracker of a way to do this that doesn't cause a F841 violation.

query_value = 20
print(df.query('b == @q', local_dict={'q': query_value}))

It's a bit more verbose, though.

Upvotes: 1

Related Questions