On Fri, Jan 07, 2022 at 04:27:37PM -0800, Christopher Barker wrote:
Note that if PEP 563 is ultimately accepted, then Annotations would be strings, and type checkers could use any language they wanted.
Annotations still have to be syntactically valid Python expressions.
>>> from __future__ import annotations >>> def func(arg: array[1...10] of float) -> str: pass File "<stdin>", line 1 def func(arg: array[1...10] of float) -> str: pass ^^^ SyntaxError: invalid syntax
Of course if you explicitly wrap your annotation in quotation marks, you can use any syntax you like (think: regexes, SQL, etc). But without a *standard* annotation syntax:
- static checkers will disagree on what annotations mean; - runtime introspection will be difficult; and - IDEs and syntax colourisers are going to just treat them as strings.
We can write little DSLs with any syntax we like using explicitly quoted strings. We're not limited to do this in annotations. But while DSLs tend to be specific to your own library or application, annotations exist in a wide ecosystem of both static and runtime tools that expect to interpret annotations.
Writing your own little DSL for annotations cuts you off from the rest of the Python ecosystem.