4 ms·
What other use cases did the community come up with?
by arjvik 4y ago
What other use cases did the community come up with?
- Too 4y agoCreating argument parsers for CLIs: https://pypi.org/project/argtyped/ https://pypi.org/project/argtyped/ I've seen similar (in-house) to generate HTML forms also.
- ok123456 4y agoThings like pydantic and fastapi where the type annotations are used for run time argument validation and interface generation.
- deleted 4y ago[deleted]
- whimsicalism 4y agoDoes this still work with Python 3.10 changes removing runtime evaluation of type annotations? I seem to recall this screwed me when I was writing something for runtime usage of types.
- BiteCode_dev 4y agoThere was a heated debate because it broke things at the time, but now it works, yes.
- patrick91 4y agowe still don't know what's going to happen with type hints in future though, but I'm sure things won't break as they might have broken if they changed the default behaviour
- whimsicalism 4y agoOh it looks like they actually pushed back the PEP 563 changes.. From this thread [0] it does not look open and shut that it will work once these changes go into effect. [0]: https://github.com/pydantic/pydantic/issues/2678 https://github.com/pydantic/pydantic/issues/2678
- LtWorf 4y agoThey postponed that change. 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.
- BiteCode_dev 4y agoDependency injections, foreign-language bridges, data validation and parsing, etc. 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.
- arjvik 4y agoCool, any examples of libraries that use it for dependency injection or FFI or validation?
- GalaxySnail 4y agoFor FFI, there is a pure python mode in cython: https://cython.readthedocs.io/en/latest/src/tutorial/external.html
- BiteCode_dev 4y agodjango-ninja and fastapi does dep injection, parsing and validation based on annotations. 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.
- kylebarron 4y agoSpecifically it's the underlying pydantic that does validation based on annotations https://pydantic-docs.helpmanual.io/ https://pydantic-docs.helpmanual.io/
- BiteCode_dev 4y agoIndeed. The dep injection is fastapi and typer code though, and quite tied to it. So it's worth mentioning that somebody is attempting (quite successfully from the look of it) to create a generic lib of dep injection using annotation named DI, inspired by those libs: https://github.com/adriangb/di https://github.com/adriangb/di
- patrick91 4y agoI've created strawberry graphql, it uses type hints to generate GraphQL schemas
- Waterluvian 4y agoI used them at runtime to define the types for my (absolutely stupid) library that takes a Python function and replaces its contents with a C call. You define a Python function interface but populate it with a string of C code.
- kortex 4y agoWait, so like this? 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.
- eru 4y agoThat might be a worthwhile addition to the suggestion at https://github.com/faster-cpython/ideas/issues/453 https://github.com/faster-cpython/ideas/issues/453
- BiteCode_dev 4y agoCombined with the cog (https://nedbatchelder.com/code/cog https://nedbatchelder.com/code/cog) to have the c declaration in a proper separate c file, but included in the docstring, you could have the best of both worlds.
- BiteCode_dev 4y agoThat's a neat hack!
- _dain_ 4y agoThere's a library for simulating the Excel calculation engine in Python. It uses annotations to hint functions, but according to Excel's type system, not Python's. Then at runtime, those hints are used to do conversion and promotion based on how Excel does it, so you can do e.g. the equivalent of "1" + 1 and get 2. It's either xlcalculator or pycel, one of the two. I forgot which one does it that way.
- arjvik 4y agoLooks like it's xlcalculator: https://github.com/bradbase/xlcalculator#addingregistering-excel-functions https://github.com/bradbase/xlcalculator#addingregistering-e...