8000 Wrong stub for `fileinput.input()` · Issue #7922 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Wrong stub for fileinput.input() #7922

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

Closed
Molkree opened this issue May 22, 2022 · 1 comment · Fixed by #7934
Closed

Wrong stub for fileinput.input() #7922

Molkree opened this issue May 22, 2022 · 1 comment · Fixed by #7934

Comments

@Molkree
Copy link
Contributor
Molkree commented May 22, 2022

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

@AlexWaygood
Copy link
Member
AlexWaygood commented May 22, 2022

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.

JelleZijlstra added a commit to JelleZijlstra/typeshed that referenced this issue May 24, 2022
Fixes python#7922, part of python#7928.

Also discovered an apparent CPython bug in the process: python/cpython#93157
AlexWaygood pushed a commit that referenced this issue May 24, 2022
* fileinput: Fix TypeVar usage

Fixes #7922, part of #7928.
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.

2 participants
0