patch.dict(x, y) interpreted as invoking __call__, not __init__ #5118
Copy link
Copy link
Closed
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]patch.dict interpreted as __call__ not __init__[/-][+]patch.dict(x, y) interpreted as invoking __call__, not __init__[/+]li-dan commentedon May 29, 2018
I think this is a bug in the stubs.
patch.dictis the_patch_dictclass itself, not an instance of_patch_dict, so the annotation should be# type: Type[_patch_dict], not# type: _patch_dict.gvanrossum commentedon May 29, 2018
Can one of you submit a PR to typeshed?
rbtcollins commentedon May 29, 2018
Let me give it a spin
rbtcollins commentedon May 29, 2018
Should we also wrap dict in ClassVar ?
JelleZijlstra commentedon May 29, 2018
I think you can just do
dict = _patch_dict; haven't tested though.rbtcollins commentedon May 29, 2018
python/typeshed#2173
rbtcollins commentedon May 30, 2018
@JelleZijlstra you can in Python 3; for some reason that fails on Python 2 (see the typeshed CI job).