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

ConfigParser has incompatible type IO[Any]; expected TextIO #283

Closed
BoboTiG opened this issue Jun 10, 2016 · 9 comments · Fixed by #284
Closed

ConfigParser has incompatible type IO[Any]; expected TextIO #283

BoboTiG opened this issue Jun 10, 2016 · 9 comments · Fixed by #284

Comments

@BoboTiG
Copy link
Contributor

BoboTiG commented Jun 10, 2016

Hello,

This is a reference from python/mypy#1688.

I am testing mypy and cannot solve this error. I am using Python 3.5.1 and mypy 0.4.2 (same error with mypy 0.4.1). The snippet:

    #!/usr/bin/env python
    # coding: utf-8

    from configparser import ConfigParser


    def update_stats(ini_file: str, lines: int) -> None:
        ''' Statistics update. '''

        ini = ConfigParser()
        ini.read(ini_file)
        lines += ini.getint('stats', 'lines')
        ini.set('stats', 'lines', str(lines))
        ini.write(open(ini_file, 'w'))

        # Same error with io
        import io
        ini.write(io.open(ini_file, 'w'))


    update_stats('stats.ini', 10)

And stats.ini:

    [stats]
    lines = 2

Using the command line:

    $ mypy tests.py
    test.py: note: In function "update_stats":
    test.py:14: error: Argument 1 to "write" of "ConfigParser" has incompatible type IO[Any]; expected "TextIO"
    test.py:18: error: Argument 1 to "write" of "ConfigParser" has incompatible type IO[Any]; expected "TextIO"

Could you please tell me what is going wrong here? Thank you in advance :)

@JukkaL
Copy link
Contributor

JukkaL commented Jun 10, 2016

Likely the stub for configparser should accept IO[str] instead of TextIO.

@JukkaL
Copy link
Contributor

JukkaL commented Jun 10, 2016

@BoboTiG Would you like to contribute a fix?

@BoboTiG
Copy link
Contributor Author

BoboTiG commented Jun 10, 2016

Yes, of course. Thanks for giving me that chance :)
I will send a patch soon.

@gvanrossum
Copy link
Member

FWIW I've seen similar issues several times now. It's unfortunate that IO[str] and TextIO are not the same thing. Maybe we should make them the same, or else we should hunt down all uses of TextIO and change them to IO[str], or maybe we should just change open() and sys.std{in,out,err} to return TextIO?

@JukkaL
Copy link
Contributor

JukkaL commented Jun 10, 2016

This was discussed before. TextIO has a bunch of additional things not available in IO[x], because binary streams don't support them. My suggestion is to move everything to IO[x] and then let tools worry about how to handle stuff that should not be available in IO[bytes], for example.

@BoboTiG
Copy link
Contributor Author

BoboTiG commented Jun 10, 2016

Which is better? Completly remove the TextIO type or to specify both are the same?

@BoboTiG
Copy link
Contributor Author

BoboTiG commented Jun 10, 2016

OK, did not see you answer at time. Do you want I send a PR to replace all TextIO?

@JukkaL
Copy link
Contributor

JukkaL commented Jun 10, 2016

Let's start with fixing configparser to not use TextIO in arguments. Generally arguments should use IO[x] instead of TextIO, but return values may use TextIO without problems. The more general issue will likely need some more discussion.

@BoboTiG
Copy link
Contributor Author

BoboTiG commented Jun 10, 2016

Good, I am working on.

pgjones added a commit to pgjones/typeshed that referenced this issue Jun 11, 2016
Instead of using TextIO and BinaryIO use IO[str] and IO[bytes]
respectively to match the type returned by the open builtin. This
follows the recommendations in python#283.

The error that prompts this is reproduced via:
```
from email import message_from_file

with open('email') as file_:
    email = message_from_file(file_)
```
Argument 1 to "message_from_file" has incompatible type IO[Any]; expected "TextIO"
pgjones added a commit to pgjones/typeshed that referenced this issue Jun 11, 2016
Instead of using TextIO and BinaryIO use IO[str] and IO[bytes]
respectively to match the type returned by the open builtin. This
follows the recommendations in python#283.

The error that prompts this is reproduced via:
```
from email import message_from_file

with open('email') as file_:
    email = message_from_file(file_)
```
Argument 1 to "message_from_file" has incompatible type IO[Any]; expected "TextIO"
gvanrossum pushed a commit that referenced this issue Jun 11, 2016
* Change (Py3) email message payload type

The docstring of the Message get_payload method indicates that the
possible return types include None and bytes. Additionally the
set_payload method accepts bytes. Therefore I've changed the
PayloadType to include bytes and the get_payload return type to be an
Optional PayloadType.

* Change email (Py3) message_from_file IO types

Instead of using TextIO and BinaryIO use IO[str] and IO[bytes]
respectively to match the type returned by the open builtin. This
follows the recommendations in #283.

The error that prompts this is reproduced via:
```
from email import message_from_file

with open('email') as file_:
    email = message_from_file(file_)
```
Argument 1 to "message_from_file" has incompatible type IO[Any]; expected "TextIO"
ajnelson-nist added a commit to dfxml-working-group/dfxml_python that referenced this issue Aug 25, 2021
Every few years, I write a program that uses .print_dfxml(), and I get a
runtime error about type confusion between the writer argument needing
to be str but being bytes.  These error messages are sufficiently
infrequent and obtuse that it always feels like it takes too long to jog
my memory on how the matter was resolved last time.

Most recently, thsi came about because a program I was writing was using
tempfile.NamedTemporaryFile, but I forgot the opening mode by default is
"w+b".  This is now encoded in a pair of tests.

This patch adds a type annotation, following the prescription in this
Issue's comment thread to resolve mypy being confused about
typing.IO[str] versus typing.TextIO:
python/typeshed#283

Hopefully, users of static type-checkers in the future will be able to
prevent this same confusion.

Signed-off-by: Alex Nelson <[email protected]>
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 a pull request may close this issue.

3 participants