-
-
Notifications
You must be signed in to change notification settings - Fork 98
feat(client): implement context manager protocol #533
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -127,7 +127,7 @@ def close(self) -> None: | |||||
| in future versions. | ||||||
| """ | ||||||
|
|
||||||
| def __enter__(self) -> "Response": ... | ||||||
| def __enter__(self) -> Any: ... | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type of
Suggested change
|
||||||
| def __exit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> None: ... | ||||||
| def __str__(self) -> str: ... | ||||||
|
|
||||||
|
|
@@ -204,7 +204,7 @@ def close( | |||||
| * `reason` - An optional reason for closing. | ||||||
| """ | ||||||
|
|
||||||
| def __enter__(self) -> "WebSocket": ... | ||||||
| def __enter__(self) -> Any: ... | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type of
Suggested change
|
||||||
| def __exit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> None: ... | ||||||
| def __str__(self) -> str: ... | ||||||
|
|
||||||
|
|
@@ -463,6 +463,9 @@ def get( | |||||
| ``` | ||||||
| """ | ||||||
| ... | ||||||
|
|
||||||
| def __enter__(self) -> Any: ... | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| def __exit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> None: ... | ||||||
|
|
||||||
|
|
||||||
| def delete( | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -585,6 +585,21 @@ impl Client { | |
| } | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl Client { | ||
| #[inline] | ||
| async fn __aenter__(slf: Py<Self>) -> PyResult<Py<Self>> { | ||
| Ok(slf) | ||
| } | ||
|
|
||
| #[inline] | ||
| async fn __aexit__(&self, _exc_type: Py<PyAny>, _exc_val: Py<PyAny>, _traceback: Py<PyAny>) { | ||
| // TODO: Implement connection closing logic if necessary. | ||
| } | ||
|
Comment on lines
+596
to
+598
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
Comment on lines
+588
to
+599
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // ===== impl BlockingClient ===== | ||
|
|
||
| #[pymethods] | ||
| impl BlockingClient { | ||
| /// Creates a new blocking Client instance. | ||
|
|
@@ -729,3 +744,22 @@ impl BlockingClient { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl BlockingClient { | ||
| #[inline] | ||
| fn __enter__(slf: PyRef<Self>) -> PyRef<Self> { | ||
| slf | ||
| } | ||
|
|
||
| #[inline] | ||
| fn __exit__<'py>( | ||
| &self, | ||
| _py: Python<'py>, | ||
| _exc_type: &Bound<'py, PyAny>, | ||
| _exc_value: &Bound<'py, PyAny>, | ||
| _traceback: &Bound<'py, PyAny>, | ||
| ) { | ||
| // TODO: Implement connection closing logic if necessary. | ||
| } | ||
|
Comment on lines
+756
to
+764
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
Comment on lines
+748
to
+765
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type hints for
__aenter__and__aexit__can be more specific.__aenter__should return an instance of the class, so its return type should be"Client". The__aexit__method in the Rust implementation doesn't return a value, which corresponds toNonein Python.