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

Allow explicit overriding of lambda fixture typing #4

Open
theY4Kman opened this issue Jun 15, 2022 · 1 comment
Open

Allow explicit overriding of lambda fixture typing #4

theY4Kman opened this issue Jun 15, 2022 · 1 comment

Comments

@theY4Kman
Copy link
Owner

There have been times I really wanted to override the types that PyCharm has guessed for a lambda fixture's expression. It would be nice if I could explicitly type the lambda fixture lhs, so I wouldn't have to curry this typing info to every test I use the fixture in (and which would be impossible to do inside other lambda params). Something like:

my_guy: LambdaFixture[int] = lambda_fixture(lambda: 12)

It would be real nice if pytest itself exposed its types publicly, as it has what we need:

# The value of the fixture -- return/yield of the fixture function (type variable).
_FixtureValue = TypeVar("_FixtureValue")

# The type of a fixture function (type alias generic in fixture value).
_FixtureFunc = Union[
    Callable[..., _FixtureValue], Callable[..., Generator[_FixtureValue, None, None]]
]

Though, those types kinda fall down when async is involved — the actual value of the fixture is not what the fixture callable returns; in those cases, the callable would return a Coroutine, but that's not what test methods/fixtures would see when requesting the fixture.

Setting the type of the declaration directly to the fixture value type doesn't quite cut it, because the actual object returned by lambda_fixture() is a callable, just like any other pytest fixture function. It would be incorrect to lose this information. Perhaps the best approach might be to make LambdaFixture generic, ignore async deets, and call it a day.

@theY4Kman
Copy link
Owner Author

In version 2.2.0, I made LambdaFixture generic. This should at least open the door to explicitly declaring the fixture value type. However, handling destructured lambda fixtures hasn't really been fleshed out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant