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

Open
#1,188 10 comments 27 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
devtools

Research direction

Start by reproducing the example with mypy, typeshed, and mock.Mock(), then read the discussion in issue #1152 for related context. No source file or test entry point is named; done would require a clearly defined supported way to specify the mock object's type without the reported invalid-assignment error, along with coverage for this example.

Written by the indexing model from the issue text.

Description

false-positive feature priority-1-normal

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.

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.