Summary:
I would like future versions of python to have some easy way to put anything into annotation, not just Types or stringified types. It is possible now, but I see it is going to be deprecated in the future.
Currently I can write:
def f(): return 1
class A:
a: f() = "and now for something completely different"
And annotation for "a"
would return a proper callable. With from __future__ import annotations
it would return a string, which could be hard to parse.
Maybe allow something like
from __future__ import old_annotations
to retain the current behaviour?
Motivation
I had developed a library that allows to write declarative statements in Python, in the style of probabilistic programming. It uses synaxis like
class MyModel(Model):
x: normal_prior(mean=0, std=1) = 0
y: normal_prior(mean=3, std=1) = 3
z: gamma_prior(a=3, b=10) = 3
Here normal_prior
would return a callable. Annotation describe prior for the model parameter, rather than its type (which is mostly irrelevant in my context).
The library works very well (in my context), by bringing expressivity and brevity of declarative probabilistic programming where it is needed. I use it to run different modelling projects, including official COVID model for Finland. The problem is, it look like future version of Python would not support such a frivolous use of annotations. So I suggest keeping the possibility to opt-out of new behaviors, maybe something like
from __future__ import old_annotations
or some alternative to get_type_hints
, which would run eval
in the appropriate context, but would not required to return Type.