Skip to content

sphinx.util.logging: Make write() methods return int #13595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

srittau
Copy link

@srittau srittau commented May 26, 2025

Purpose

In typeshed, we're currently switching from our old custom protocol _typeshed.SupportsWrite to the new Writer protocol (in Python 3.14's io module and backported to typing_extensions starting with 4.14.0). One incompatibility is that Writer.write() expects an int return type, while SupportsWrite.write() was using object. This leads to a few new type errors in Sphinx (as pointed out by mypy_primer).

This PR fixes upcoming type check errors in Sphinx, but also makes the two logging writer classes more compatible by returning the number of written bytes from write().

Does this require an entry in CHANGES.rst or are the classes in util internal-facing?

References

See python/typeshed#14149 for background.

Comment on lines +586 to 594
def write(self, data: str) -> int:
try:
self.stream.write(data)
return self.stream.write(data)
except UnicodeEncodeError:
# stream accept only str, not bytes. So, we encode and replace
# non-encodable characters, then decode them.
self.stream.write(
return self.stream.write(
data.encode(self.encoding, 'replace').decode(self.encoding)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srittau what is the return value here meant to mean? In one branch it is the number of characters written, in the other it is the number of bytes. This doesn't make sense. Please can we keep None as a valid return value for the (small-p) protocol?

A

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typing.Writer protocol (as well as the legacy typing.IO pseudo-protocol) require an int return type. If you keep the None type here, you will need to # type: ignore this.

@AA-Turner AA-Turner closed this Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants