psf / requests-html

Pythonic HTML Parsing for Humans™
http://html.python-requests.org
MIT License
13.64k stars 977 forks source link

Adding __aenter__ and __aexit__ Methods to AsyncHTMLSession #556

Open Curtidor opened 10 months ago

Curtidor commented 10 months ago

Changes Overview

Description

This merge request introduces the addition of __aenter__ and __aexit__ methods to the AsyncHTMLSession 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 the AsyncHTMLSession 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 the AsyncHTMLSession instance can be used within an asynchronous context to make an HTTP GET request, and the session is properly closed upon exiting the context.