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

Need a way to specify types for mock objects (e.g. mock.Mock()) #1188

Open
timabbott opened this issue Jan 28, 2016 · 8 comments
Open

Need a way to specify types for mock objects (e.g. mock.Mock()) #1188

timabbott opened this issue Jan 28, 2016 · 8 comments
Labels
false-positive mypy gave an error on correct code feature priority-1-normal

Comments

@timabbott
Copy link

Example test case code here:

if True:
    from pycurl import Curl, version_info
else:
    from mock import Mock
    Curl = Mock()
    version_info = Mock()

Posting this in mypy, since I don't think it's possible to specify a typeshed type for mock.Mock that won't throw an error; e.g. if stubs say mock.Mock is an Any, you get /home/tabbott/foo.py:7: error: Invalid assignment target

Reporting as separate from #1152 since this might be worth solving even if that is not, since most every use of mock.Mock() will have this problem.

@JukkaL JukkaL added the feature label Feb 4, 2016
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 4, 2016

Here is one way of supporting this:

  • Make mock.Mock same as Any in the stubs.
  • Allow assigning a value of type Any to an arbitrary target, including to a class or a module.

Another approach would be to special case Mock, but the above idea seems more general and not very problematic.

@timabbott
Copy link
Author

Yeah, I think that approach makes a lot of sense.

@ddfisher ddfisher modified the milestones: Future, 0.4.0 Mar 1, 2016
@gvanrossum gvanrossum removed this from the 0.5 milestone Mar 29, 2017
@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 18, 2018
@kentwongg
Copy link

kentwongg commented Jun 22, 2018

I'm writing unit tests and this is a real nuisance for PEP8 compliance.

example code:

class FactoryHandler:
"""
:param worker:
:type worker:
"""

class TestFactoryHandler
    def test_something(self):
         worker = MagicMock()
         factory = FactoryHandler(worker)

PEP8 Type check will throw a warning saying it expected a type worker but got type magic mock instead.
As far as I know, all I can do is # no inspection the line.

@gvanrossum
Copy link
Member

What is "PEP8 Type check"?

Note that we waffled a bit on this -- up till mypy 0.610 there was no typeshed definition for Mock. Then we added one, but we found it was too noisy, and we rolled it back, instead changing it so that we have Mock: Any in mock.pyi.

See also python/typing#567. (Maybe this is the feature Tim was looking for?)

@MatanRubin
Copy link

Any updates on this? Running Mypy on any test that involves mocks is painful...

@gvanrossum
Copy link
Member

gvanrossum commented Apr 11, 2019 via email

@MatanRubin
Copy link

@gvanrossum, after re-examining the behavior I'm experiencing, I think it's related to this issue but not exactly the same.

Opened a separate issue to track what I'm seeing: #6713

@bandophahita
Copy link

bandophahita commented Mar 23, 2023

Would/Could using Protocols work to annotate mocked objects?

class MyMockedObjectProtocol(MockProtocol, MyObjectProtocol, Protocol):
    ...

mymock: MyMockedObjectProtocol = Mock()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive mypy gave an error on correct code feature priority-1-normal
Projects
None yet
Development

No branches or pull requests

7 participants