Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest fixtures dimmed as unused import despite being used #6458

Open
dl-lim opened this issue Sep 26, 2024 · 3 comments
Open

pytest fixtures dimmed as unused import despite being used #6458

dl-lim opened this issue Sep 26, 2024 · 3 comments
Assignees
Labels
bug Something isn't working pytest Having to do with dynamic pytest support

Comments

@dl-lim
Copy link

dl-lim commented Sep 26, 2024

Environment data

  • Pylance version: v2024.9.2
  • OS and version: Win10.0.19045 Build 19045
  • VSCode Version: 1.93.1
  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.5, direct from Python.org

Code Snippet

Given these two files: tests.test_dependencies and tests.test_module

tests.test_dependencies

import pytest

@pytest.fixture(scope="function")
def fixture_function():
    yield 1

tests.test_module

from tests.test_dependencies import fixture_function

def test_number_one(fixture_function):
    assert fixture_function == 1

Repro Steps

  1. Use VSCode's built-in Pylance
  2. pip install pytest
  3. Write above code in a module.
  4. See fixture dimmed, depending on your theme.

Expected behavior

image

Actual behavior

image

Logs

N/A

Comments

I was initially hesitant on posting this after reading #1059 and #684, but I thought I'd post my own discussion here to get to the bottom of this, if that's welcome.

Is this dimming of the unused import something handled by pylint, and not an issue with pylance?

Is this also due to the way pytest calls fixtures which may perhaps be less than pythonic?

Any hints on where the root of this may lie? I'd be glad to look into this.

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Sep 26, 2024
@rchiodo
Copy link
Contributor

rchiodo commented Sep 26, 2024

This is because Pyright (the underlying typechecker for Pyright) doesn't see this code as using the fixture:

def test_fixture_function(fixture_function):
    assert fixture_function == 1

From its point of view, the fixture_function parameter is just a named parameter, it is not actually using the import. That you have to import the fixture is really a function of how pytest works at runtime and can't currently be modeled by a static typechecker.

We do however add some dynamic capabilities to Pyright inside of Pylance and usage of fixtures is just something we haven't done yet.

@rchiodo rchiodo added pytest Having to do with dynamic pytest support and removed needs repro Issue has not been reproduced yet labels Sep 26, 2024
@debonte debonte added the bug Something isn't working label Sep 26, 2024
@dl-lim
Copy link
Author

dl-lim commented Sep 26, 2024

Hmm, further digging, I realise this is exactly what conftest.py was made for! Using that means one does not need to perform such imports. Hope this helps anyone else stumbling upon this.

Regardless, the imports also work just fine even if they are declared as in my example above. They'll just be linted wrongly as above. I believe it could still be looked into...

@heejaechang
Copy link
Contributor

heejaechang commented Oct 3, 2024

if you want to keep the import, you need to write like from tests.test_dependencies import fixture_function as fixture_function

the grey out means the code will work without the import statement. you will get same behavior if you add import os. it won't break your code even if you have unnecessary import, but we are telling you that the code will still work even if you remove the import statement.

...

ah, I think I misunderstood what you are saying. you are saying when you do it without conftest.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pytest Having to do with dynamic pytest support
Projects
None yet
Development

No branches or pull requests

4 participants