ramonhagenaars / nptyping

💡 Type hints for Numpy and Pandas
MIT License
593 stars 30 forks source link

PyCharm reports syntax error when using Shape['*'] in variable declarations #90

Closed nikolajsheller closed 2 years ago

nikolajsheller commented 2 years ago

The following will run correctly:

    a: NDArray[Shape['*']] = np.array([1, 2, 3])
    isinstance(a, NDArray[Shape['*'], Int])

But causes PyCharm to issue the syntax error:

Statement expected, found Py:MULT

ramonhagenaars commented 2 years ago

This is unfortunate behavior by PyCharm that cannot be solved by nptptying. I ~am about to release~ just released a patch that would allow you to use extra quotes as follows:

Shape['"*"']

This will appease PyCharm. Though I agree that it looks less attractive, at least it offers you a choice. As I state in the FAQ of the release to come, there is only this option, or the option of accepting the PyCharm complaints... alas.

nikolajsheller commented 2 years ago

Thank you for your reply @ramonhagenaars, I think I will go with the extra quote solution. It is very distracting to me when PyCharm complains.

Thank you for your work

ramonhagenaars commented 2 years ago

There is one other option I forgot to mention. You can also replace Shape by typing.Literal, this will silence the PyCharm complaints as well.

If you use an alias, it doesn't look so bad:

from typing import Literal as Shape

s: Shape["*"]  # PyCharm is still happy.