I've been having trouble adding types to:
from sys import stdin, stdout
... # import something as TIO... See below.
def main(input_io: TIO=stdin, output_io: TIO=stdout) -> None:
for line in input_io.readlines():
output_io.write(line)
if __name__ == '__main__':
main()
Depending on which class is used for the typing we get different results:
from io import TextIO as TIO
Type checks but fails at runtime with:
ImportError: cannot import name 'TextIO'
I'd expect this one to fail during type checking. This is a bug, I think. There seems to be issues with https://github.com/python/typeshed/blob/397f99836842494de2e9561001d16cbe64b3b937/stdlib/3/io.pyi and I suspect a change there (possibly breaking it out to minor versions of Python 3) would catch this.
from io import TextIOBase as TIO
Fails at type check:
error: Incompatible types in assignment (expression has type "TextIO", variable has type "TextIOBase")
error: Argument 1 to "write" of "TextIOBase" has incompatible type "bytes"; expected "str"
This is okay, but a poor error message, e.g. "Did you mean typing.TextIO?" might be a better error message. A stretch goal :)
from io import TextIOWrapper as TIO
Fails at type check:
error: Incompatible types in assignment (expression has type "TextIO", variable has type "TextIOWrapper")
Also okay.
from typing import TextIO as TIO
Works great, but type(sys.stdin) is not TextIO in Python 3.6.
Related issues with TextIO
:
python/mypy#2111 python/mypy#2337 python/mypy#266 https://github.com/python/mypy/issues/1462 python/typeshed#163