Hi Chris,

Chris Bergstresser <ch...@subtlety.com> writes:

> 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
_______________________________________________
code-quality mailing list
code-quality@python.org
https://mail.python.org/mailman/listinfo/code-quality

Reply via email to