On 1/12/21 11:26 AM, Jim J. Jewett wrote:
If I understand correctly, the problem is that you can't store multiple alternative annotations on my_attr.  Therefore:

    class C:
        my_attr:(int if random.random > 0.5 else float)

should be OK, because there is only a single annotation.

Sure, that works fine.  Any expression (except "yield" and ":=") is okay in an annotation.


What about optional attributes, like:

 class C:
    if random.random() > 0.5:
      my_attr:int=3

Also, would (conditionally defined) function variable attributes become a problem if they were actually stored?  (Take Larry's class example, and make if a def instead of a class statement.)

You mean attributions on function locals?

def foo():
  if random.random() > 0.5:
    x:int=3
  else:
    x:float=3.5

As I mentioned in my PEP, attributions on function locals have no effect at runtime.  If they did, this would cause the same problem that doing it in classes has.


Cheers,


/arry