classification
Title: Hide __enter__ calls in mock_open
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: CendioOssman, michael.foord
Priority: normal Keywords:

Created on 2021-06-24 11:08 by CendioOssman, last changed 2021-06-26 15:05 by terry.reedy.

Messages (2)
msg396469 - (view) Author: Pierre Ossman (CendioOssman) Date: 2021-06-24 11:08
I'd like to write this test case:

  with patch('builtins.open') as pyopen:
    mock_open(pyopen, read_data="foo")
    run()
    pyopen.assert_has_calls([call("filename", "wt"),
                             call().write("gazonk"),
                             call().close()])

and I shouldn't have to care if the code is written like this:

  def run():
    f = open("filename", "wt")
    try:
      write("gazonk")
    finally:
      f.close()

or like this:

  def run():
    with open("filename", "wt") as f:
      write("gazonk")
msg396470 - (view) Author: Pierre Ossman (CendioOssman) Date: 2021-06-24 11:09
Also see Issue44185 for __exit__.
History
Date User Action Args
2021-06-26 15:05:24terry.reedysetnosy: + michael.foord
2021-06-24 11:09:22CendioOssmansetmessages: + msg396470
2021-06-24 11:08:55CendioOssmancreate