Fixing linting issues in hosting/core/app - #488
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors parts of the hosting-core app layer (routing/state initialization), OAuth authorization handlers, and streaming response helpers to improve type safety (modern union syntax), strengthen runtime validation in OAuth flows, and standardize token-return behavior.
Changes:
- Modernized type annotations across app/OAuth/streaming components (e.g.,
T | None,TYPE_CHECKINGusage). - Hardened OAuth flow handling by validating required turn-state objects (e.g.,
UserTokenClient,ClaimsIdentity.aud) and adding clearer error paths/logging for missing agentic tenant IDs. - Standardized token handling so OAuth helper methods return
TokenResponseobjects instead ofNone.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/streaming/streaming_response.py | Adds TYPE_CHECKING-based typing improvements for TurnContext references in streaming response handling. |
| libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/oauth/_handlers/connector_user_authorization.py | Standardizes OBO helper to always return a TokenResponse. |
| libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/oauth/_handlers/agentic_user_authorization.py | Adds explicit tenant-id validation + logging for agentic token retrieval. |
| libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/oauth/_handlers/_user_authorization.py | Improves runtime validation for required OAuth turn-state keys and modernizes optional typing syntax. |
| libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/oauth/_handlers/_authorization_handler.py | Enforces presence of auth_handler_settings when constructing a handler from settings. |
| libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py | Refactors state factory/storage usage, adds deprecation marker for env var parsing helper, and modernizes type hints. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py:801
turn_state_factoryappears to have been renamed toset_turn_state_factory. Ifturn_state_factorywas part of the public fluent/decorator API, this is a breaking change for existing consumers. Consider keepingturn_state_factoryas a deprecated alias that forwards toset_turn_state_factoryto preserve backwards compatibility while still guiding users to the new name.
def set_turn_state_factory(self, func: Callable[[], StateT]):
"""
Custom Turn State Factory
"""
logger.debug(f"Setting custom turn state factory: {func.__name__}")
2fa4ef9
into
main
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py:803
- Renaming the public
AgentApplication.turn_state_factorydecorator toset_turn_state_factoryis a breaking API change for existing callers (even if internal uses were updated). Consider keeping a deprecatedturn_state_factoryshim that forwards toset_turn_state_factory, and add explicit return type annotations so decorator-style usage stays type-safe.
def set_turn_state_factory(self, func: Callable[[], StateT]):
"""
Custom Turn State Factory
"""
logger.debug(f"Setting custom turn state factory: {func.__name__}")
self._turn_state_factory = func
return func
This pull request introduces several improvements and refactorings across the authentication, agent application, and streaming components. The main focus is on improving type safety and error handling, modernizing type hints, and clarifying the initialization and configuration logic. Additionally, some deprecated methods are flagged, and logging is enhanced for better debugging.
Agent Application and State Management:
agent_application.pyto use modern union syntax (e.g.,Type | None), improving code clarity and type safety._storageattribute instead of repeatedly accessingself._options.storage, reducing potential errors and code duplication. [1] [2] [3] [4]StateTtype, and renamed the setter method toset_turn_state_factoryfor clarity. [1] [2]parse_env_vars_configurationas deprecated, guiding users to a new method for loading configuration from the environment.OAuth and Authorization Handling:
UserTokenClient,ClaimsIdentitywith 'aud' claim) are missing in the turn state, preventing subtle bugs. [1] [2]auth_handler_settingsif noauth_handleris provided during authorization handler construction.str | Noneinstead ofOptional[str]). [1] [2] [3]TokenResponseobject is always returned (neverNone) to standardize downstream handling.Streaming Response Improvements:
TYPE_CHECKINGimports and improved type annotations forTurnContextinstreaming_response.py, enhancing static analysis and code clarity. [1] [2] [3] [4]These changes collectively improve maintainability, type safety, and error transparency across the codebase.