Fix patching of io.TextWrapper in tests#2489
Conversation
`patch("io.TextIOWrapper")` wouldn't actually cause
`io.TextIOWrapper()` to return the `StringIO` object created by the
test. This started to fail as soon as `content=` is non-empty.
0f837d3 to
9a1897c
Compare
JelleZijlstra
left a comment
There was a problem hiding this comment.
Can you clarify why the previous way didn't work? It looks to me like it should.
Co-authored-by: Jelle Zijlstra <[email protected]>
@JelleZijlstra, a very good question, which was a tough one to sort out! Indeed, in the In other words, this is a case of a common problem when patching and asserting library classes/functions: You can't be sure that the patched item won't in the future get extra invocations in addition to the one you're originally testing for. For some reason, patching only the As a conclusion from looking into this deeper, the modification to patching in the test function really belongs in #2484 since it's only the changes there which break the patching. So closing this PR. |
We need to "surgically" make sure only the intended invocation of `io.TextIOWrapper()` is patched, and other calls get through to the original implementation. See discussion in psf#2489
|
Thanks for the thorough explanation! |
Description
patch("io.TextIOWrapper")wouldn't actually causeio.TextIOWrapper()to return theStringIOobject created by the test. This didn't cause any problems though until I added a test in #2484 in whichcontent=is non-empty:In the debugger we can see that
io.TextIOWrapperused onsrc/black/__init__.py:842–844:is not the patched lambda:
With the changes in the PR, it is properly patched:
Checklist - did you ...