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

HttpMockSequence is missing close() method #2359

Open
bramp opened this issue Mar 19, 2024 · 1 comment
Open

HttpMockSequence is missing close() method #2359

bramp opened this issue Mar 19, 2024 · 1 comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@bramp
Copy link

bramp commented Mar 19, 2024

Environment details

  • OS type and version: macOS 14.3.1 (23D60)
  • Python version: Python 3.12.2
  • pip version: pip 24.0
  • google-api-python-client version: Version: 2.122.0

Steps to reproduce

  1. Use a HttpMockSequence to mock out the http client
  2. Create a service
  3. Call close() on the service
  4. AttributeError :(

Code example

from googleapiclient.http import HttpMockSequence
from googleapiclient.discovery import build

http = HttpMockSequence([
	({'status': '200'}, {})
])

with build('drive', 'v3', http=http) as service:
	pass

# or
# build('drive', 'v3', http=http).close()

Using build in a context manager is recommended by https://github.com/googleapis/google-api-python-client/blob/main/docs/start.md to ensure that service (and underlying http resources) are cleaned up.

Httplib2 has close() method that was added ~5 years ago.

Stack trace

Traceback (most recent call last):
  File "~/test.py", line 8, in <module>
    with build('drive', 'v3', http=http) as service:
  File "~/.venv/lib/python3.12/site-packages/googleapiclient/discovery.py", line 1422, in __exit__
    self.close()
  File "~/.venv/lib/python3.12/site-packages/googleapiclient/discovery.py", line 1429, in close
    self._http.close()
    ^^^^^^^^^^^^^^^^
AttributeError: 'HttpMockSequence' object has no attribute 'close'

I think HttpMockSequence is just missing the close() method. TBH I just started using this library, but after a bunch of searching I think I'm using it as recommended, and I'm surprised that I'm the first to stumbled across this / report this.

Thanks for any help, and I'm happy to send a PR fixing the issue.

@bramp
Copy link
Author

bramp commented Mar 19, 2024

FYI A workaround is to

http = HttpMockSequence([
	({'status': '200'}, {})
])
http.close = lambda: None

@parthea parthea added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants