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

memoryview is a context manager #2442

Merged
merged 1 commit into from
Sep 11, 2018

Conversation

Zac-HD
Copy link
Contributor

@Zac-HD Zac-HD commented Sep 11, 2018

Since Python 3.2, memoryview has been usable as a context manager, so this PR adds the appropriate methods to let Mypy know. Found while working on python-trio/trio#543.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Good catch! One remark though.

@@ -495,6 +495,8 @@ class memoryview(Sized, Container[int]):
ndim = ... # type: int

def __init__(self, obj: Union[bytes, bytearray, memoryview]) -> None: ...
def __enter__(self) -> memoryview: ...
def __exit__(self, exc_type, exc_value, exc_tb) -> None: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please annotate __exit__ with:

    def __exit__(self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]) -> bool: ...

@Zac-HD Zac-HD force-pushed the memoryview-contextmanager branch 2 times, most recently from d5add1a to 5d82e02 Compare September 11, 2018 11:10
@Zac-HD
Copy link
Contributor Author

Zac-HD commented Sep 11, 2018

Thanks - and done!

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Nearly. :)

@@ -495,6 +495,8 @@ class memoryview(Sized, Container[int]):
ndim = ... # type: int

def __init__(self, obj: Union[bytes, bytearray, memoryview]) -> None: ...
def __enter__(self) -> memoryview: ...
def __exit__(self, exc_type: Optional[BaseException], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]) -> bool: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

exc_type should be Optional[Type[BaseException]].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow, somehow I managed to drop the Type while copy-pasting and then manually deleted the extra brackets. Fixed now!

Since Python 3.2, __enter__ returns self and __exit__ calls self.release()
@srittau
Copy link
Collaborator

srittau commented Sep 11, 2018

Thank you! I will merge when the CI finishes successfully.

@Zac-HD
Copy link
Contributor Author

Zac-HD commented Sep 11, 2018

Hurray! Thanks for the fast and helpful reviews 😍

@srittau srittau merged commit 6afa610 into python:master Sep 11, 2018
@Zac-HD Zac-HD deleted the memoryview-contextmanager branch September 11, 2018 12:31
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this pull request Jan 23, 2019
Since Python 3.2, __enter__ returns self and __exit__ calls self.release()
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 this pull request may close these issues.

2 participants