Ruff removes import for fixture used by pytest
Closed this issue · 3 comments
I searched for "remove pytest fixture"
I have code like this:
from my_code import my_fixture
...
def test_my_thing(my_fixture):
do_the_test(my_fixture)
Ruff sees my_fixture
as unused and removes the import
line. This, of course, then breaks the test.
This was done with ruff check --fix
on ruff 0.8.0, with no special settings.
Sorry to hear this broke your tests, that's frustrating!
This is a bit challenging to address because pytest is (if I understand correctly) performing a custom parse of your test in order to actually "insert" the fixture as an argument to that function. From the point of view of the actual Python specification the imported my_fixture
really is unused.
Would it be possible in your use-case to:
- Ignore the rule unused-import (F401) for test files by using the
exclude
option, or by making a separate config in your tests directory? or - Use conftest.py so you don't have to import fixtures?
Ah, and in fact it looks like this is a duplicate of #3295 - so maybe further discussion should be moved there!
Thanks!