Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pyscreeze stubs #8823

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stubs/PyScreeze/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version = "0.1.*"
requires = ["types-Pillow"]

[tool.stubtest]
ignore_missing_stub = false
196 changes: 196 additions & 0 deletions stubs/PyScreeze/pyscreeze/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import sys
from _typeshed import Incomplete, StrOrBytesPath
from collections.abc import Callable, Generator
from typing import NamedTuple, SupportsFloat, TypeVar, overload
from typing_extensions import Literal, ParamSpec, SupportsIndex, TypeAlias

from PIL import Image

_Unused: TypeAlias = object
_P = ParamSpec("_P")
_R = TypeVar("_R")
# TODO: cv2.Mat is not available as a type yet: https://github.com/microsoft/python-type-stubs/issues/211
# cv2.Mat is just an alias for a numpy NDArray, but can't import that either.
_Mat: TypeAlias = Incomplete

useOpenCV: bool
RUNNING_PYTHON_2 = sys.version_info < (3,)
GRAYSCALE_DEFAULT: Literal[False]
USE_IMAGE_NOT_FOUND_EXCEPTION: bool
scrotExists: bool

class Box(NamedTuple):
left: int
top: int
width: int
height: int

class Point(NamedTuple):
x: int
y: int

class RGB(NamedTuple):
red: int
green: int
blue: int

class PyScreezeException(Exception): ...
class ImageNotFoundException(PyScreezeException): ...

def requiresPillow(wrappedFunction: Callable[_P, _R]) -> Callable[_P, _R]: ...
@overload
def locate(
needleImage: str | Image.Image | _Mat,
haystackImage: str | Image.Image | _Mat,
*,
grayscale: bool | None = ...,
limit: _Unused = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: SupportsFloat | SupportsIndex | str = ...,
) -> Box | None: ...

# _locateAll_python / _locateAll_pillow
@overload
def locate(
needleImage: str | Image.Image,
haystackImage: str | Image.Image,
*,
grayscale: bool | None = ...,
limit: _Unused = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: None = ...,
) -> Box | None: ...

# _locateAll_opencv
@overload
def locateOnScreen(
image: str | Image.Image | _Mat,
minSearchTime: float = ...,
*,
grayscale: bool | None = ...,
limit: _Unused = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: SupportsFloat | SupportsIndex | str = ...,
) -> Box | None: ...

# _locateAll_python / _locateAll_pillow
@overload
def locateOnScreen(
image: str | Image.Image,
minSearchTime: float = ...,
*,
grayscale: bool | None = ...,
limit: _Unused = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: None = ...,
) -> Box | None: ...

# _locateAll_opencv
@overload
def locateAllOnScreen(
image: str | Image.Image | _Mat,
*,
grayscale: bool | None = ...,
limit: int = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: SupportsFloat | SupportsIndex | str = ...,
) -> Generator[Box, None, None]: ...

# _locateAll_python / _locateAll_pillow
@overload
def locateAllOnScreen(
image: str | Image.Image,
*,
grayscale: bool | None = ...,
limit: int | None = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: None = ...,
) -> Generator[Box, None, None]: ...

# _locateAll_opencv
@overload
def locateCenterOnScreen(
image: str | Image.Image | _Mat,
*,
minSearchTime: float,
grayscale: bool | None = ...,
limit: _Unused = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: SupportsFloat | SupportsIndex | str = ...,
) -> Point | None: ...

# _locateAll_python / _locateAll_pillow
@overload
def locateCenterOnScreen(
image: str | Image.Image,
*,
minSearchTime: float,
grayscale: bool | None = ...,
limit: _Unused = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: None = ...,
) -> Point | None: ...

# _locateAll_opencv
@overload
def locateOnWindow(
image: str | Image.Image | _Mat,
title: str,
*,
grayscale: bool | None = ...,
limit: _Unused = ...,
step: int = ...,
confidence: SupportsFloat | SupportsIndex | str = ...,
) -> Box | None: ...

# _locateAll_python / _locateAll_pillow
@overload
def locateOnWindow(
image: str | Image.Image,
title: str,
*,
grayscale: bool | None = ...,
limit: _Unused = ...,
step: int = ...,
confidence: None = ...,
) -> Box | None: ...
def showRegionOnScreen(region: tuple[int, int, int, int], outlineColor: str = ..., filename: str = ...) -> None: ...
def center(coords: tuple[int, int, int, int]) -> Point: ...
def pixelMatchesColor(
x: int, y: int, expectedRGBColor: tuple[int, int, int] | tuple[int, int, int, int], tolerance: int = ...
) -> bool: ...
def pixel(x: int, y: int) -> tuple[int, int, int]: ...
def screenshot(imageFilename: StrOrBytesPath | None = ..., region: tuple[int, int, int, int] | None = ...) -> Image.Image: ...

grab = screenshot
# _locateAll_opencv
@overload
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a strange use of overloads, since it looks like either one variant or the other is used depending on whether cv2 is present. Overloads are normally used when there are complicated relationships between different parameters and/or the return type. On the other hand, I can't think of any concrete ill effect of using overloads here, so I'm fine with keeping them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, since we can't represent that condition in typing, overloads are the next best thing so that both possible signatures are accurate.

def locateAll(
needleImage: str | Image.Image | _Mat,
haystackImage: str | Image.Image | _Mat,
grayscale: bool | None = ...,
limit: int = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: SupportsFloat | SupportsIndex | str = ...,
) -> Generator[Box, None, None]: ...

# _locateAll_python / _locateAll_pillow
@overload
def locateAll(
needleImage: str | Image.Image,
haystackImage: str | Image.Image,
grayscale: bool | None = ...,
limit: int | None = ...,
region: tuple[int, int, int, int] | None = ...,
step: int = ...,
confidence: None = ...,
) -> Generator[Box, None, None]: ...