I'm assuming you are using a code generator? My understanding is that as MyPy is a static analyser there is no way to automatically create types at runtime (which is actually super annoying, particularly for a language as dynamic as Python).
I'm assuming you are using a code generator? My understanding is that as MyPy is a static analyser there is no way to automatically create types at runtime (which is actually super annoying, particularly for a language as dynamic as Python).
Could you explain what you mean by this? I'm not clear on what typescript behavior this describes.
https://www.typescriptlang.org/docs/handbook/utility-types.h...
It unfortunately leaves you with a choice of either taking advantage of dynamic annotations or using MyPy, but not both.
And as the sibling comment says, typescript did it so much better.
```py
from typing import TYPE_CHECKING
if TYPE_CHECKING:
Foo = "expression that mypy can understand"
else:
Foo = "dynamic expression"
``` class Book(MonDoc):
title = StrField()
yearPublished = IntField()
Is it possible to give run-time types to `title` and `yearPublished`, in a way that works with mypy and the Python typing ecosystem? If so, is there a tutorial on this, as I'd like to add the feature to my ORM. class Book(MonDoc):
title: str = StrField()
yearPublished: int = IntField()
If this means too much redundant information for you then you could try defining the fields to return the corresponding python type e.g. def IntField() -> int:
...When I define something as a `StrField` I'm already saying it's a string, so I shouldn't have to say it again elsewhere. It would be nice if Python has an API such that I can write code within my `StrField` class that tells python that `title` is a `str`, but to the best of my understanding this doesn't exist.