I want to be able to import Mock and MagicMock types for type annotations.
I don't want to have to import from unittest.mock because I am enforcing the use of pytest-mock by banning the import of unittest.mock.
from typing import TYPE_CHECKING
import pytest
if TYPE_CHECKING:
from pytest_mock import Mock, MagicMock, MockerFixture
@pytest.fixtue
def my_mock(mocker: MockerFixture) -> Mock:
return mocker.Mock()
@pytest.fixtue
def my_magic_mock(mocker: MockerFixture) -> MagicMock:
return mocker.MagicMock()
def test_something(my_mock: Mock):
...
I see 2 different approaches here.
- "Export"
unittest.mock Mock + MagicMock in the top-level __init__ file
- Add a
pytest_mock.types subpackage. This could also include things like ANY which would otherwise need to be imported from unittest.mock.
1 could be an issue because it might encourage users to use the Mock/Mocker as more than just type-annotations.
I want to be able to import
MockandMagicMocktypes for type annotations.I don't want to have to import from
unittest.mockbecause I am enforcing the use ofpytest-mockby banning the import ofunittest.mock.I see 2 different approaches here.
unittest.mockMock+MagicMockin the top-level__init__filepytest_mock.typessubpackage. This could also include things likeANYwhich would otherwise need to be imported fromunittest.mock.1could be an issue because it might encourage users to use theMock/Mockeras more than just type-annotations.