def module(): import pytest from pydantic.generics import is_call_from_module with pytest.raises( RuntimeError, match='This function must be used inside another function' ) as exc_info: is_call_from_module() e = exc_info.value assert isinstance(e.__cause__, IndexError), e.__cause__ assert isinstance(e.__context__, IndexError), e.__context__
def module(): from unittest.mock import patch import pytest from pydantic.generics import is_call_from_module with pytest.raises( RuntimeError, match='This function must be used inside another function' ) as exc_info: with patch('inspect.stack', new=lambda: [..., ...]): is_call_from_module() e = exc_info.value assert isinstance(e.__cause__, IndexError) assert isinstance(e.__context__, IndexError)
def third_function(): assert not is_call_from_module()
def another_function(): assert not is_call_from_module() third_function()
def function(): assert is_call_from_module() another_function()