On Sun, Dec 12, 2021 at 3:00 PM Steven D'Aprano <st...@pearwood.info> wrote:

> > But what's being annotated, the type or the attribute?
>
> That's easy to test:
>
> >>> class A:
> ...     attr: Annotated[int, "Doc string"]
> ...
> >>> int.__annotations__
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: type object 'int' has no attribute '__annotations__'
>
>
> Okay, so it's not the type. *wink*


I know what a wink means, but I have no idea what your actual point is.
It’s very hard not to get tripped up by the overloading of the word
“annotation”, but

typing.Annotated can exist outside of __annotations__ it is not an
annotation itself, it has special kind of type object (type descriptor?
What the heck do we call things that are used for type hints but are not
regular  Python  instantiable types?) it’s a container for a type and other
extra information that pertains to that type, not necessarily to the
parameter or whatever it gets attached to.

As far as I know, no one in this conversation has used typing.Annotated in
a type checking system— we really should hear from those folks.


>>> A.__annotations__
> {'attr': typing.Annotated[int, 'Doc string']}
>
> It's the *class* that is annotated. But note that the mapping is between
> the attribute name to annotation, so in the sense that attributes are
> represented by their name, it is the attribute that is annotated.


The attribute got annotated the typing.Annotated object, but what did the
string ‘Doc string’ get attached to?

As long as we are winking:

class A:
    attr = “a string”

A.attr.upper() exists, so obviously the class attribute has that method.

Of course not. The attribute has a value that is a string, and that has the
upper attribute.

Similarly, in your example, attr has an annotation, and the value of that
annotation has a string attached to it.

I’m not saying that a convention couldn’t be established to store
docstrings in annotations via the typing.Annotated type. But I’m not sure
that it wouldn’t get in the way of other uses, and it’s really not that
obvious or convenient either.

-CHB




>
> As it obviously has to be. After all, didn't we *literally* annotate the
> attribute?
>
>     attr: annotation
>
> Don't be fooled by the use of the Annotated pseudo-type as the
> annotation. We're still annotating the attribute, just like the code
> shows, regardless of what type we annotate it as.
>
> If we had written:
>
>     attr: Sequence[int]
>
> there would be no questions about what's being annotated, is it the type
> int or the attribute.
>
> It cannot be the type, because many different objects with many
> different annotations may share the same type.
>
> Unlike modules, classes and functions, the annotation cannot be on the
> attribute's *value*, because the attribute may not even have a value at
> this point. And even if it does, it might be immutable, or like the
> type, many different attributes, with different annotations, may be
> sharing the same value.
>
> This rules out putting the docstring on the attribute directly.
>
> The syntax shows us annotating the attribute, and that's exactly what
> got annotated.
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/UBGM3KTTH3A7BJXOQCZAAGKNWJ6LBRN4/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4QOJ3M7255XSO4L3YW7PU5EDZ5OJP33Q/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to