But thanks to how we can introspect annotations, we could have a specialized syntax for types that would be also usable for those things, while the other way around would be harder.
So it's nice we went the type hints road, because you can now define types, and yet have wonderful libs like pydantic, fastapi, typer, taichi and so on.
I designed typedload before pydantic was a thing and I made it with mypy in mind already, and with the idea of using the standard library, not defining my own dataclass.
Unfortunately mypy has a limitation so you can't make a function that takes Type[T] and returns T. You can define it but it won't work for all types. There is still discussion on how to provide this feature.
@inject
def main(service: Service = Provide[Container.service]) -> None:
...
FastAPI also does a similar sort of trick, for DI and also request parsing.https://python-dependency-injector.ets-labs.org/introduction...
taichi does ffi with their own dsl, cython with c.
I you haven't tried pydantic or sister projects (django-ninja, fastapi and typer), this is a good place to start. They bring a breath of fresh air and are quite fun to use.
An example:
@dataclass
class HackerNewsPost:
title: str
author: str
votes: int = 0
This will generate a constructor that looks like this: def __init__(title: str, author: str, votes: int = 0):
self.title = title
self.author = author
self.votes = votes
This is pretty useful, writing __init__ boilerplate has been a common thing in my experience using Python, and it makes code more compact and readable.It can do some other things too!
https://cython.readthedocs.io/en/latest/src/tutorial/external.htmlYou define a Python function interface but populate it with a string of C code.
def add(x: int, y: int) -> int:
""" // c code
int x, y
return x + y
"""
And it compiles it and turns it into a C extension?That's...horrifying yet amazing.
It's either xlcalculator or pycel, one of the two. I forgot which one does it that way.
https://github.com/bradbase/xlcalculator#addingregistering-e...
It remained enabled with a future and while it kinda works, it doesn't work in all cases. Non top level definitions can never be retrieved basically.
From this thread [0] it does not look open and shut that it will work once these changes go into effect.
I've seen similar (in-house) to generate HTML forms also.