python / python/cpython

Unitest.mock.MagicMock raises a StopIteration - not following PEP 479

Open
#98,362 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Description

It looks like Unitest.mock.MagicMock raises a StopIteration when repeated calls to the mock are made and an iterable is exhausted. After Python 3.7+, due to PEP 479, this raises a Runtime Error. This makes unit tests that worked in Python 3.6 fail, including any list comprehensions on MagicMock results. These can be easily caught with a try/except, but it's a bit clunky.

According to PEP 479 instead of StopIteration MagicMock should just return return when the iterable is exhausted. See here: https://peps.python.org/pep-0479/

Please let me know if I missed something on my end. Thanks for looking into this.

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.

Research direction

Start with unittest.mock.MagicMock and reproduce repeated calls with an exhausted iterable, including use in a list comprehension. Read PEP 479 alongside the mock's iterable side-effect behavior; done means the intended exhausted-iterable behavior is established and covered by regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.