azure-sdk-for-python: Typing errors in blob storage documentation code

Hi,

I am receiving typing errors from mypy when following the blob storage examples in the official documentation.

It would be awesome, if the documentation can provide an example with no type errors 😃

This is the code I am type checking (copy & pasted from the linked documentation):

import os
import uuid

from azure.storage.blob import BlobServiceClient

# Create the BlobServiceClient object which will be used to create a container client
blob_service_client: BlobServiceClient = BlobServiceClient.from_connection_string(
    "connect_str"
)
# Create a unique name for the container
container_name = str(uuid.uuid4())

# Create a local directory to hold blob data
local_path = "./data"
os.mkdir(local_path)

# Create a file in the local data directory to upload and download
local_file_name = str(uuid.uuid4()) + ".txt"
upload_file_path = os.path.join(local_path, local_file_name)

# Write text to the file
file = open(upload_file_path, "w")
file.write("Hello, World!")
file.close()

# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(
    container=container_name, blob=local_file_name
)

print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)

# Upload the created file
with open(upload_file_path, "rb") as data:
    blob_client.upload_blob(data)

download_file_path = os.path.join(
    local_path, str.replace(local_file_name, ".txt", "DOWNLOAD.txt")
)
print("\nDownloading blob to \n\t" + download_file_path)

with open(download_file_path, "wb") as download_file:
    download_file.write(blob_client.download_blob().readall())

I receive 2 errors when applying mypy:

test.py:35: error: Argument 1 to "upload_blob" of "BlobClient" has incompatible type "BufferedReader"; expected "Iterable[str]"
test.py:35: note: Following member(s) of "BufferedReader" have conflicts:
test.py:35: note:     Expected:
test.py:35: note:         def __iter__(self) -> Iterator[str]
test.py:35: note:     Got:
test.py:35: note:         def __iter__(self) -> Iterator[bytes]
test.py:43: error: Argument 1 to "write" of "BufferedWriter" has incompatible type "Union[bytes, str]"; expected "Union[bytes, Union[bytearray, memoryview, array[Any], mmap, _CData, PickleBuffer]]"
Found 2 errors in 1 file (checked 1 source file)

environment python: 3.10.4 azure-storage-blob: 12.12.0 mypy: 0.960

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 18 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Is there an estimated release date for 12.14.0? Apache Airflow uses azure-storage-blob for the Azure provider and currently we are unable to update package version due to the typing problem. It seems like you already fixed it but have not yet release.

https://github.com/apache/airflow/pull/25426