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

Examples fail mypy-checks in unexpected ways #31

Closed
julian-goettingen opened this issue May 25, 2023 · 3 comments
Closed

Examples fail mypy-checks in unexpected ways #31

julian-goettingen opened this issue May 25, 2023 · 3 comments

Comments

@julian-goettingen
Copy link

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)

@julian-goettingen julian-goettingen changed the title Example does not pass mypy-checks for me Examples fail mypy-checks in unexpected ways May 25, 2023
@julian-goettingen
Copy link
Author

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

@rsokl
Copy link
Owner

rsokl commented May 25, 2023

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!

@julian-goettingen
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants