You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add dedicated page about using types with pytest #12842
What's the recommended typing for fixtures returning more complex callables?
Should it be typed as a Protocol, or should the implementation be using a class instance?
@daara-s I don't think there's anything pytest specific there, the same applies to type annotating complex callables elsewhere.
The Python documentation agrees with your Protocol example and explains that in-depth. Personally that's also what I'd prefer, as it seems simpler than defining a callable class that doesn't do anything else.
For more complex fixtures, you might want to expose functionality via an utility class. This also has the benefit of usually being easier to understand for Python beginners over returning a nested function. Then I'd prefer having a more clear method name (say, start) rather than using __call__ though.
Activity
okken commentedon Sep 25, 2024
This would be good. I’m on a project trying to use fixture types and it’s not obvious how to get those types.
mwychung commentedon Nov 13, 2024
Hi, it is my first time make contribution. Can this be assigned to me please? Thank you.
Pierre-Sassoulas commentedon Nov 13, 2024
Sure, thank you for wanting to contribute to pytest !
Add documentation for typing with pytest (pytest-dev#12842)
Added changelog entry for pytest-dev#12842
daria-shaw commentedon Dec 1, 2024
Heya, I've got a Q about typing fixtures that return callables, which might fit into the docs PR.
Given the example below, the typing should be
Callable[[str], str]based on the internal function in the fixture.What's the recommended typing for fixtures returning more complex callables?
Should it be typed as a Protocol, or should the implementation be using a class instance?
Aware this is more useful for typing plugins than regular fixture use in projects.
The-Compiler commentedon Dec 1, 2024
@daara-s I don't think there's anything pytest specific there, the same applies to type annotating complex callables elsewhere.
The Python documentation agrees with your
Protocolexample and explains that in-depth. Personally that's also what I'd prefer, as it seems simpler than defining a callable class that doesn't do anything else.For more complex fixtures, you might want to expose functionality via an utility class. This also has the benefit of usually being easier to understand for Python beginners over returning a nested function. Then I'd prefer having a more clear method name (say,
start) rather than using__call__though.Added changelog entry for pytest-dev#12842
Added dedicated page about using types with pytest #12842 (#12963)
Added dedicated page about using types with pytest #12842 (#12963) (#…
Added dedicated page about using types with pytest pytest-dev#12842 (p…
chore(deps): update dependency pytest to v9 (#305)