Код: Выделить всё
def chunk(data, chunk_size: int):
yield from (data[i : i + chunk_size] for i in range(0, len(data), chunk_size))
Код: Выделить всё
def chunk[T](data: Sequence[T], chunk_size: int) -> Generator[Sequence[T]]:
yield from (data[i : i + chunk_size] for i in range(0, len(data), chunk_size))
Код: Выделить всё
def print_bytes(b: bytes) -> None: ...
Код: Выделить всё
error: Argument of type "Sequence[int]" cannot be assigned to parameter "b" of type "bytes" in function "print_bytes"
"Sequence[int]" is not assignable to "bytes" (reportArgumentType)
< /code>
Итак, я попытался использовать ограничение типа: "chunk
Код: Выделить всё
def chunk[T: Sequence](data: T, chunk_size: int) -> Generator[T]:
yield from (data[i : i + chunk_size] for i in range(0, len(data), chunk_size))
< /code>
На этот раз пирайт жалуется на саму функцию: < /p>
error: Return type of generator function must be compatible with "Generator[Sequence[Unknown], Any, Any]"
"Generator[Sequence[Unknown], None, Unknown]" is not assignable to "Generator[T@chunk, None, None]"
Type parameter "_YieldT_co@Generator" is covariant, but "Sequence[Unknown]" is not a subtype of "T@chunk"
Type "Sequence[Unknown]" is not assignable to type "T@chunk" (reportReturnType)
Код: Выделить всё
@typing.overload
def chunk[T: bytes | bytearray](data: T, chunk_size: int) -> Generator[T]: ...
@typing.overload
def chunk[T](data: Sequence[T], chunk_size: int) -> Generator[Sequence[T]]: ...
def chunk(data, chunk_size: int):
yield from (data[i : i + chunk_size] for i in range(0, len(data), chunk_size))
Подробнее здесь: https://stackoverflow.com/questions/796 ... -the-given