microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 766 forks source link

Pytest fixtures and tests do not have autocompletion for types #5203

Open zmievsa opened 11 months ago

zmievsa commented 11 months ago

Environment data

Code Snippet

import pytest

class MyVeryLongClassName:
    pass

def I_HAVE_AUTOCOMPLETION(s):
    pass

@pytest.fixture()
def s() -> MyVeryLongClassName:
    return MyVeryLongClassName()

@pytest.fixture()
def I_DONT_HAVE_AUTOCOMPLETION(s):
    pass

Repro Steps

Try adding a type hint with that class name to both functions. You will see that it gives autocompletion for one but doesn't for another. It should give autocompletion for both.

Logs

Attached. [Uploading logs.txt…]()

heejaechang commented 11 months ago

@zmievsa

Try adding a type hint with that class name to both functions. You will see that it gives autocompletion for one but doesn't for another. It should give autocompletion for both.

I am not sure what you meant? where are you adding the type hint? after s? like s: <= here? and by that class name you meant MyVeryLongClassName ?

zmievsa commented 11 months ago

Yes! Exactly! :)

heejaechang commented 11 months ago

@zmievsa I assume the s is another fixture? and you want to write fixture's type explicitly?

heejaechang commented 11 months ago

the bug here is when user is trying to write fixture's type directly, we don't show completion for that. we let them auto insert it using code action or inlay hint but doesn't help writing it manually.

zmievsa commented 11 months ago

Yes. This is very much the bug I'm encountering.

I understand very well that It was intentional for you not to support this behavior because you can auto-generate annotations.

However, "pytest-fixture-classes" is not supported by pylance and hence it doesn't auto-generate annotations too which gives me essentially no way to work with that package conveniently (my package so I'm biased but bear with me).

Essentially there is no way to define factory fixtures with pytest and without any repetition, which is why we use "pytest-fixture-classes". I am quite ok with this package not being supported by pylance — it's not super popular. But let's at least consider supporting auto-completion for annotations of fixtures in tests and other fixtures. It shouldn't be too hard to do and I would love to do it myself if there is any developer documentation in pylance :)

heejaechang commented 11 months ago

what do you mean by pytest-fixture-classes can you give us a link? so we can see whether we can add a suport for it?

zmievsa commented 11 months ago

Here it is: https://github.com/zmievsa/pytest-fixture-classes

Sorry for not providing it with the initial message :)

zmievsa commented 11 months ago

@heejaechang what do you think?