Changed tempfile.NamedTemporaryFile mode to w - #3739
Merged
Merged
Conversation
Author
|
@kdeldycke |
Collaborator
Ah. Sorry for the confusion. Maintainers are overloaded with noise these past few months so I wrongly flagged that. Let me review your proposal then. |
tempfile.NamedTemporaryFile mode to w
This was
linked to
issues
Aug 13, 2026
Collaborator
|
OK this is confirmed as a regression from #1572 . And it was not caught because |
Co-authored-by: H-Sorkatti <H.Sorkatti.B@Gmail.com>
kdeldycke
force-pushed
the
fix-tempfile-mode
branch
from
August 13, 2026 09:20
d920ccf to
a2ac583
Compare
Collaborator
|
Just added tests to cover the regression and added a changelog entry. Also added test for color handling. This PR is ready to be merged upstream. |
This was referenced Aug 13, 2026
Rowlando13
approved these changes
Aug 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Inside
src/click/_termui_impl.py, the functionget_pager_filedelegates the creation of a tempfile to_tempfilepager, which returns aBinaryIOobject of the opened file:Which is called by _pager_contextmanager in
In the end
get_pager_fileIn the above
yield t.cast(t.TextIO, stream)stream object will beBinaryIO, contradicting theTextIOannotation of the return value, all because it was created with the mode"wb"earlier.Then if a caller tries:
stream.write("some text")it will raise an exception:TypeError: a bytes-like object is required, not 'str'FIX:
In
_tempfilepager, create the temp file withmode='w'Closes #3740