Would you generate a schema from the type annotations so that other languages can use the data?

JSON is really not the most efficient data representation for round-tripping to and from Python. A binary format (or pickle without arbitrary code execution) would solve for Python object > file > Python object.

IMHO, JSON is most useful when the data needs to be read/written from (browser-side) JS, too.

And then you need data validation for user-supplied input (which is not distinct from the deserialization problem). E.g. DRF has data validation. But (mypy) type annotations are insufficient for data validation: compare JSONschema and the maximum amount of information storable in mypy-compatible type annotations, for example. 

Data validation needs to return errors in a feedback loop with the user, so it's sort of more than just the preconditions that need to be satisfied before a function proceeds.

- serialization from Python (TA useful)
- deserialization with the same Python code (TA useful)

- deserialization with other Python code (insufficient)
- deserialization with other languages (insufficient)
- form generation (insufficient)
- data validation (insufficient)
- preconditions (insufficient)

So, IMHO type annotations are not insufficient and thus redundant and not elegant.



On Tue, Apr 7, 2020, 11:45 AM Tin Tvrtković <tinchester@gmail.com> wrote:
As the author of one of these third-party libraries, I feel like I can contribute to this discussion. It can indeed be done very elegantly with type annotations, and it should for sure be left to the ecosystem.

The only things we need from core Python are good tools for dealing with run-time type information. For example, singledispatch doesn't really work with types (i.e. Optionals, Unions, Sequences as opposed to actual classes), but all of that can be worked around.

In my experience, runtime type information is extremely useful exactly in cases of deserialization, and for big projects starts being useful much before the type information starts being useful in a static analysis context.
 
From: Greg Ewing <greg.ewing@canterbury.ac.nz>
Subject: [Python-ideas] Re: Improvement: __json__
To: python-ideas@python.org
Message-ID: <e70016ba-5419-e252-d7bd-9705dbebf24c@canterbury.ac.nz>
Content-Type: text/plain; charset=utf-8; format=flowed

On 7/04/20 4:57 am, Wes Turner wrote:
> Python object > JSON  > object requires type information to be serialized.

Not necessarily -- Java's json library uses reflection on compile
time type information to deserialise json into an object hierarchy.
You tell it the class corresponding to the top level and it figures
out the rest.

Something similar could be done in Python using type annotations.

--
Greg

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/ISSUQVYI5OYYXKELUNCD5YCEDZ75LCEB/
Code of Conduct: http://python.org/psf/codeofconduct/