On Tue, Apr 7, 2020 at 11:17 AM Wes Turner <wes.turner@gmail.com> wrote:
Would you generate a schema from the type annotations so that other languages can use the data?

I haven't done this yet, but it would be pretty cool.

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

you mean are not sufficient / or are insufficient?

But what is meant by "type annotations"? I'm using them via dataclasses -- really as a shorthand for assigning a type to every field -- the annotations are just a shorthand that auto-generates a schema, essentially.

@dataclass
class MyClass:
    x: A_Type = A_default

So now I know that this class has a field names x that is the type int. So I use that type for validation, and serialization / deserialization.

But if you mean "type annotations" in the sense of the types provided out of the box in the typing module and used by MyPy (so I've heard, never did it myself) -- no, they are not sufficient -- I need types that support my serialization / deserialization system, and my validation system. And I suppose we could have a standardized __json__ and __from_json__ protocol that I could use, but it seems a special case to me.

Note: I don't need to do anything special for types with standard json representation, but that's only the basics -- I end up using custom types for anything nested.

-CHB






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/
_______________________________________________
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/LQKGBUGU4R5V2HYBKJHKBNFVR5VK7WCA/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython