Skip to content

Conversation

Copy link

Copilot AI commented May 30, 2025

Fixed mypy type checking error in sdk/openai/azure-openai/samples/images_aoai_quickstart.py where result.data was being accessed without proper null checking.

Issue

The sample code was attempting to index result.data[0] directly, but result.data has type Optional[list[Image]], which means it could be None. This caused the mypy error:

Value of type "Optional[list[Image]]" is not indexable [index]

Solution

Added proper null and length checking before accessing the list:

# Before (unsafe):
image_url = result.data[0].url

# After (type-safe):
if result.data and len(result.data) > 0:
    image_url = result.data[0].url
    # ... rest of image processing
else:
    print("No image data received from the API.")

This change:

  • Ensures type safety by checking if result.data is not None
  • Verifies the list has at least one element before indexing
  • Provides helpful feedback when no image data is available
  • Maintains the original functionality when data is present

Fixes #8.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] fix mypy error in sample Fix mypy error: add null check for Optional[list[Image]] in images sample May 30, 2025
Copilot AI requested a review from kristapratico May 30, 2025 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix mypy error in sample

2 participants