On 2021-12-12 16:22, Steven D'Aprano wrote:
On Sun, Dec 12, 2021 at 03:38:23PM -0800, Paul Bryan wrote:

OK, so it's not the type, except it kind of is.

Except it isn't annotating the type, it is annotating the attribute.

We don't annotate *types*:

     int: Sequence[str]

That would be a regular variable or attribute called "int" that just
shadowed the builtin type int. We annotate *variables* (including
function parameters and class attributes).

You're still missing what people have mentioned a few times. It is true that this annotates an attribute:

class Foo:
        x: int

        The question is what does this annotate:

Annotated[int, "some text here"]

In other words what does the use of Annotated in itself annotate. As Paul pointed out in his earlier message, you can create this Annotated thing without attaching it to any attribute. It seems that Annotated itself is annotated the type. In fact, there can be no debate about what Annotated annotates, since the documentation even says it explicitly (https://docs.python.org/3/library/typing.html#typing.Annotated):

> Specifically, a type T can be annotated with metadata x via the typehint Annotated[T, x].

So yes, Annotated IS annotating the type. The text specified in `Annotated[T, text]` is creating a sort of augmented type, which is like "a type that is type T but additionally means such-and-such". It is true that that can type can itself later be used to annotate an attribute (or variable), but at that point it will be marking the variable as being of a type that incorporates the annotating text, not annotating the variable itself with that text.

As such, I don't think Annotated is really a good choice for this. We really want the text of the annotation to be associated with the attribute itself, not with the attribute's type. Moreover, using Annotated requires the user to specify a type, but people should be able to specify annotations for documentation even if they're not using typing at all and not specifying any types. (They shouldn't have to resort to an awkward workaround like always throwing in a dummy Any type.)

As an aside, seeing what the docs say about Annotated makes me think that "Annotated" is a very bad name for this thing. It confuses the idea of a type annotation (i.e., attached to a variable) with this type-incorporating-a-label, where neither the label nor the type is actually an annotation in the type-annotation sense (because they have not been attached to a variable to annotate it). It seems it would have been better to called Annotated "Tagged" or "Labeled" or some such thing to make it clear that when you using it you are defining a new special-purpose type for use in later annotations./

--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail."
   --author unknown
_______________________________________________
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/P5QIDQLETFHOWNJG3YBWBCAAWZUOAO2S/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to