Skip to content

Support TextIOWrapper.reconfigure() #3049

Closed

Description

@grantwwu

I'm getting a

"TextIO" has no attribute "reconfigure"

error for

sys.stdin.reconfigure(encoding="latin-1")

It's a new Python 3.7 feature. Was informed in Gitter that this hadn't been added to Typeshed yet and to open a ticket for it.

See https://docs.python.org/3/whatsnew/3.7.html#io

Activity

  1. srittau commented on Jun 11, 2019

    @srittau
    Collaborator

    Currently sys.stdin et al. are typed as TextIO, not TextIOWrapper. This would need to be changed as well, although I am not sure that this does not cause complications. But adding TextIOWrapper.reconfigure() would be welcome in any case.

  2. serhiy-storchaka commented on May 14, 2020

    @serhiy-storchaka
    Member

    For the following code

    import sys, io
    
    if isinstance(sys.stdout, io.TextIOWrapper) and sys.version_info >= (3, 7):
        sys.stdout.reconfigure(errors="replace")

    I get the error:

    reconfigure37.py:4: error: "TextIOWrapper" has no attribute "reconfigure"
    

    MyPy understand that sys.stdout is a TextIOWrapper instance.

  3. serhiy-storchaka commented on May 14, 2020

    @serhiy-storchaka
    Member

    What is worse -- I cannot silence the error by adding # type: ignore, because on Python 3.6 I get error: unused 'type: ignore' comment.

  4. grantwwu commented on May 14, 2020

    @grantwwu
    Author

    I haven't touched MyPy in many months but your platform check might be too complex for https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks to understand. Try splitting it into two ifs?

  5. srittau commented on May 14, 2020

    @srittau
    Collaborator

    FWIW, I just checked the following with mypy 0.770 using --warn-unused-ignores under both Python 3.6.9 and 3.7.4 and it works or me:

    import sys, io
    
    if isinstance(sys.stdout, io.TextIOWrapper) and sys.version_info >= (3, 7):
        sys.stdout.reconfigure(errors="replace")  # type: ignore

    That said, we'll gladly accept a PR that adds TextIOWrapper.reconfigure() :)

  6. serhiy-storchaka commented on May 18, 2020

    @serhiy-storchaka
    Member

    It is the same if split on two ifs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions