Created
May 8, 2024 21:26
-
-
Save mypy-play/00775fdfb0b214e6433422f49b50d942 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typing | |
class NDArray: | |
shape: tuple | |
def __setitem__(self, idx: NDArray, v: object) -> None: ... | |
def random(x: object) -> NDArray: return NDArray() | |
Image = typing.NewType('Image', NDArray) | |
Mask = typing.NewType('Mask', NDArray) | |
array = random((50, 50)) | |
image = Image(array) | |
mask_array = random((50, 50)) | |
mask = Mask(mask_array) | |
def zero(image : Image, mask : Mask) -> Image: | |
if not image.shape == mask.shape: | |
raise ValueError("Image and mask shapes must match") | |
image[mask] = 0 | |
return image | |
zero(image, mask) | |
# Fails mypy | |
zero(array, mask_array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment