[Python-ideas] Runtime types vs static types

Terry Reedy tjreedy at udel.edu
Sun Jul 2 14:09:38 EDT 2017


On 7/2/2017 7:57 AM, Steven D'Aprano wrote:
> On Sun, Jul 02, 2017 at 09:38:11PM +1000, Chris Angelico wrote:
>> On Sun, Jul 2, 2017 at 9:16 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>>> If we want to support that optimization, we could add an optimization
>>> flag that strips annotations at runtime, just as the -OO flag strips
>>> docstrings. That becomes a matter of *consenting adults* -- if you don't
>>> want annotations, you don't need to keep them, but it then becomes your
>>> responsibility that you don't try to use them. (If you do, you'll get a
>>> runtime AttributeError.)
>>
>> IMO people should act as if this will eventually be the case.
>> Annotations should be evaluated solely for the purpose of populating
>> __annotations__, and not for any sort of side effects - just like with
>> assertions.
> 
> Avoiding side-effects is generally a good idea, but I think that's
> probably taking it too far.
> 
> I think that we should assume that
> 
> def func(x:Spam()):
>      ...
> 
> will always look up and call Spam when the function is defined. But we
> should be prepared that
> 
> func.__annotations__
> 
> might not exist, if we're running in a highly-optimized mode, or
> MicroPython, or similar.

Code that does not control the compilation of the file with func should 
also not assume the existence of func.__doc__.  On the other hand, 
programs, such as IDEs, that do control compilation, by calling the 
standard compile(), can assume both attributes if they pass the 
appropriate compile flags.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list