by BiteCode_dev · 4y ago · 0 comments
When Python 3 came out, annotations were indeed added to the language, but no use case was decided for them. It was an experiment to see what the community would do with them instead of forcing a use case base on what the core devs would think would be useful.
In order to allow this, annotations where permitted to be any legal python expression.
Nowadays, type hints have won: annotations are widely used to add static type checking in Python. But the way the language treat them have not changed much appart from becomming lazy. While mypy cannot understand dynamic Python expressions in them, those are nevertheless legal syntax, and will result in correct metadata at runtime.
Therefore, were the typing system not turing complete, the annotations would have been anyway by nature of being basically an exec().
def f() -> int: return 1
def f() -> (lambda: int)(): return 1
only the first is correctly typed by PEP 484. mypy rejects the second with “error: Invalid type comment or annotation”.That’s the whole idea of static type checking: in return for restricting yourself to a controlled subset of the language that isn’t “too dynamic”, you gain the ability to type-check your code without running it.
The result of this paper is that one can nonetheless abuse subtyping to perform Turing-complete computation in the type checker. The computation doesn’t happen when your code is executed (annotations have no effect on the Python interpreter at runtime unless your program chooses to inspect them), it happens when you run mypy on it.
This is generally considered undesirable, because we want type checking to be decidable, and Turing-complete computation is not.
Eh, you are mixing up distinctions.
All that's required of _static_ typing is that your static type specification finishes running before you start your program.
Whether that static type specification language is Turing complete or not is a completely separate issue. And, yes, you are right that it's generally useful for that language to be decidable. But see eg Haskell's UndecidableInstances.
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.
I've seen similar (in-house) to generate HTML forms also.