Skip to content

Typing errors in blob storage documentation code #24661

@TobiRoby

Description

@TobiRoby

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

Metadata

Metadata

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.customer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions