rsokl / phantom-tensors

Tensor-like types – with variadic shapes – that support both static and runtime type checking, and convenient parsing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Examples fail mypy-checks in unexpected ways

julian-goettingen opened this issue · comments

What you are doing looks very cool and could be helpful to me and a lot of people.

However, the examples from the readme do not work for me (I have tried mypy with python3.10 and python3.11)

For example, with this one:

from typing import Any

import numpy as np

from phantom_tensors import parse
from phantom_tensors.numpy import NDArray
from phantom_tensors.alphabet import A, B  # these are just NewType(..., int) types

def func_on_2d(x: NDArray[Any, Any]): ...
def func_on_3d(x: NDArray[Any, Any, Any]): ...
def func_on_any_arr(x: np.ndarray): ...

# runtime: ensures shape of arr_3d matches (A, B, A) patterns
arr_3d = parse(np.ones((3, 5, 3)), NDArray[A, B, A])

func_on_2d(arr_3d)  # static type checker: Error!  # expects 2D arr, got 3D

func_on_3d(arr_3d)  # static type checker: OK
func_on_any_arr(arr_3d)  # static type checker: OK

I get the following errors:

phan_tensor_test.py:11: error: "NDArray" expects no type arguments, but 2 given  [type-arg]
phan_tensor_test.py:12: error: "NDArray" expects no type arguments, but 3 given  [type-arg]
phan_tensor_test.py:15: error: The type "Type[NDArray]" is not generic and not indexable  [misc]

I am not deep enough into python-typing to understand how this issue could be related to a misconfiguration. I know variadic generics are a very recent feature, which is why I tried to run it with python 3.11 but it still showed this error.

I am using the latest pypi-version of your library (I did not clone your repo)

(edited the title because the example is supposed to fail mypy, but on another line)

mypy does not support PEP 646 yet, but pyright does. You can run pyright on the following examples to see that they do, indeed type-check as expected!

Thanks!

mypy is working on support: python/mypy#12280
I wont switch typechecker to use phantom_tensors, but I expect to start using it once mypy catches up.