Creating and using a pseudo-type like object causes mypy to bail with "invalid syntax".
Closed this issue · 5 comments
I've created a library with some pseudo-types that I'm using for validation, but I've run into some problems with using them with mypy.
Here's a trimmed down example to recreate the issue:
class BoundedMeta(type):
def __getitem__(self, args):
...
class Bounded(metaclass=BoundedMeta):
...
Age = Bounded[int, 0:150]
class Person:
age: Age
mypy gives example.py:8: error: Invalid type alias
for the Age
assignment that I can't seem to work around, but otherwise continues scanning the code and gives example.py:11: error: Invalid type "example.Age"
.
The real problem lies in changing the definition of Person
to use Bounded
directly.
class Person:
age: Bounded[int, 0:150]
mypy gives: example.py:9: error: syntax error in type comment
when run on this code. Notably, it gives this error even if this has # type: ignore
after the line.
I'd like to be able to use these pseudo-types with mypy, or at least have a means of telling mypy to ignore them everywhere so that library consumers don't get mypy errors using it, but at a minimum, I'd expect consistency between the two examples.
I see the value in such possibility (for example custom extensions to type system, documentation). But it is not easy to fix this. I think the right strategy is to just make this a non-blocking error, so that # type: ignore
will work. I think we already have a similar request, but can't find the issue.
That would be a good first step.
I really like the idea of custom extensions to the type system, but for now I think keeping library use from breaking mypy
use is sufficient.
FYI, there is https://www.python.org/dev/peps/pep-0593/ that proposes mechanism to support arbitrary custom types.
(EDIT: fixed PEP number in the link.)
PEP 593 is a thing and the original example does not issue a syntax error / the error is type ignorable