Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Ignore unused-import error when importing into conftest.py #2

Closed
TylerYep opened this issue Jul 21, 2020 · 5 comments
Closed

Ignore unused-import error when importing into conftest.py #2

TylerYep opened this issue Jul 21, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@TylerYep
Copy link

I get the following pylint error when I try to import my fixtures into conftest.py but do not use them anywhere in the code. However, these unused imports in conftest.py are a special case, because fixtures imported that file are accessible globally in all other test files without needing to import them.

conftest.py:11:0: W0611: Unused example_statement imported from fixtures (unused-import)

It would be great if this plugin could ignore this error specifically for conftest.py!

The simple workaround is to add at the top of conftest.py:

# pylint: disable=unused-import

But I would like to avoid this if possible. Thanks!

@blueyed
Copy link

blueyed commented Jul 22, 2020

Is the use case having fixtures in non-conftest files, or/and importing them from somewhere else into other conftest files only?
(asking since you might want to / could put them into specific conftest modules based on where they get used directly)

Apart from that I think disabling the warning there (maybe per line / affected import) is quite OK.

Do you suggest that the warning should get disabled by default for conftest modules? This would hide any issues in that regard in general then though.

@TylerYep
Copy link
Author

TylerYep commented Jul 22, 2020

I have a fixtures/ folder containing all of my fixtures (there are many). In conftest.py, I import all of these fixtures so that they can be used in all of my test files, but they are not used in conftest.py themselves. They are used across all tests, so having them globally available works well.

I was thinking that the warning should be disabled specifically for the module named conftest, but if this would cause other problems, then this issue can be closed. Thanks!

@reverbc reverbc added the enhancement New feature or request label Jul 23, 2020
@reverbc
Copy link
Owner

reverbc commented Jul 23, 2020

Thanks for requesting this @TylerYep. To me it sounds like a legit use case if we have many fixtures and want to group them in different modules to avoid one gigantic conftest.py.

By leveraging pytest --fixtures, we should be able to tell if an imported attr is a pytest fixture or not, and if yes && the current module is conftest then we suppress the warning.

Since conftest.py is a magic module in pytest, I guess it's totally okay to make a special case for this and should be doable by modifying pylint_pytest/checkers/fixture.py.

@blueyed do you see any potential issue by implementing this FR? If we can make sure we only suppress the unused-import warning for fixtures in conftest.py will that still be a concern?

@TylerYep
Copy link
Author

TylerYep commented Mar 3, 2021

Noticed the new release, any chance this can be added as well? Thanks :)

I can try looking into making a PR if it's not a super fast fix.

@reverbc reverbc self-assigned this Mar 4, 2021
reverbc added a commit that referenced this issue Mar 4, 2021
#2 suppressing FP when importing fixtures into conftest
@reverbc
Copy link
Owner

reverbc commented Mar 4, 2021

Implemented with #9

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants