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

unittest.mock.patch decorator doesn't accept args #3270

Closed
ashb opened this issue Sep 27, 2019 · 4 comments
Closed

unittest.mock.patch decorator doesn't accept args #3270

ashb opened this issue Sep 27, 2019 · 4 comments

Comments

@ashb
Copy link
Contributor

ashb commented Sep 27, 2019

Airflow uses mypy and the latest update (0.730) started causing us errors, that I think are related to invalid typing on mock.patch.

from unittest import mock

...

    @mock.patch(
        'airflow.gcp.hooks.base.GoogleCloudBaseHook.project_id',
        new_callable=PropertyMock,
        return_value=GCP_PROJECT_ID_HOOK_UNIT_TEST
    )

(from https://github.com/apache/airflow/blob/2abf0f5b70970bcd16e8cf5846ba67cf3945ecae/tests/gcp/hooks/test_cloud_build.py#L118-L122)

Causes these errors:

tests/gcp/operators/test_cloud_build.py:118: error: Argument 2 to "__call__" of "_patcher" has incompatible type "**Dict[str, Dict[str, object]]"; expected "bool"

tests/gcp/operators/test_cloud_build.py:134: error: Argument 2 to "__call__" of "_patcher" has incompatible type "**Dict[str, Dict[str, object]]"; expected "bool"

tests/gcp/operators/test_cloud_build.py:171: error: Argument 2 to "__call__" of "_patcher" has incompatible type "**Dict[str, Dict[str, object]]"; expected "bool"

(There are more examples of the same error in https://travis-ci.org/apache/airflow/jobs/590239664)

I'm not sure if this is a bug in the type definition or a bug in mypy itself.

@srittau
Copy link
Collaborator

srittau commented Sep 27, 2019

I am not sure what is going on, but I can't reproduce it with this simple test case:

from unittest import mock
from unittest.mock import PropertyMock

@mock.patch(
    'airflow.gcp.hooks.base.GoogleCloudBaseHook.project_id',
    new_callable=PropertyMock,
    return_value=''
)   
@mock.patch("airflow.gcp.hooks.cloud_build.CloudBuildHook.get_conn")
@mock.patch("airflow.gcp.hooks.cloud_build.time.sleep")
def test_error_operation(self, _, get_conn_mock, mock_project_id):
    pass

@ashb
Copy link
Contributor Author

ashb commented Sep 27, 2019

Iiinteresting.

If I run mypy tests/gcp/operators/test_cloud_build.py it doesn't error, but it does if I run mypy tests/ -- that is probably pointing more towards a bug in mypy than a typeshed error.

@ashb
Copy link
Contributor Author

ashb commented Sep 27, 2019

Here's a minimal repro case at least:

import unittest
from unittest import mock

class TestCloudBuildHookWithPassedProjectId(unittest.TestCase):
    @mock.patch(
        'airflow.gcp.hooks.base.GoogleCloudBaseHook.project_id',
        new_callable=mock.PropertyMock,
        **{"return_value.classify_text.return_value": {}}
    )
    def test_error_operation(self, _, mock_project_id):
        pass

Oddly that seems to "leak" in to other files.

This style of **{} is taken from the docs btw: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.configure_mock

@srittau
Copy link
Collaborator

srittau commented Sep 27, 2019

Filed as python/mypy#7572, where I have a repro that doesn't use typeshed stubs. Closing here.

@srittau srittau closed this as completed Sep 27, 2019
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