Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

typing: accept buffers in typing.IO.write#9084

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
JelleZijlstra wants to merge17 commits intopython:mainfromJelleZijlstra:typing

Conversation

@JelleZijlstra
Copy link
Member

Fixes#9082

@JelleZijlstra
Copy link
MemberAuthor

The mypy failures are a bug,python/mypy#14002.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@srittau
Copy link
Collaborator

This looks quite disruptive to me and I'm not sure whether it's worth it, considering the slightly legacy nature ofIO.

AlexWaygood reacted with thumbs up emoji

@JelleZijlstra
Copy link
MemberAuthor

Worth noting that most of the mypy-primer fallout is due to a mypy bug (python/mypy#14002).

@hauntsaninjahauntsaninja added the status: deferredIssue or PR deferred until some precondition is fixed labelNov 5, 2022
@hauntsaninja
Copy link
Collaborator

This seems useful, but given that I think I have a mypy fix, let's wait until it's released:python/mypy#14017

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@AlexWaygoodAlexWaygood removed the status: deferredIssue or PR deferred until some precondition is fixed labelFeb 8, 2023
Copy link
Member

@AlexWaygoodAlexWaygood left a comment

Choose a reason for hiding this comment

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

Looks like we can go back to this now, with the latest mypy release!

def__iter__(self:Self)->Self: ...
defwrite(self,data:str)->None: ...# type: ignore[override]
defwritelines(self,list:Iterable[str])->None: ...
defwritelines(self,list:Iterable[str])->None: ...# type: ignore[override] # https://github.com/python/mypy/issues/14002
Copy link
Member

Choose a reason for hiding this comment

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

We probably don't need thistype: ignore anymore (as well as several others), with the latest mypy release.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Cleaned up all comments referencing that issue.

defparse_headers(fp:io.BufferedIOBase,_class:Callable[[],email.message.Message]= ...)->HTTPMessage: ...

classHTTPResponse(io.BufferedIOBase,BinaryIO):
classHTTPResponse(io.BufferedIOBase,BinaryIO):# type: ignore[misc] # https://github.com/python/mypy/issues/14002
Copy link
Member

Choose a reason for hiding this comment

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

We do still need thistype: ignore, but not because of the mypy issue linked (which is now closed) -- we need it because the definitions ofwritelines() in the two base classes are incompatible. The same goes for all thetype: ignores inio.pyi and the one inlzma.pyi.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Updated these comments.

Comment on lines 218 to 225
@overload
defwrite(self:_TemporaryFileWrapper[str],s:str)->int: ...
@overload
defwrite(self:_TemporaryFileWrapper[bytes],s:ReadableBuffer)->int: ...
@overload
defwritelines(self:_TemporaryFileWrapper[str],lines:Iterable[str])->None: ...
@overload
defwritelines(self:_TemporaryFileWrapper[bytes],lines:Iterable[ReadableBuffer])->None: ...
Copy link
Member

Choose a reason for hiding this comment

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

These overloads might not work very well with pyright, which has different handling of self types to mypy (#9591). The same goes for the overloads on lines 401-408, and the overloads intyping.pyi. You might want to instead do something similar to what I did withre.Pattern andre.Match in#9592.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Changed, and added some testcases forIO.write.

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

Ping@JelleZijlstra, have you had a chance to take a look at my review feedback? :)

@JelleZijlstra
Copy link
MemberAuthor

Oops, I think I assumed this was still blocked. I will take a look soon!

AlexWaygood reacted with heart emoji

@github-actions

This comment has been minimized.

@JelleZijlstra
Copy link
MemberAuthor

hm, the new way to write the overloads makes subclassing much worse. Let me see if I can fix that.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

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

rich (https://github.com/Textualize/rich)+ rich/progress.py:173: error: Definition of "writelines" in base class "IOBase" is incompatible with definition in base class "IO"  [misc]psycopg (https://github.com/psycopg/psycopg)+ psycopg/psycopg/copy.py:464: error: Unused "type: ignore" commentcwltool (https://github.com/common-workflow-language/cwltool)+ cwltool/main.py:963: error: Unused "type: ignore[arg-type]" comment

@JelleZijlstra
Copy link
MemberAuthor

Seems like we have a choice between:

  1. One overload for AnyStr and another for Buffer: pyright acceptsIO.write(AnyStr), but mypy hitsIncorrect incompatible override report with self type + overload + TypeVar with constraints mypy#14866 and subclasses of IO classes that override thewrite/writelines method start complaining.
  2. One overload for str and another for bytes + Buffer: mypy is fine with more subclasses, but pyright rejectsIO.write(AnyStr).

I'm inclined to go with (1) because mypy's behavior is more clearly buggy than pyright's, but ideally we'd fix the mypy bug first.

@AlexWaygood
Copy link
Member

Now thatpython/mypy#14882 has been merged, I guess we have two options:

I'm easy either way :)

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

Reviewers

@AlexWaygoodAlexWaygoodAwaiting requested review from AlexWaygood

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Support buffers inIO[bytes].write

4 participants

@JelleZijlstra@srittau@hauntsaninja@AlexWaygood

[8]ページ先頭

©2009-2025 Movatter.jp