etemesi254 / zune-image

A fast and memory efficient image library in Rust
Other
319 stars 29 forks source link

Type stubs for zune-python #233

Closed kiyoon closed 1 week ago

kiyoon commented 1 week ago

zune-python currently doesn't have type hint stubs, which makes the type checkers like pyright struggle.

I've made a template version of the stub and this could be polished and added to the repository:

import enum
from typing import Literal, overload

class ImageFormat(enum.Enum):
    FarbFeld = ...
    JPEG = ...
    PNG = ...
    PSD = ...
    Unknown = ...
    BMP = ...
    HDR = ...
    JPEG_XL = ...
    PPM = ...
    Qoi = ...

class ResizeMethod(enum.Enum):
    Bilinear = ...
    Bicubic = ...

class Image:
    @staticmethod
    def open(file: str) -> Image: ...
    def save(self, file: str, *, format: ImageFormat) -> None: ...
    def dimensions(self) -> tuple[int, int]: ...
    def width(self) -> int: ...
    def height(self) -> int: ...
    @overload
    def auto_orient(self, *, in_place: Literal[False]) -> Image: ...
    @overload
    def auto_orient(self, *, in_place: Literal[True]) -> None: ...
    def auto_orient(self, *, in_place: bool) -> Image | None: ...
    @overload
    def resize(
        self,
        new_width: int,
        new_height: int,
        method: ResizeMethod,
        *,
        in_place: Literal[False],
    ) -> Image: ...
    @overload
    def resize(
        self,
        new_width: int,
        new_height: int,
        method: ResizeMethod,
        *,
        in_place: Literal[True],
    ) -> None: ...
    def resize(
        self, new_width: int, new_height: int, method: ResizeMethod, *, in_place: bool
    ) -> Image | None: ...
    @overload
    def crop(
        self, width: int, height: int, x: int, y: int, *, in_place: Literal[False]
    ) -> Image: ...
    @overload
    def crop(
        self, width: int, height: int, x: int, y: int, *, in_place: Literal[True]
    ) -> None: ...
    def crop(
        self, width: int, height: int, x: int, y: int, *, in_place: bool
    ) -> Image | None: ...
etemesi254 commented 1 week ago

Is this an issue with. how marutin does things or it's an issue with zune -python?

I think maybe marutin should generate type hints or maybe I'm wrong

kiyoon commented 1 week ago

In my experience with maturin, it doesn't really generate stubs. You have to manually add this.

See https://github.com/PyO3/pyo3/issues/2454

https://www.maturin.rs/project_layout This page also says you need to put a separate .pyi files