On Tue, Apr 7, 2020 at 11:17 AM Wes Turner firstname.lastname@example.org 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
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.
On Tue, Apr 7, 2020, 11:45 AM Tin Tvrtković email@example.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 firstname.lastname@example.org Subject: [Python-ideas] Re: Improvement: __json__ To: email@example.com Message-ID: firstname.lastname@example.org 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
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.
Python-ideas mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://email@example.com/message/ISSUQV... Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list -- firstname.lastname@example.org To unsubscribe send an email to email@example.com https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://firstname.lastname@example.org/message/LQKGBU... Code of Conduct: http://python.org/psf/codeofconduct/