-
Notifications
You must be signed in to change notification settings - Fork 421
refactor(python): update Python Cookbook to Async, add missing methods & Py Bug Fixes (fixes #130, #129, #118) #138
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
base: main
Are you sure you want to change the base?
Conversation
…onMetadata, list_sessions(), delete_session()
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.
Pull request overview
This PR comprehensively refactors the Python SDK to use async patterns, implements missing session management methods (list_sessions() and delete_session()), and fixes Python 3.8 compatibility issues. The changes address three GitHub issues (#130, #129, #118) that reported broken cookbook examples, missing SDK methods, and Python 3.8 syntax errors.
Changes:
- Added
list_sessions()anddelete_session()methods to the CopilotClient with proper async patterns and camelCase-to-snake_case conversion - Fixed Python 3.8 compatibility by adding
from __future__ import annotationsto client.py and e2e test files - Converted all 5 cookbook recipes from synchronous to async patterns with proper
asyncio.run(),await, and event handling - Updated ruff configuration to ignore Python 3.8-incompatible style rules
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| python/pyproject.toml | Added Python 3.13/3.14 classifiers, configured ruff to ignore Python 3.8-incompatible rules |
| python/copilot/types.py | Added SessionMetadata TypedDict, changed type hints to Optional[X] for compatibility |
| python/copilot/client.py | Added future annotations import, implemented list_sessions() and delete_session() methods |
| python/copilot/init.py | Exported SessionMetadata type in public API |
| python/e2e/test_tools.py | Added future annotations import for Python 3.8 compatibility |
| python/e2e/test_mcp_and_agents.py | Added future annotations import for Python 3.8 compatibility |
| cookbook/python/recipe/requirements.txt | Fixed SDK path from ../.. to ../../../python |
| cookbook/python/recipe/pr_visualization.py | Converted to async with proper event handling and 300s timeout |
| cookbook/python/recipe/persisting_sessions.py | Converted to async, added list_sessions() and delete_session() examples |
| cookbook/python/recipe/multiple_sessions.py | Converted to async with send_and_wait() |
| cookbook/python/recipe/managing_local_files.py | Converted to async with proper event attribute access |
| cookbook/python/recipe/error_handling.py | Converted to async error handling patterns |
| cookbook/python/recipe/README.md | Updated documentation with async patterns and corrected SDK path |
| cookbook/python/pr-visualization.md | Updated documentation to match async code examples |
| cookbook/python/persisting-sessions.md | Added examples for new list_sessions() and delete_session() methods |
| cookbook/python/multiple-sessions.md | Updated to show async patterns and new session management methods |
| cookbook/python/managing-local-files.md | Updated to show async patterns with proper event handling |
| cookbook/python/error-handling.md | Comprehensive async error handling documentation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| client.start() | ||
| # Create Copilot client | ||
| client = CopilotClient({"log_level": "error"}) | ||
| await client.start() |
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.
I was working quite similar changes on my local workarea to get this example run. Looks good & approved.
About-face
This PR fixes critical Python SDK bugs, updates all cookbook examples to use proper async patterns, implements missing session management methods, and ensures full Python 3.8-3.14 compatibility.
Issues Fixed
list_sessions()anddelete_session()methodstuple[str, int]syntax errorKey Changes
1. SDK Enhancements (
python/copilot/)list_sessions()- ReturnsList[SessionMetadata]with session details (ID, creation time, modification time, summary)delete_session(session_id)- Permanently deletes a session and its dataSessionMetadataTypedDict - Proper type definitions for session metadata returned bylist_sessions()from __future__ import annotationstoclient.pyX | NonetoOptional[X]for Python 3.8/3.9 compatibilitySessionMetadatato public API in__init__.py2. Cookbook Recipe Fixes (All 5 files)
asyncio.run(main())andawaitproperly.typeand.event.data.content(not dict access)wait_for_idle(), usingsend_and_wait()insteaderror_handling.py- Proper exception handling with async patternsmanaging_local_files.py- File organization with async session handlingmultiple_sessions.py- Multi-session management examplespersisting_sessions.py- Session persistence withlist_sessions()anddelete_session()examplespr_visualization.py- Fixed 60s timeout → 300s timeout for complex GitHub API operations3. Documentation Updates
list_sessions()anddelete_session()in documentationpr_visualization.mdwith proper timeout configuration4. Code Quality & Compatibility
pyproject.tomlto ignore Python 3.8 incompatible style rules (UP045, UP007)from __future__ import annotationse2e/)test_mcp_and_agents.pyandtest_tools.pyTesting
python -m ruff check- All SDK and e2e files pass lintingpython -m pytest- 15 unit tests passingpython -m py_compile- All source files compile successfullypython managing_local_files.py, etc.Files Modified
Breaking Changes
None - All changes are backward compatible.
Benefits
Developers can now:
await client.list_sessions()await client.delete_session(session_id)Checklist