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

Turn TextIOWrapper(buffer) into a protocol #11420

Merged
merged 16 commits into from
Feb 14, 2024

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Feb 14, 2024

Closes: #11418

@srittau
Copy link
Collaborator Author

srittau commented Feb 14, 2024

This could also use some regression tests, but I'm currently a bit short on time.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@AlexWaygood
Copy link
Member

AlexWaygood commented Feb 14, 2024

Why do we need all those empty __init__.py files in the test_cases directory? They appear to be causing some confusion for our regr_test.py script

@srittau
Copy link
Collaborator Author

srittau commented Feb 14, 2024

Why do we need all those empty __init__.py files in the test_cases directory? They appear to be causing some confusion for our regr_test.py script

Because otherwise mypy gets confused about check_io.pyi and typing/check_io.pyi. But renaming the latter should help as well.

@AlexWaygood
Copy link
Member

Why do we need all those empty __init__.py files in the test_cases directory? They appear to be causing some confusion for our regr_test.py script

Because otherwise mypy gets confused about check_io.pyi and typing/check_io.pyi. But renaming the latter should help as well.

Ugh. Yeah, renaming seems the easiest option there for now... thanks!

This comment has been minimized.

@srittau srittau marked this pull request as ready for review February 14, 2024 13:48
stdlib/io.pyi Outdated Show resolved Hide resolved
stdlib/io.pyi Outdated Show resolved Hide resolved
stdlib/io.pyi Show resolved Hide resolved

This comment has been minimized.

This comment has been minimized.

@AlexWaygood
Copy link
Member

Hmm, looks like we've been here before:

The differences that PR has to this PR are:

  • The read() method in the new protocol returns ReadableBuffer rather than bytes in that PR.
  • That PR overrides TextIOWrapper.detach() and TextIOWrapper.buffer() to return instances of the new protocol

@srittau
Copy link
Collaborator Author

srittau commented Feb 14, 2024

Hrm, cc @JelleZijlstra. Since this PR seem to fix the issues without adding to the primer output. I suggest to merge this first, and then have another look at Jelle's old PR.

@AlexWaygood
Copy link
Member

Hrm, cc @JelleZijlstra. Since this PR seem to fix the issues without adding to the primer output. I suggest to merge this first, and then have another look at Jelle's old PR.

That sounds reasonable to me, though if it would be more accurate to have the read() method return ReadableBuffer, I think it would be safe to make that change. That change should reduce the number of false-positives in user code, if anything.

This comment has been minimized.

This comment has been minimized.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up. The major remaining difference from my PR seems to be that you're not overriding buffer and detach. This helps to get rid of some of the primer hits.

def name(self) -> Any: ...
@property
def closed(self) -> bool: ...
def read(self, size: int = ..., /) -> ReadableBuffer: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def read(self, size: int = ..., /) -> ReadableBuffer: ...
def read(self, size: int, /) -> ReadableBuffer: ...

It always calls read with an argument. Note this change makes the protocol more permissive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment linking to that line in the source code?

stdlib/io.pyi Outdated Show resolved Hide resolved
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

streamlit (https://github.com/streamlit/streamlit)
- lib/tests/streamlit/web/server/component_request_handler_test.py:176:38: error: Argument 1 to "TextIOWrapper" has incompatible type "str"; expected "IO[bytes]"  [arg-type]
+ lib/tests/streamlit/web/server/component_request_handler_test.py:176:38: error: Argument 1 to "TextIOWrapper" has incompatible type "str"; expected "_WrappedBuffer"  [arg-type]

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. #11420 (comment) is optional.

@AlexWaygood
Copy link
Member

I think this also fixes #6061?

@srittau srittau linked an issue Feb 14, 2024 that may be closed by this pull request
@srittau srittau merged commit 4664986 into python:main Feb 14, 2024
57 checks passed
@srittau srittau deleted the textiowrapper-protocol branch February 14, 2024 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants