GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add dedicated page about using types with pytest · Issue #12842 · pytest-dev/pytest

Add dedicated page about using types with pytest #12842

New issue
Copy link
New issue
Copy link
Closed
Closed
Add dedicated page about using types with pytest#12842
Copy link
Assignees
mwychung
Labels
type: docsdocumentation improvement, missing or needing clarification

Description

@nicoddemus
nicoddemus
opened on Sep 25, 2024
Issue body actions

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

Reacted by Gabriel

Activity

  1. nicoddemus
    added
    type: docsdocumentation improvement, missing or needing clarification
    on Sep 25, 2024
  2. okken commented on Sep 25, 2024

    @okken
    okken
    on Sep 25, 2024
    Contributor
    More actions

    This would be good. I’m on a project trying to use fixture types and it’s not obvious how to get those types.

  3. mwychung commented on Nov 13, 2024

    @mwychung
    mwychung
    on Nov 13, 2024
    Contributor
    More actions

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

  4. Pierre-Sassoulas
    assigned
    mwychung
    on Nov 13, 2024
  5. Pierre-Sassoulas commented on Nov 13, 2024

    @Pierre-Sassoulas
    Pierre-Sassoulas
    on Nov 13, 2024
    Member
    More actions

    Sure, thank you for wanting to contribute to pytest !

  6. mwychung
    added a commit that references this issue on Nov 14, 2024

    Add documentation for typing with pytest (pytest-dev#12842)

    8eabd1c
  7. mwychung
    added a commit that references this issue on Nov 29, 2024

    Added changelog entry for pytest-dev#12842

    e2ad0bb
  8. daria-shaw commented on Dec 1, 2024

    @daria-shaw
    daria-shaw
    on Dec 1, 2024
    Contributor
    More actions

    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.

  9. The-Compiler commented on Dec 1, 2024

    @The-Compiler
    The-Compiler
    on Dec 1, 2024
    Member
    More actions

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

    Reacted by Daria
  10. nicoddemus
    added a commit that references this issue on Mar 1, 2025

    Added changelog entry for pytest-dev#12842

    335327c
  11. nicoddemus
    closed this as completedin ca35145on Mar 1, 2025
  12. patchback
    added a commit that references this issue on Mar 1, 2025

    Added dedicated page about using types with pytest #12842 (#12963)

    0d02455
  13. nicoddemus
    added 2 commits that reference this issue on Mar 1, 2025

    Added dedicated page about using types with pytest #12842 (#12963) (#…

    e217726

    Added dedicated page about using types with pytest pytest-dev#12842 (p…

    ca35145
  14. luketainton
    added a commit that references this issue on Jan 5, 2026

    chore(deps): update dependency pytest to v9 (#305)

    4b75154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @mwychung
    mwychung

Labels

type: docsdocumentation improvement, missing or needing clarification

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.