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

pytest_mock MockFixture does not accept mock_open #1

Closed
staticdev opened this issue Jun 29, 2020 · 4 comments
Closed

pytest_mock MockFixture does not accept mock_open #1

staticdev opened this issue Jun 29, 2020 · 4 comments

Comments

@staticdev
Copy link

staticdev commented Jun 29, 2020

If I call patch with just the target the stub works, but if I put the arg mock_open I get mypy errors:

from unittest.mock import Mock
import pytest
from pytest_mock import MockFixture

@pytest.fixture
def mock_tomlkit_parse(mocker: MockFixture) -> Mock:
    return mocker.patch("tomlkit.parse") # this works!

@pytest.fixture
def mock_open_valid_file(mocker: MockFixture) -> Mock:
    return mocker.patch("builtins.open", mocker.mock_open(read_data="[x]\na = 3"))

Mypy errors:

tests/test_validation.py: note: In function "mock_open_invalid_file":
tests/test_validation.py:34:5: error: Returning Any from function declared to return "Mock"  [no-any-return]
        return mocker.patch(
        ^
tests/test_validation.py:35:26: error: "MockFixture" has no attribute "mock_open"  [attr-defined]
            "builtins.open", mocker.mock_open(read_data="[x]\na = 3\n[x]\na = 3")
                             ^

You can see full example in this PR: staticdev/toml-validator#110

@srittau
Copy link
Owner

srittau commented Jun 29, 2020

These stubs are very incomplete. There are more complete stubs now in typeshed HEAD (and will be in the next mypy release). Unfortunately there is a problem with mock_open (and other mocks from unittest.mock): Due to google/pytype#612 this is not a proper alias. I will fix it in this branch in the meantime.

@staticdev
Copy link
Author

staticdev commented Jun 29, 2020

This one right? https://github.com/python/typeshed/blob/master/third_party/3/pytest_mock/plugin.pyi

With this stub I don't have the mock_open error, but I still have the first one: Returning Any from function declared to return "Mock" [no-any-return]

@srittau
Copy link
Owner

srittau commented Jun 29, 2020

@staticdev Yeah, as said above, it's still problematic. But please try the updated version in this repository. It won't work with pytype, due to the bug mentioned above, but it should work with mypy.

@staticdev
Copy link
Author

staticdev commented Jun 29, 2020

@srittau I tested with your latest commit, now the error changed:

tests/test_validation.py: note: In function "mock_open_invalid_file":
tests/test_validation.py:34:5: error: Returning Any from function declared to return "Mock"  [no-any-return]
        return mocker.patch(
        ^
tests/test_validation.py:35:26: error: Attribute function "mock_open" with type "Callable[[Optional[Any], Any], Any]" does not accept self argument  [misc]
            "builtins.open", mocker.mock_open(read_data="[x]\na = 3\n[x]\na = 3")
                             ^

Maybe this one is related to python/mypy#6910

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

No branches or pull requests

2 participants