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

Incompatible type with non-mandatory bool arg and kwargs #7572

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

Incompatible type with non-mandatory bool arg and kwargs #7572

srittau opened this issue Sep 27, 2019 · 4 comments

Comments

@srittau
Copy link
Contributor

srittau commented Sep 27, 2019

(Originally reported as python/typeshed#3270.)

Please consider:

from typing import Any

def dec(x: bool = ..., **kwargs: Any) -> None: 
    pass

dec(**{"": 123})

With mypy 0.720 (Python 3.7.3) and 0.730 (Python 3.7.4) and mypy master this reports (no flags):

foo.py:6: error: Argument 1 to "dec" has incompatible type "**Dict[str, int]"; expected "bool"
@ashb
Copy link

ashb commented Sep 27, 2019

We found this in our Airflow test suite -- for us the error only started appearing with 0.730

@ilevkivskyi
Copy link
Member

I think this is a duplicate of #5382, possible solution suggested in #5382 (comment) should fix this.

@ashb
Copy link

ashb commented Oct 14, 2019

@ilevkivskyi The original problem I ran in to from python/typeshed#3270 was on mock.patch -- so I'm not quite sure where I should be putting the TypedDict?

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

(I'm not sure if it's relevant or not but this error doesn't show up on 0.720, only on 0.730.)

@ilevkivskyi
Copy link
Member

I meant solution for us, not for users. But if you want a workaround, then I think something like this should pass (didn't test however):

import unittest
from unittest import mock
from typing import Any
from typing_extensions import TypedDict

DummyMock = TypedDict('DummyMock', {"return_value.classify_text.return_value": Any})
dm: DummyMock = {"return_value.classify_text.return_value": {}}

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

If you need this for many keys you can probably add all of them to the same TypedDict and make it total=False.

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