Anthony Flury <> added the comment:

No - it isn't related.

In the case of mock_open; it isn't intended to be a simple MagicMock - it is 
meant to be a mocked version of open, and so to be useful as a testing tool, it 
should emulate a file as much as possible.  

When a mock_open is created, you can provide an argument 'read_data' which is 
meant to be the data from your mocked file, so it is key that the dunder iter 
method actually returns an iterator. The mock_open implementation already 
provides special versions of read, readline and readlines methods which use the 
'read_data' initial value as the content.
Currently though the dunder iter method isn't set at all - so the returned 
value would currently be an empty iterator (which makes mock_open unable to be 
used to test idiomatic python : 

    def display(file_name):
        with open('a.txt', 'r') as fp:
            for line in fp:

As a trivial example the above code when mock_open is used will be equivalent 
of opening an empty file,  but this code : 

    def display(file_name):
        with open('a.txt', 'r') as fp:
            while True:
                line = readline(fp)
                if line == '':

Will work correctly with the data provided to mock_open.

Regardless of how and when #33236 is solved - a fix would still be needed for 
mock_open to make it provide an iterator for the mocked file.


Python tracker <>
Python-bugs-list mailing list

Reply via email to