Search code examples
pythonhypothesis-test

Disable NonInteractiveExampleWarning in python hypothesis


I want to use hypothesis awesome features to create some sample data for my application. I use it roughly like this

from hypothesis import strategies as st

ints = st.integers()  #simplified example
ints.example()

I get this warning:

NonInteractiveExampleWarning: The .example() method is good for exploring strategies, but should only be used interactively

Is there a simple way to disable this warning? Just to be clear: I want to use the example data generation outside of a testing and in a non-interactive context and I'm aware of what the warning is referring to. I just want to get rid of it.


Solution

  • The warnings module lets you selectively ignore specific warnings; for example:

    from hypothesis.errors import NonInteractiveExampleWarning
    from hypothesis import strategies as st
    import warnings
    
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=NonInteractiveExampleWarning)
    
        ints = st.integers()
        print( ints.example() )