[Python-ideas] Optional static typing -- the crossroads
Chris Angelico
rosuav at gmail.com
Sun Aug 17 16:30:49 CEST 2014
On Sun, Aug 17, 2014 at 8:20 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Hmmm. Anything the decorators do to the annotations will be at runtime,
> so is it reasonable to say that the static typing tool will only operate
> on the annotations available at compile time?
>
> That is, given:
>
> @mangle_annotations
> def spam(x:int)->List[str]: ...
>
> the type checker is expected to use the annotations seen at
> compile-time, no matter what the mangle_annotations decorator happens to
> do at run-time.
>
> Otherwise, the type-checker needs to be a full Python interpreter, in
> order to see what mangle_annotations does. And that could be an
> intractible problem:
>
> def mangle_annotations(func):
> if random.random() < 0.5:
> func.__annotations__['x'] = List[str]
> else:
> func.__annotations__['x'] = float
>
>
> I don't see how any type-checker is supposed to take that into account.
You give an example of a malicious mangling, but more significant is
the naive mangling - wrapping the decorated function in a
non-annotated outer function, without using functools.wraps() or
equivalent (I'm sure it'd be possible to propagate the annotations
through wraps(), so that would take care of a lot of cases).
IMO the right handling here is to completely ignore all unrecognized
decorators, on the assumption that most decorators should be returning
an "equivalently usable" function. I don't, for instance, see
real-world examples of decorators that add extra parameters to a
function, even though it would be plausible (maybe you have a whole
bunch of functions that all take an optional mode parameter, which
causes other arguments to be translated automatically by the
decorator?). If you're annotating the function, the type checker can
assume that that's intended to be correct.
ChrisA
More information about the Python-ideas
mailing list