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

Add dedicated page about using types with pytest #12842

Closed
nicoddemus opened this issue Sep 25, 2024 · 5 comments
Closed

Add dedicated page about using types with pytest #12842

nicoddemus opened this issue Sep 25, 2024 · 5 comments
Assignees
Labels
type: docs documentation improvement, missing or needing clarification

Comments

@nicoddemus
Copy link
Member

As follow up to #9947, add a dedicated page describing how to use types with pytest: how to type tests, fixtures, etc.

@nicoddemus nicoddemus added the type: docs documentation improvement, missing or needing clarification label Sep 25, 2024
@okken
Copy link
Contributor

okken commented 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
Copy link
Contributor

Hi, it is my first time make contribution. Can this be assigned to me please? Thank you.

@Pierre-Sassoulas
Copy link
Member

Sure, thank you for wanting to contribute to pytest !

@daara-s
Copy link
Contributor

daara-s commented 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.

@pytest.fixture
def hello_world_fixture() -> Callable[[str], str]:
    def _hello_world_func(input: str) -> str:
        """docstring"""
        return "hello " + input

    return _hello_world_func


def test_hello_world(hello_world_fixture: Callable[[str], str]):
    assert hello_world_fixture("world") == "hello world"

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?

class HelloWorldProtocol(Protocol):
    def __call__(self, input: str) -> str:
        """call docstring"""
        ...


class HelloWorldFixture:
    """class docstring"""

    def __call__(self, input: str) -> str:
        """call docstring"""
        return "hello " + input

Aware this is more useful for typing plugins than regular fixture use in projects.

@The-Compiler
Copy link
Member

@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.

nicoddemus pushed a commit to mwychung/pytest that referenced this issue Mar 1, 2025
patchback bot pushed a commit that referenced this issue Mar 1, 2025
Fixes #12842

---------

Co-authored-by: Bruno Oliveira <[email protected]>
(cherry picked from commit ca35145)
nicoddemus pushed a commit that referenced this issue Mar 1, 2025
…13260)

Fixes #12842

---------

Co-authored-by: Bruno Oliveira <[email protected]>
(cherry picked from commit ca35145)

Co-authored-by: mwychung <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: docs documentation improvement, missing or needing clarification
Projects
None yet
Development

No branches or pull requests

6 participants