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

Bug with patch and mypy #4263

Closed
staticdev opened this issue Jun 25, 2020 · 8 comments
Closed

Bug with patch and mypy #4263

staticdev opened this issue Jun 25, 2020 · 8 comments

Comments

@staticdev
Copy link

from unittest.mock import Mock
from unittest.mock import patch

import click.testing
import pytest
from pytest_mock import MockFixture

from irpf_cei import __main__

@pytest.fixture
def mock_cei_validate_header(mocker: MockFixture) -> Mock:
    """Fixture for mocking cei.validate."""
    mock = mocker.patch("irpf_cei.cei.validate_header")
    mock.return_value = 2019, "ABC"
    return mock

When I check the code above I get:

error: Returning Any from function declared to return "Mock" 
[no-any-return]
        return mock

But I expect Mock from patch. I worked in previous versions.

@JelleZijlstra
Copy link
Member

I suppose this is caused by #3871?

@staticdev
Copy link
Author

@JelleZijlstra is there a release with this fix so I can test?

@srittau
Copy link
Collaborator

srittau commented Jun 25, 2020

pytest_mock has no stubs in typeshed, which is why mocker.patch() returns Any. Previously, Mock was aliased to Any, which is why this did not cause errors before. Possible solutions:

@srittau srittau closed this as completed Jun 25, 2020
@staticdev
Copy link
Author

staticdev commented Jun 25, 2020

@srittau so adding type-hints to pytest_mock also solves the problem?
It seems like a more permanent solution and this lib has like 300 lines of code.

@srittau
Copy link
Collaborator

srittau commented Jun 25, 2020

That would be the best solution, of course.

@staticdev
Copy link
Author

staticdev commented Jun 26, 2020

@srittau while it is not accepted by pytest_mock. I tried adding the stubs for pytest_mock from https://github.com/srittau/python-stubs/tree/master/stubs/pytest_mock.

I am still getting a related errors:

tests/test_validation.py: note: In function "mock_open_valid_file":
tests/test_validation.py:28:5: error: Returning Any from function declared to return "Mock" 
[no-any-return]
        return mocker.patch("builtins.open", mocker.mock_open(read_data="[x]\na = 3"))
        ^
tests/test_validation.py:28:42: error: "MockFixture" has no attribute "mock_open"  [attr-defined]
        return mocker.patch("builtins.open", mocker.mock_open(read_data="[x]\na = 3"))
                                             ^
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")
                             ^
Found 4 errors in 1 file (checked 8 source files)

The draft PR is here staticdev/toml-validator#110

Can you also help me with that one?

@srittau
Copy link
Collaborator

srittau commented Jun 26, 2020

@staticdev Do you run mypy with MYPYPATH? In our internal codebase, the stubs are in a sub-directory stubs and mypy is called like this: MYPYPATH=stubs mypy ...

@staticdev
Copy link
Author

@srittau yes, I put them on the path. And if I remove the folder I get even more errors:

tests/test_validation.py:5:1: error: Skipping analyzing 'pytest_mock': found module but no type
hints or library stubs  [import]
    from pytest_mock import MockFixture
    ^
tests/test_validation.py: note: In function "mock_tomlkit_parse":
tests/test_validation.py:14:5: error: Returning Any from function declared to return "Mock" 
[no-any-return]
        return mocker.patch("tomlkit.parse")
        ^
tests/test_validation.py: note: In function "mock_tomlkit_parse_exception":
tests/test_validation.py:22:5: error: Returning Any from function declared to return "Mock" 
[no-any-return]
        return mock
        ^
tests/test_validation.py: note: In function "mock_open_valid_file":
tests/test_validation.py:28:5: error: Returning Any from function declared to return "Mock" 
[no-any-return]
        return mocker.patch("builtins.open", mocker.mock_open(read_data="[x]\na = 3"))
        ^
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_main.py:6:1: error: Skipping analyzing 'pytest_mock': found module but no type hints or
library stubs  [import]
    from pytest_mock import MockFixture
    ^
tests/test_main.py:6:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
tests/test_main.py: note: In function "mock_validation_validate_extension":
tests/test_main.py:20:5: error: Returning Any from function declared to return "Mock" 
[no-any-return]
        return mocker.patch("toml_validator.validation.validate_extension")
        ^
tests/test_main.py: note: In function "mock_validation_validate_toml_no_error":
tests/test_main.py:28:5: error: Returning Any from function declared to return "Mock" 
[no-any-return]
        return mock
        ^
tests/test_main.py: note: In function "mock_validation_validate_toml_with_error":
tests/test_main.py:36:5: error: Returning Any from function declared to return "Mock" 
[no-any-return]
        return mock
        ^
Found 9 errors in 2 files (checked 8 source files)

I think the argument mocker.mock_open is breaking the interface in this case.

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

3 participants