Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Flake8 to work with F811 errors?

Tags:

python

flake8

We're using flake8 to test our code, and we're using pytest with fixtures. The following code:

from staylists.tests.fixtures import fixture1  # noqa: F401

def test_case(fixture1):  # noqa: F811
    # Test goes here
    assert 1 == 1

Generates a lib/python/test.py:3:1: F811 redefinition of unused 'fixture1' from line 1 error during linting.

  • Why does it ignore the noqa flag?
  • Is there a better way to avoid flagging this error?
like image 317
Chris B. Avatar asked May 02 '17 20:05

Chris B.


People also ask

How do error codes work in flake8?

Flake8 and its plugins assign a code to each message that we refer to as an error code (or violation ). Most plugins will list their error codes in their documentation or README. Flake8 installs pycodestyle, pyflakes, and mccabe by default and generates its own error code s for pyflakes:

What is flake8 and how to use it?

Subscribe to our YouTube Channel! Flake8 is a Python library that wraps PyFlakes, pycodestyle and Ned Batchelder’s McCabe script. It is a great toolkit for checking your code base against coding style (PEP8), programming errors (like “library imported but unused” and “Undefined name”) and to check cyclomatic complexity.

How do I ignore a file in flake8?

There are two ways to ignore the file: The former is the recommended way of ignoring entire files. By using our exclude list, we can include it in our configuration file and have one central place to find what files aren’t included in Flake8 checks.

How do I run flake8 checks in Python?

See the full list of options. Just add # noqa in the end of the line. The example below is a .travis.yml file from a project of mine. You can run flake8 checks very easily just by adding the commands inside the script list. That’s it!


1 Answers

The F401 and F811 errors can be avoided by moving all fixtures into the conftest.py file. Pytest loads this file automatically and makes all fixtures inside available in all tests, even without explicit import statements.

More discussion about the file can be found here: In py.test, what is the use of conftest.py files?

like image 108
fafl Avatar answered Oct 02 '22 15:10

fafl