8000 patch.dict(x, y) interpreted as invoking __call__, not __init__ · Issue #5118 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
8000

patch.dict(x, y) interpreted as invoking __call__, not __init__ #5118

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

Closed
rbtcollins opened this issue May 29, 2018 · 7 comments
Closed

patch.dict(x, y) interpreted as invoking __call__, not __init__ #5118

rbtcollins opened this issue May 29, 2018 · 7 comments

Comments

@rbtcollins
Copy link
Member
rbtcollins commented May 29, 2018

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

I think this is a mypy bug, happy to be sent to typeshed though.

I'm using mypy 0.6, and have the following code:

from unittest import TestCase
from unittest.mock import patch

class A(TestCase):

    @patch.dict("os.environ", {"PATH": os.path.expanduser("~/.local/bin")})
    @patch.object(moduleA, "a")
    @patch("subprocess.check_output")
    def test_missing(self, check_output, a):
        pass

This errors like so:
error: Too many arguments for "__call__" of "_patch_dict"

The typeshed stub for this is

    class _patch_dict:
...
        def __init__(self, in_dict: Any, values: Any = ..., clear: Any = ..., **kwargs: Any) -> None: ...
        def __call__(self, f: Any) -> Any: ...
...

    class _patcher:
        TEST_PREFIX = ...  # type: str
        dict = ...  # type: _patch_dict
...
    patch = ...  # type: _patcher

So mypy should be able to tell that this is instantiating an instance of _patch_dict with __init__ and then that instance is being called with one argument - the curried patcher next inwards in the stack.

@rbtcollins rbtcollins changed the title patch.dict interpreted as __call__ not __init__ patch.dict(x, y) interpreted as invoking __call__, not __init__ May 29, 2018
@li-dan
Copy link
Contributor
li-dan commented May 29, 2018

I think this is a bug in the stubs. patch.dict is the _patch_dict class itself, not an instance of _patch_dict, so the annotation should be # type: Type[_patch_dict], not # type: _patch_dict.

@gvanrossum
Copy link
Member

Can one of you submit a PR to typeshed?

@rbtcollins
Copy link
Member Author

Let me give it a spin

@rbtcollins
Copy link
Member Author

Should we also wrap dict in ClassVar ?

@JelleZijlstra
Copy link
Member

I think you can just do dict = _patch_dict; haven't tested though.

@rbtcollins
Copy link
Member Author

python/typeshed#2173

@rbtcollins
Copy link
Member Author

@JelleZijlstra you can in Python 3; for some reason that fails on Python 2 (see the typeshed CI job).

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

4 participants
0