pytest-dev/pytest-mock

Allow for easier import of `mocker.Mock | mocker.MagicMock` types

Kilo59 opened this issue · 0 comments

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.

  1. "Export" unittest.mock Mock + MagicMock in the top-level __init__ file
  2. 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.