minio / minio-py

MinIO Client SDK for Python
https://docs.min.io/docs/python-client-quickstart-guide.html
Apache License 2.0
822 stars 318 forks source link

fix typing in remove_objects/upload_snowball_objects APIs #1389

Closed cuu508 closed 8 months ago

cuu508 commented 8 months ago

The delete_object_list argument in minio.api.remove_objects was annotated as Iterator[DeleteObject]. This means that mypy would complain if client code passes a list of DeleteObject-s to remove_objects.

But in practice passing in a list works: the delete_objects function converts the list to an iterator using itertools.chain.

I changed the type annotation to Iterable[DeleteObject]. This way, mypy is happy if we pass an iterator, and when we pass a list.

(And same thing in upload_snowball_objects)

Here's a small isolated testcase demonstrating the problem:

from __future__ import annotations

from collections.abc import Iterable
from typing import Iterator

def foo(data: Iterator[int]):
    pass

a = [1, 2, 3, 4]

foo(a)
foo(iter(a))

Running mypy on it results in:

experiment.py:13: error: Argument 1 to "foo" has incompatible type "list[int]"; expected "Iterator[int]"  [arg-type]
experiment.py:13: note: "list" is missing following "Iterator" protocol member:
experiment.py:13: note:     __next__

But if I change the type annotation from Iterator[int] to Iterable[int]:

from __future__ import annotations

from collections.abc import Iterable
from typing import Iterator

def foo(data: Iterable[int]):
    pass

a = [1, 2, 3, 4]

foo(a)
foo(iter(a))

then mypy is happy:

Success: no issues found in 1 source file