Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for easier import of mocker.Mock | mocker.MagicMock types #414

Closed
Kilo59 opened this issue Mar 13, 2024 · 0 comments · Fixed by #415
Closed

Allow for easier import of mocker.Mock | mocker.MagicMock types #414

Kilo59 opened this issue Mar 13, 2024 · 0 comments · Fixed by #415

Comments

@Kilo59
Copy link
Contributor

Kilo59 commented Mar 13, 2024

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.

nicoddemus added a commit that referenced this issue Mar 21, 2024
Fix #414 

---------

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant