F811 errors with pytest fixtures?
Hi all -- 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? -- Chris
Hi Chris,
Chris Bergstresser
Hi all --
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
Firstly I'd like to warn against importing pytest fixtures. With pytest this is really considered a bad practice and can lead to surprising results. The problem is with fixtures scoped at higher levels then functions, when imported they can appear multiple times and the scope no longer applies. Instead it is recommended to put all fixtures in the correct conftest.py file and rely on the pytest fixture search path to find the fixture.
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.
There is no great answer to this unfortunately. One primary way to avoid this linting warning is to put the fixture in the conftest.py file as described above, there is then no definition in the same file and the warning will go away. For fixtures in the test module itself you can use the somewhat clunky @pytest.fixture(name='foo): def fix_foo(): pass Another option is that we should really teach the linters about pytest. E.g. I'd love a pylint plugin that complains about misspelling fixture etc (and importing fixtures ;-))
Why does it ignore the noqa flag?
I'll pass on this one - I'm not a heavy flake8/pyflakes user.
Is there a better way to avoid flagging this error?
I hope I've answered this somewhat reasonably though. Regards, Floris
Hi Chris,
Chris Bergstresser
Hi all -- 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
Firstly I'd like to warn against importing pytest fixtures. With pytest this is really considered a bad practice and can lead to surprising results. The problem is with fixtures scoped at higher levels then functions, when imported they can appear multiple times and the scope no longer applies. Instead it is recommended to put all fixtures in the correct conftest.py file and rely on the pytest fixture search path to find the fixture.
def test_case(fixture1): # noqa: F811 # Test goes here [ 2 more citation lines. Click/Enter to show. ] assert 1 == 1 ----
Generates a "lib/python/test.py:3:1: F811 redefinition of unused 'fixture1' from line 1" error during linting.
There is no great answer to this unfortunately. One primary way to avoid this linting warning is to put the fixture in the conftest.py file as described above, there is then no definition in the same file and the warning will go away. For fixtures in the test module itself you can use the somewhat clunky @pytest.fixture(name='foo): def fix_foo(): pass Another option is that we should really teach the linters about pytest. E.g. I'd love a pylint plugin that complains about misspelling fixture etc (and importing fixtures ;-))
Why does it ignore the noqa flag?
I'll pass on this one - I'm not a heavy flake8/pyflakes user.
Is there a better way to avoid flagging this error?
I hope I've answered this somewhat reasonably though. Regards, Floris
On Tue, May 02, 2017 at 04:15:22PM -0400, Chris Bergstresser wrote:
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?
This works for me: if I copy your 4-line snippet into a file and run flake8, I get one warning (E302 expected 2 blank lines, found 1). If I remove both noqa comments, I get three warnings (F401, E302, F811). Tried with two flake8 versions -- whatever I had before, which was $ flake8 --version 3.2.1 (pyflakes: 1.3.0, pycodestyle: 2.2.0, mccabe: 0.5.3) CPython 2.7.13 on Linux and the current release, which is 3.3.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.5.0) CPython 2.7.13 on Linux HTH, Marius Gedminas -- The primary purpose of the DATA statement is to give names to constants; instead of referring to PI as 3.141592653589797, at every appearance, the variable PI can be given that value with a DATA statement, and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of PI change. -- Fortran manual for Xerox computers
participants (4)
-
Chris Bergstresser
-
Floris Bruynooghe
-
Floris Bruynooghe
-
Marius Gedminas