refactor(google-auth): refactor async AuthorizedSession to remove inheritance from aiohttp.ClientSession#16153
refactor(google-auth): refactor async AuthorizedSession to remove inheritance from aiohttp.ClientSession#16153marotta-j wants to merge 2 commits intogoogleapis:mainfrom
Conversation
…eritance from aiohttp.ClientSession
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the asynchronous Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the async AuthorizedSession to use composition over inheritance with aiohttp.ClientSession, which is the recommended approach. The changes are well-implemented, using an internal session and proxying methods to maintain the same interface. This also resolves an issue with the auth_request parameter and adds proper resource cleanup. I have one suggestion to simplify the __aexit__ method for better code clarity and to reduce duplication.
| async def __aexit__(self, *exc): | ||
| if self._auth_request_session is not None: | ||
| await self._auth_request_session.close() | ||
| return await self._session.__aexit__(*exc) |
There was a problem hiding this comment.
The implementation of __aexit__ duplicates logic from the close method for closing _auth_request_session. To improve maintainability and align with the pattern used in aiohttp.ClientSession (which just calls self.close() in its __aexit__), this method can be simplified to delegate the closing logic to self.close(). This makes the code cleaner and avoids redundancy.
async def __aexit__(self, *exc):
await self.close()
aiohttp discourages inheritance from aiohttp.ClientSession as noted in issue #15184 (and discussed here and here). This PR alters the async implementation of AuthorizedSession to hold aiohttp.ClientSession internally rather than inheriting.
This removes the DeprecationWarning while also keeping the same interface as aiohttp.ClientSession. Fixes #15184.
The previous implementation also did not respect the
auth_requestparameter which has now been fixed to mirror the sync version of AuthorizedSession. Explicit cleanup for the auth_request session was also added inclose().