Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created May 8, 2024 21:26
Show Gist options
  • Save mypy-play/00775fdfb0b214e6433422f49b50d942 to your computer and use it in GitHub Desktop.
Save mypy-play/00775fdfb0b214e6433422f49b50d942 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
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