Search code examples
pythonpytestmypy

What is the type hint for Pytest's "caplog" fixture?


I am using the caplog fixture that comes with pytest. I am using mypy for my type checking, and would like to know what the correct type hint for caplog is.

For example:

def test_validate_regs(caplog: Any) -> None:
    validate_regs(df, logger)
    assert caplog.text == "", "No logs should have been made."

In this example, I have it set to Any, but I am wondering if there is a more specific type hint I can use.

I've tried reading the docs on caplog, as well as searching the pytest code in github to see what the caplog fixture returns, but couldn't find anything more than the following. But using a str type just gave me an error, saying that str type doesn't have the attribute text, which makes sense.

When I print the type of caplog I get _pytest.logging.LogCaptureFixture although I'm not sure how I'd import this from _pytest and make use of it.


Solution

  • as of pytest 6.2.0 you should use pytest.LogCaptureFixture

    prior to that you needed to import a private name which is not recommended (we frequently change the internals inside the _pytest namespace without notice and with no promises of forward or backward compatibility)


    disclaimer: I'm a pytest core dev