This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: mock_open does not support the use of 'seek()'
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, lisroach, mariocj89, michael.foord, nikle, rbcollins, xtreak
Priority: normal Keywords:

Created on 2021-05-10 11:32 by nikle, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg393378 - (view) Author: Niko (nikle) Date: 2021-05-10 11:32
Using 'mock_open' to unit test a code that uses the 'seek()' function for the file object does not work. This would be a great addition for mock_open functionality as this use case happens occasionally.

Test file contains:
def test_one(self):
    with patch("builtins.open", mock_open(read_data="#123")):
        script.main()

Example script:
def main():
    with open('some/file', 'r') as f:
        print(f.read(1))
        f.seek(0)
        print(f.read(1))

if __name__ == "__main__":
    main()

Output will be:
#
1

While the expected output is:
#
#
msg393383 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-05-10 12:47
See also https://bugs.python.org/issue25690 . https://github.com/nivbend/mock-open has support for seek.

As stated in the other issue supporting all operations of a filesystem might add more complexity.
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88268
2021-05-10 12:47:20xtreaksetversions: + Python 3.11, - Python 3.9
nosy: + lisroach, cjw296, xtreak, rbcollins, mariocj89, michael.foord

messages: + msg393383

components: + Library (Lib)
type: behavior -> enhancement
2021-05-10 11:32:27niklecreate