Re: More Questions About Python 3000 Answered
Posted: Aug 14, 2007 5:45 AM
> You can copy the annotations just like the docstring:
> _func.__annotations__ = f.__annotations__.copy()
This takes care of my doubts. Thankyou.
> No. In general the signature of a subclass __init__
> doesn't have to match the signature of the base class
> __init__ so it would be a mistake to do something like
> this automatically; and deducing that the signatures are
> actually the same would require way too much understanding
> in the compiler.
That may be so, but what if there was a decorator may be to say that the signatures are same. You may be aware that, esp in GUI class hierarchies, we have a lot of override chains that just calles the super(SomeClass, self).__current_method__(*args, **kw). Having them kill annotations will defeat the purpose of annotation.
I guess there can be a few annotation related decorators:
* @preserves_signature. This will follow the super logic, and find the nearest super, and .copy() its annotation.
This might take as keyword arguments the new arguments added that were not there in the base method.
@preserves_signature(new_var="annotation of new vars")
def __init__(self, new_var, *args, **kw):
self.new_var = new_var
super(NewClass, self).__init__(*args, **kw)
This will effectively copy the annotations from BaseClass.__init__ and add annotation for new_var, and apply to new __init__.
This leave the matter of changing annotation for return type. That too can be handled by similar means.
Is this what you think things will go? or is there still some gap in my understanding?