From c29a149d16758fb3101ff4a8d7af4b2ea89e7721 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Fri, 25 Jun 2021 10:25:33 +0800 Subject: [PATCH] compiler: allows string annotation According to PEP484, type hint can be a string literal for forward references. With PEP563, type hint would be preserved in annotations in string form. --- artiq/compiler/embedding.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index fe197d81b..d4628bd77 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -949,6 +949,27 @@ class Stitcher: if annot is None: annot = builtins.TNone() + if isinstance(function, SpecializedFunction): + host_function = function.host_function + else: + host_function = function + + embedded_function = host_function.artiq_embedded.function + + if isinstance(embedded_function, str): + embedded_function = host_function + + if isinstance(annot, str): + if annot not in embedded_function.__globals__: + diag = diagnostic.Diagnostic( + "error", + "type annotation for {kind}, {annot}, is not defined", + {"kind": kind, "annot": repr(annot)}, + self._function_loc(function), + notes=self._call_site_note(call_loc, fn_kind)) + self.engine.process(diag) + annot = embedded_function.__globals__[annot] + if not isinstance(annot, types.Type): diag = diagnostic.Diagnostic("error", "type annotation for {kind}, '{annot}', is not an ARTIQ type",