Unitest.mock.MagicMock raises a StopIteration - not following PEP 479
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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