Skip to content

Type annotations for BlobClient class methods cause invalid type inference in the async client #19906

@tonybaloney

Description

@tonybaloney
  • Package Name: azure-storage-blob
  • Package Version: latest
  • Operating System: macOS
  • Python Version: Python 3.9

The async BlobClient class uses BlobClient (sync) as a mixin. The class method factories for instantiating the client, such as from_connection_string, from_blob_url have a type annotated return type as BlobClient, which is invalid:

https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py#L203-L206

The return type should be cls, using a TypeVar[T]. This ends up causing issues with type systems when you use the methods.

This simple example demonstrates the issue

from typing import Any, Dict
from azure.storage.blob.aio import (
    BlobClient,
)


async def download_blob(blob_url: str) -> Any:
    blob_client = BlobClient.from_blob_url(blob_url)  # Has type BlobClient instead of AsyncBlobClient
    response = await (await blob_client.download_blob()).readall()  # Works but raises type errors
    return response

Any type tool then causes issue because it thinks that blob_client is the sync client and not the async client.

screenshot 2021-07-22 at 09 09 42

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Service AttentionWorkflow: This issue is responsible by Azure service team.StorageStorage Service (Queues, Blobs, Files)bugThis issue requires a change to an existing behavior in the product in order to be resolved.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions