[docutils] Improve types in docutils.io - #16260
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
JelleZijlstra
left a comment
There was a problem hiding this comment.
Are the mypy-primer hits in Sphinx expected?
| encoding: Incomplete | ||
| encoding_errors: Incomplete | ||
| decoding_errors: Incomplete | ||
| destination: TextIO | BinaryIO | Literal[False] |
There was a problem hiding this comment.
Shouldn't this be SupportsWrite[str] | SupportsWrite[bytes] | Literal[False] to mirror the constructor parameter?
There was a problem hiding this comment.
Done in 4cec7a9, and dropped the now unused BinaryIO import.
Whatever is passed to __init__ is stored, so mirror the parameter with SupportsWrite[str] | SupportsWrite[bytes] | Literal[False] rather than the concrete TextIO | BinaryIO. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
Yes, they're expected. docutils 0.23 annotates |
|
Diff from mypy_primer, showing the effect of this PR on open source code: sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/util/docutils.py: note: In member "write" of class "SphinxFileOutput":
+ sphinx/util/docutils.py:457:21: error: Argument 1 of "write" is incompatible with supertype "docutils.io.FileOutput"; supertype defines the argument type as "str | bytes" [override]
+ sphinx/util/docutils.py:457:21: note: This violates the Liskov substitution principle
+ sphinx/util/docutils.py:457:21: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
+ sphinx/util/docutils.py: note: At top level:
+ sphinx/util/docutils.py:473: error: Unused "type: ignore" comment [unused-ignore]
+ sphinx/util/docutils.py: note: In member "write" of class "SphinxFileOutput":
+ sphinx/util/docutils.py:473:16: error: Incompatible return value type (got "str | bytes", expected "str") [return-value]
+ sphinx/util/docutils.py:473:16: note: Error code "return-value" not covered by "type: ignore[no-untyped-call]" comment
|
Several attributes and parameters of the
docutils.ioinput/output classes wereIncompleteor unannotated although docutils annotates them inline. This types theencoding,error_handler,destination_pathandautocloseattributes ofOutput/FileOutput, theErrorOutputattributes, the path/encoding/mode parameters of theFileInputandFileOutputconstructors, and theread()/write()methods ofStringInput,StringOutputandFileOutput, following the annotations in docutils 0.23.Agent used: Claude Code