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). > In my other example, I used `Annotated` to create what I guess is (and > what you suggested) a pseudo-type? When it was annotated it had > nothing to do with any attribute. To reiterate: > > SomeType = Annotated[str, "some type"] That's not an annotation. That's a type alias. Annotations follow a colon, not an equals sign. > No attribute here (yet). Only when this is applied as a type hint to an > attribute would it then apply. > > class A: > attr: SomeType Right. Now you have an annotation, and sure enough, we would find {'attr': typing.Annotated[str, "some type"]} in A.__annotations__. It is still annotating the attribute (to be precise, the attribute name). > `Annotated` certainly appears to be intended to provide hints about the > type. In PEP 593, `MaxLen` is an example of prescribing constraints on > value. It would apply to an attribute if that attribute was annotated > with it as a type hint. And until some variable or attribute was actually annotated with it, it might as well not exist. Annotated allows us to create new types, by associating arbitrary metadata with an existing type. The meaning of that metadata is completely unspecified. So we can say: Vec = Annotated[List[Tuple[T, T]], MaxLen(10)] but that has no real meaning until you annotate a variable or attribute, in which case the Vec type is annotated onto the attribute. That use-case for Annotated is independent of the proposed use-case here. Sure you can use it to create new types, or type-aliases. But that's not the only thing we can use it for. The meaning of MaxLen(10) is unspecified. To a human reader, we can guess that it probably means that the list can have a length of no more than 10. Any tooling that comes across it is supposed to ignore it if it doesn't know how to interpret it. So for all we know, MaxLen(10) is not enforced by any tool, and it is purely there as documentation to the reader: "Please don't make your vector longer than ten items". We're not limited to only using Annotated to create new types. In an annotation, we can associate arbitrary metadata with the annotated attribute or variable. The interpretation of that metadata is still up to the consumer. Is it a type restriction? Is it a docstring? Is it something else? It's entirely up to the tooling. By the way, the proposal to just follow attributes with a string has already been considered and rejected: https://www.python.org/dev/peps/pep-0224/ -- 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/6UP3ZJBNXBXZS5QCIJQIDCX35HXQUZCI/ Code of Conduct: http://python.org/psf/codeofconduct/