classification
Title: make unittest.mock.mock_open honor next()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Anvil, cjw296, lisroach, mariocj89, michael.foord, miss-islington
Priority: normal Keywords: patch

Created on 2019-05-22 11:19 by Anvil, last changed 2019-05-23 10:52 by asvetlov. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13492 merged Anvil, 2019-05-22 11:23
PR 13521 merged miss-islington, 2019-05-23 10:04
Messages (3)
msg343177 - (view) Author: Damien Nadé (Anvil) * Date: 2019-05-22 11:19
While we can iterate on a mock_open handle, it is not actually possible to perform a next() on it.

My use case is the following: I have a file with a one-line header that I want to skip. So, roughly, my code is like

    with open(filename, 'r') as my_file:
        confirm_header(next(my_file))
        for line in my_file:
            do_something(line)

And while writing a unit test on this code, the handle returned by mock_open(read_data=my_read_data) returns a mock instead of the first line of my_read_data.
msg343283 - (view) Author: miss-islington (miss-islington) Date: 2019-05-23 10:03
New changeset 394119afc6611f17bac96f5ec6fefa00000ae795 by Miss Islington (bot) (Damien Nadé) in branch 'master':
bpo-37008: make mock_open handle able to honor next() (GH-13492)
https://github.com/python/cpython/commit/394119afc6611f17bac96f5ec6fefa00000ae795
msg343284 - (view) Author: miss-islington (miss-islington) Date: 2019-05-23 10:21
New changeset 7cc47e9c19b7d67c8f08df15a413d14cf69f45da by Miss Islington (bot) in branch '3.7':
bpo-37008: make mock_open handle able to honor next() (GH-13492)
https://github.com/python/cpython/commit/7cc47e9c19b7d67c8f08df15a413d14cf69f45da
History
Date User Action Args
2019-05-23 10:52:12asvetlovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-23 10:51:58asvetlovsetversions: + Python 3.7
2019-05-23 10:21:14miss-islingtonsetmessages: + msg343284
2019-05-23 10:04:04miss-islingtonsetpull_requests: + pull_request13436
2019-05-23 10:03:37miss-islingtonsetnosy: + miss-islington
messages: + msg343283
2019-05-22 12:34:54xtreaksetnosy: + cjw296, michael.foord, lisroach, mariocj89

versions: - Python 3.7
2019-05-22 11:23:54Anvilsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13407
2019-05-22 11:19:07Anvilcreate