Skip to content

Allow explicit overriding of lambda fixture typing #4

@theY4Kman

Description

@theY4Kman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions