[Python-ideas] Was: Annotations (and static typing), Now:Sharing __annotations__

Steven D'Aprano steve at pearwood.info
Sun Aug 24 16:03:14 CEST 2014


On Sun, Aug 24, 2014 at 09:32:38AM -0400, Cem Karan wrote:
> 
> On Aug 24, 2014, at 7:39 AM, Ed Kellett <edk141 at gmail.com> wrote:
> 
> > I have a few questions:
> > 
> > - How often do multiple kinds of annotation end up on the same function?
> 
> In this thread, we've already talked about type checkers and 
> documentation generators, both of which can use the __annotations__ 
> dictionary legitimately.  Now imagine that you are an end user that 
> has installed a documentation generator and a static type analyzer.  
> If both tools were to use the __annotations__ dictionary, then right 
> now you could choose one or the other tool, but not both.

Not necessarily. It depends on what the tools expect to find in the 
annotations. If type hints become the standard for annotations, which is 
Guido's proposal, then both tools will expect to find type hints, and 
both can use them.

As for the cases where the tool or library wants to use annotations for 
something else, I'm quite happy to say that, in those cases, you can't 
use both. It's like docstrings. Docstrings are for documentation, 
including the interactive help system. If a library wants to use __doc__ 
for something else, it can (within the limits that it can only be a 
string, or None). But a user of that library cannot expect to *both* use 
the function __doc__ as a docstring *and* for this other purpose at the 
same time. They have to pick one.


> However, if 
> the standard I'm proposing was adopted, then each tool would choose 
> its own UUID as its key, which would mean they could share entries in 
> the annotations dictionary.

I don't think Guido wants to encourage multiple, incompatible uses for 
function annotations. It is going to be tricky enough to persuade him to 
merely allow other uses.

My feeling is that this is a case of YAGNI. I don't think there are 
going to be so many competing uses for function annotations in common 
use that this will be necessary. 


-- 
Steven


More information about the Python-ideas mailing list