Postponed evaluation of annotations #19

Closed
opened 2021-06-25 05:16:21 +08:00 by lriesebos · 2 comments

Starting from Python 3.10, postponed evaulation of annotations will be the default behavior for Python:

This PEP proposes changing function annotations and variable annotations so that they are no longer evaluated at function definition time. Instead, they are preserved in annotations in string form.

For Python >=3.7 this feature can be enabled using from __future__ import annotations. When enabled, the ARTIQ compiler throws an error. See the following snippet.

from __future__ import annotations  # Adding this line causes the compile error

from artiq.experiment import *


class ArtiqTest(EnvExperiment):

    def build(self):
        self.setattr_device('core')

    @kernel
    def run(self):
        self.foo(1)

    @kernel
    def foo(self, v: TInt32):
        self.core.reset()
        print('foo=', v)

When enabling postponed evaluation of annotations, the following error is raised.

[nix-shell:~/kc705]$ artiq_run repository/artiq_test.py
repository/artiq_test.py:16:1: error: type annotation for argument 'v', ''TInt32'', is not an ARTIQ type
def foo(self, v: TInt32):
^                        
repository/artiq_test.py:16:1: note: in kernel function here
def foo(self, v: TInt32):
^                        
repository/artiq_test.py:13:9-13:17: note: while inferring a type for an attribute 'foo' of a host object
        self.foo(1)
        ^^^^^^^^

It is fine that currently the ARTIQ compiler is not compatible with postponed evaluation of annotations. Will the new compiler be?

Starting from Python 3.10, [postponed evaulation of annotations](https://www.python.org/dev/peps/pep-0563/) will be the default behavior for Python: > This PEP proposes changing function annotations and variable annotations so that they are no longer evaluated at function definition time. Instead, they are preserved in __annotations__ in string form. For Python >=3.7 this feature can be enabled using `from __future__ import annotations`. When enabled, the ARTIQ compiler throws an error. See the following snippet. ```python from __future__ import annotations # Adding this line causes the compile error from artiq.experiment import * class ArtiqTest(EnvExperiment): def build(self): self.setattr_device('core') @kernel def run(self): self.foo(1) @kernel def foo(self, v: TInt32): self.core.reset() print('foo=', v) ``` When enabling postponed evaluation of annotations, the following error is raised. ``` [nix-shell:~/kc705]$ artiq_run repository/artiq_test.py repository/artiq_test.py:16:1: error: type annotation for argument 'v', ''TInt32'', is not an ARTIQ type def foo(self, v: TInt32): ^ repository/artiq_test.py:16:1: note: in kernel function here def foo(self, v: TInt32): ^ repository/artiq_test.py:13:9-13:17: note: while inferring a type for an attribute 'foo' of a host object self.foo(1) ^^^^^^^^ ``` It is fine that currently the ARTIQ compiler is not compatible with postponed evaluation of annotations. Will the new compiler be?

This is a bug in the current ARTIQ compiler and could be fixed. The new compiler should also be fine with this. I would submit a PR soon.

This is a bug in the current ARTIQ compiler and could be fixed. The new compiler should also be fine with this. I would submit a PR soon.
https://github.com/m-labs/artiq/pull/1704
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3-spec#19
There is no content yet.