Skip to content

Wrong stub for fileinput.input() #7922

Closed

Description

@Molkree

@JelleZijlstra:

the stubs appear to be wrong here:

openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ...,

Only the openhook argument constrains the TypeVar, but it has a default, and if the argument is not given there's nothing for the type checker to use to decide on the value of the TypeVar. The typeshed stub should be changed to add a separate overload where openhook is not given.

microsoft/pyright#3494 (comment)

Activity

  1. AlexWaygood commented on May 22, 2022

    @AlexWaygood
    Member

    The stubs at the moment look quite buggy indeed! From my reading of the source code, I don't think it's actually the openhook parameter at all that determines whether the object returned is of type FileInput[str] or FileInput[bytes]. Instead, it's the value passed into the mode parameter. (You can see this by looking at the FileInput._readline method, which is called from FileInput.__next__.)

    IO modes can be tricky to type in stub files, but it should be possible.

  2. added a commit that references this issue on May 24, 2022
    860cd4a
  3. added a commit that references this issue on May 24, 2022
    d950ec3
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions