On Tue, Apr 13, 2021 at 12:32 PM Larry Hastings <larry@hastings.org> wrote:

On 4/12/21 7:24 PM, Guido van Rossum wrote:
I've been thinking about this a bit, and I think that the way forward is for Python to ignore the text of annotations ("relaxed annotation syntax"), not to try and make it available as an expression.

To be honest, the most pressing issue with annotations is the clumsy way that type variables have to be introduced. The current convention, `T = TypeVar('T')`, is both verbose (why do I have to repeat the name?) and widely misunderstood (many help request for mypy and pyright follow from users making a mistaken association between two type variables that are unrelated but share the same TypeVar definition). And relaxed annotation syntax alone doesn't solve this.

Nevertheless I think that it's time to accept that annotations are for types -- the intention of PEP 3107 was to experiment with different syntax and semantics for types, and that experiment has resulted in the successful adoption of a specific syntax for types that is wildly successful.


I don't follow your reasoning.  I'm glad that type hints have found success, but I don't see why that implies "and therefore we should restrict the use of annotations solely for type hints".  Annotations are a useful, general-purpose feature of Python, with legitimate uses besides type hints.  Why would it make Python better to restrict their use now?


Because typing is, to many folks, a Really Important Concept, and it's confusing to use the same syntax ("x: blah blah") for different purposes, in a way that makes it hard to tell whether a particular "blah blah" is meant as a type or as something else -- because you have to know what's introspecting the annotations before you can tell. And that introspection could be signalled by a magical decorator, but it could also be implicit: maybe you have a driver that calls a function based on a CLI entry point name, and introspects that function even if it's not decorated.

OTOH, not requiring that annotations are syntactically valid expressions might liberate such a CLI library too: you could write things like

def foo(prec: --precision int):
    ...


--
--Guido van Rossum (python.org/~guido)