Skip to content

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

@dangle

Description

@dangle

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions