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

Creating and using a pseudo-type like object causes mypy to bail with "invalid syntax". #4285

Closed
dangle opened this issue Nov 25, 2017 · 5 comments

Comments

@dangle
Copy link

dangle commented Nov 25, 2017

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.

@ilevkivskyi
Copy link
Member

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.

@dangle
Copy link
Author

dangle commented Nov 26, 2017

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.

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Jun 6, 2019

FYI, there is https://www.python.org/dev/peps/pep-0593/ that proposes mechanism to support arbitrary custom types.

@ilevkivskyi
Copy link
Member

(EDIT: fixed PEP number in the link.)

@hauntsaninja
Copy link
Collaborator

PEP 593 is a thing and the original example does not issue a syntax error / the error is type ignorable

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

No branches or pull requests

3 participants