[et_xmlfile] Complete the writer_cm annotation#16075
Conversation
This comment has been minimized.
This comment has been minimized.
| class xmlfile: | ||
| encoding: str | ||
| writer_cm: Incomplete | ||
| writer_cm: _GeneratorContextManager[tuple[ET._FileWrite, str]] | None |
There was a problem hiding this comment.
I think we can actually type the first tuple element a bit more accurate by using:
| writer_cm: _GeneratorContextManager[tuple[ET._FileWrite, str]] | None | |
| writer_cm: _GeneratorContextManager[tuple[SupportsWrite[str], str]] | None |
(SupportsWrite must be imported from _typeshed.) _FileWrite is a type alias, of which one element is SupportsWrite. But the other elements of this type alias don't make much sense if you look at how its used:
This will actually just call the writer with a str.
There was a problem hiding this comment.
_get_writer yields the bound write method itself, and _IncrementalFileWriter calls it directly (self._file(...)), so I went with Callable[[str], object] instead of SupportsWrite[str]. Aligned _IncrementalFileWriter.__init__ too, it gets the same value.
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
xmlfile.writer_cmwasIncomplete. It is initialised toNonein__init__and assigned the result of the@contextmanager-decoratedincremental_tree._get_writerinxmlfile.__enter__, so its type is_GeneratorContextManager[tuple[ET._FileWrite, str]] | None.The context manager yields
(write, encoding): the first element is passed straight to_IncrementalFileWriter, whoseoutput_fileparameter is already typed asET._FileWrite, and the second is the encodingstr.Verified locally with stubtest, mypy, and pyright.