Skip to content

Bug with patch and mypy #4263

Closed
Closed

Description

@staticdev
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.

Activity

  1. JelleZijlstra commented on Jun 25, 2020

    @JelleZijlstra
    Member

    I suppose this is caused by #3871?

  2. staticdev commented on Jun 25, 2020

    @staticdev
    Author

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

  3. srittau commented on Jun 25, 2020

    @srittau
    Collaborator

    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:

  4. staticdev commented on Jun 25, 2020

    @staticdev
    Author

    @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.

  5. srittau commented on Jun 25, 2020

    @srittau
    Collaborator

    That would be the best solution, of course.

  6. staticdev commented on Jun 26, 2020

    @staticdev
    Author

    @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?

  7. srittau commented on Jun 26, 2020

    @srittau
    Collaborator

    @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 ...

  8. staticdev commented on Jun 26, 2020

    @staticdev
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions