Skip to content

open() returns IO[str] instead of io.TextIOWrapper #1229

Closed
@jcrmatos

Description

@jcrmatos

Hello,

My env is Win7P+SP1 x64, Py 3.5.2 32b, Mypy 0.501.

I opened this issue (#3286) in mypy, but GVR told me to open it here because it was a "(complex) typeshed issue".

The var used in the with context is of type <class '_io.TextIOWrapper'>.

with open(pathname, encoding='utf-8') as f_in:
    print(type(f_in))

<class '_io.TextIOWrapper'>

But when I try to set it's type, mypy returns the following error

from io import TextIOWrapper
with open(pathname, encoding='utf-8') as f_in:  # type: TextIOWrapper
    print(type(f_in))

data_processing.py:917: error: Incompatible types in assignment (expression has type IO[Any], variable has type "TextIOWrapper")

If I try to use module typing's own TextIO type it returns

from typing import TextIO
with open(pathname, encoding='utf-8') as f_in:  # type: TextIO
    print(type(f_in))

data_processing.py:917: error: Incompatible types in assignment (expression has type IO[Any], variable has type "TextIO")

It only works if I do

from typing import IO
with open(pathname, encoding='utf-8') as f_in:  # type: IO[Any]
    print(type(f_in))

Best regards,

JM

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions