On 29 Nov 2021, at 23:56, Barry Warsaw wrote:
[...]
(not that you're not allowed to use for anything else, of course you
are, but that other uses won;t be taken into account when designing
the new interface)
But I have never seen that clearly stated anywhere. The closest is
from PEP 563, where it says:
"""
With this in mind, uses for annotations incompatible with the
aforementioned PEPs should be considered deprecated.
"""
Which pretty much makes the point, but it's a bit subtle -- what does
"incompatible' mean?
You make a good point. I agree that while all the signs are there for
“annotations are for typing”, this has never been explicitly or
sufficiently codified, and I’ve been proposing that Someone write a
PEP that makes this an official pronouncement. That someone may very
well be a person on the SC, but given the timing of elections, it’s
likely to fall to the next SC, if they still agree with that position!
:-D
My recollection of the history of annotations falls somewhere between
Greg’s and Guido’s. Annotations as a feature were inspired by the
typing use case (with no decision at the time whether those were to be
static or runtime checks), but at the same time allowing for
experimentation for other use cases. Over time,
annotations-for-typing clearly won the mindset and became the
predominant use case. Personally, I was strongly against type
annotations because of my experience in other languages, but over time
I was also won over. From library documentation, to complex code
bases, to the transient nature of contributors, type annotations are
pretty compelling, and most (but not all) of my worries really
didn’t come to fruition.
We can lament the non-typing use of annotations, but I think that
horse is out of the barn and I don’t know how you would resolve
conflicts of use for typing and non-typing annotations. It’s been a
slow boil, with no definitive pronouncement, and that needs to be
fixed, but I think it's just acknowledging reality. That’s my
personal opinion.
But isn't that the reason why we have `typing.Annotated`, so that
annotations used for typing and annotations used for other purposes can
coexist?
An annotation used for typing only is:
```
def f(x: int):
... return x
...
f.__annotations__['x']
<class 'int'>
```
An annotation used for something else is:
```
def f(x: 'something not typing related'):
... return x
...
f.__annotations__['x']
'something not typing related'
```
`typing.Annotated` gives us both:
```
from typing import *
def f(x: Annotated[int, 'something not typing related']):
... return x
...
f.__annotations__['x'].__args__[0]
<class 'int'>
f.__annotations__['x'].__metadata__[0]
'something not typing related'
```
Granted, for someone who only wants to use annotations for their own
purpose, digging out the original not typing related stuff from
`typing.Annotated` is more work, but doable.
Or am I missing something here?
[...]
Servus,
Walter
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/4WM43CS45R7H7X2WO4F3UVDCH7HFHDJJ/
Code of Conduct: http://python.org/psf/codeofconduct/