Adding __aenter__ and __aexit__ Methods to AsyncHTMLSession #556
+37
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes Overview
__aenter__
method, returning the instance ofAsyncHTMLSession
within an asynchronous context.__aexit__
method, ensuring proper closing of the session upon context exit.test_async_context_manager
) to validate the functionality of the async context manager.Description
This merge request introduces the addition of
__aenter__
and__aexit__
methods to theAsyncHTMLSession
class, providing support for using the class as an asynchronous context manager. Additionally, a test suite has been created to verify the functionality of the async context manager. Also, some imports have been moved to better align with PEP 8 – style guide.Motivation
The introduction of the
__aenter__
and__aexit__
methods allows users of theAsyncHTMLSession
class to easily manage the lifecycle of the session within an asynchronous context. The__aenter__
method returns the instance itself, enabling seamless integration with asynchronous context management. On the other hand, the__aexit__
method ensures that the session is properly closed when exiting the context.Test Results
The new test suite (
test_async_context_manager
) has been executed successfully, validating the behavior of the async context manager. The test case confirms that theAsyncHTMLSession
instance can be used within an asynchronous context to make an HTTP GET request, and the session is properly closed upon exiting the context.