Skip to content
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

Unexpected behavior with io.TextIO #3306

Closed
kmarekspartz opened this issue May 2, 2017 · 2 comments
Closed

Unexpected behavior with io.TextIO #3306

kmarekspartz opened this issue May 2, 2017 · 2 comments

Comments

@kmarekspartz
Copy link

Dropped my notes / examples / Dockerfile into https://gist.github.com/zeckalpha/c0bed39b7de8dee64e0c895b852a850d

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:

io.TextIO

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.

io.TextIOBase

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 :)

io.TextIOWrapper

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.

typing.TextIO

from typing import TextIO as TIO

Works great, but type(sys.stdin) is not TextIO in Python 3.6.

Related issues with TextIO:

#2111
#2337
#266
https://github.com/python/mypy/issues/1462
python/typeshed#163

@gvanrossum
Copy link
Member

Good notes. These issues could all be fixed by changes to typeshed -- can you file an issue (or issues) there?

@kmarekspartz
Copy link
Author

Filed as python/typeshed#1240.

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

No branches or pull requests

2 participants