application of type vars to generic class is not currently supported #227

Open
opened 2022-03-20 22:21:50 +08:00 by sb10q · 2 comments

Based on https://github.com/m-labs/artiq/issues/1207

from typing import TypeVar, Generic

from numpy import int32

from artiq.experiment import *
from artiq.coredevice.core import Core


@nac3
class A:
    @kernel
    def do(self) -> int32:
        return 0


@nac3
class B:
    @kernel
    def do(self) -> int32:
        return 1


T = TypeVar("T", A, B)


@nac3
class C(Generic[T]):
    sub: Kernel[T]

    def __init__(self, which):
        if which:
            self.sub = B()
        else:
            self.sub = A()

@nac3
class Types(EnvExperiment):
    core: KernelInvariant[Core]
    c0: Kernel[C[T]]
    c1: Kernel[C[T]]
    
    def build(self):
        self.setattr_device("core")
        self.c0 = C(False)
        self.c1 = C(True)
    
    @kernel
    def run(self):
        print_rpc(self.c0.do() + self.c1.do())

application of type vars to generic class is not currently supported (at typedef.py: line 39 column 18)
Based on https://github.com/m-labs/artiq/issues/1207 ```python from typing import TypeVar, Generic from numpy import int32 from artiq.experiment import * from artiq.coredevice.core import Core @nac3 class A: @kernel def do(self) -> int32: return 0 @nac3 class B: @kernel def do(self) -> int32: return 1 T = TypeVar("T", A, B) @nac3 class C(Generic[T]): sub: Kernel[T] def __init__(self, which): if which: self.sub = B() else: self.sub = A() @nac3 class Types(EnvExperiment): core: KernelInvariant[Core] c0: Kernel[C[T]] c1: Kernel[C[T]] def build(self): self.setattr_device("core") self.c0 = C(False) self.c1 = C(True) @kernel def run(self): print_rpc(self.c0.do() + self.c1.do()) ``` ``` application of type vars to generic class is not currently supported (at typedef.py: line 39 column 18) ```

I am not sure why is there such a restriction. And what is application of typevar to generic class? Do you mean anything like C[T] where T = TypeVar(...) is not supported? @ychenfo

I am not sure why is there such a restriction. And what is application of typevar to generic class? Do you mean anything like `C[T]` where `T = TypeVar(...)` is not supported? @ychenfo
Collaborator

I am not sure why is there such a restriction. And what is application of typevar to generic class? Do you mean anything like C[T] where T = TypeVar(...) is not supported?

Yes, I think this is what I try to say by 'application of typevar to generic class'.

As for the restriction, this indeed should not be a limit here.. but if I am not mistaken, I remembered that the main reason is that I am not sure how subst can work with something like:

T = TypeVar('T', int, bool, float)
V = TypeVar('V', int64, bool, float)

class C(Generic[T]):
	...

where C[V] would actually be something like C[K], where we need to create another fresh typevar K on the fly whose range is the intersection of T and V and whose var_id is fresh and cannot known by others and hence cannot be put in a var_map that can be used to further subst this C[K].. Or will the new commit fixing type var by fixing the var_id be able to somehow handle this?

> I am not sure why is there such a restriction. And what is application of typevar to generic class? Do you mean anything like C[T] where T = TypeVar(...) is not supported? Yes, I think this is what I try to say by 'application of typevar to generic class'. As for the restriction, this indeed should not be a limit here.. but if I am not mistaken, I remembered that the main reason is that I am not sure how `subst` can work with something like: ``` T = TypeVar('T', int, bool, float) V = TypeVar('V', int64, bool, float) class C(Generic[T]): ... ``` where `C[V]` would actually be something like `C[K]`, where we need to create another fresh typevar `K` on the fly whose range is the intersection of `T` and `V` and whose `var_id` is fresh and cannot known by others and hence cannot be put in a var_map that can be used to further `subst` this `C[K]`.. Or will the new commit fixing type var by fixing the var_id be able to somehow handle this?
sb10q added this to the Beta milestone 2022-03-31 10:31:51 +08:00
Sign in to join this conversation.
No Milestone
No Assignees
3 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#227
There is no content yet.