You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] On Windows, get_pager_file returns a typing.BinaryIO object NOT typing.TextIO #3740
Inside src/click/_termui_impl.py, the function get_pager_file delegates the creation of a tempfile to _tempfilepager, which returns a BinaryIO object of the opened file:
f=tempfile.NamedTemporaryFile(mode="wb", delete=False)
try:
yieldt.cast(t.BinaryIO, f), encoding, color
...
defget_pager_file(color: bool|None=None) ->t.Generator[t.TextIO, None, None]:
...
with_pager_contextmanager(color=color) as (stream, encoding, color):
# Split streams by capabilities rather than the abstract TextIO /# BinaryIO annotations: buffered text streams can be unwrapped to bytes,# while other streams are yielded as-is.wrapper: MaybeStripAnsi|None=Noneif_has_binary_buffer(stream):
# Text stream backed by a binary buffer.wrapper=MaybeStripAnsi(stream.buffer, color=color, encoding=encoding)
stream=wrappertry:
# Narrow the BinaryIO | TextIO union that _pager_contextmanager# yields; the caller writes text to the pager.yieldt.cast(t.TextIO, stream)
...
In the above yield t.cast(t.TextIO, stream) stream object will be BinaryIO, contradicting the TextIO annotation 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 with mode='w'
Hello @davidism
In case you ever read this issue. I must point out I didn't know this was a duplicate of #3731.
if you think it's not useful, close it.
Activity
H-Sorkatti commentedon Aug 3, 2026
Hello @davidism
In case you ever read this issue. I must point out I didn't know this was a duplicate of #3731.
if you think it's not useful, close it.