TSP → Python conversions #8220
Replies: 1 comment
-
My two cents: RecordUsing Also we could argue that for things like:
This is more a model with extra items, than this is a record with a forced key. IOW, that it should be a dataclass (or whatever we're using for models). Making the whole model a TypedDict because it's also a record, feels like losing information, and potential breaking changes over time (adding a record later would shape the model entirely differently). Model as dataclassDataclass may be limited compared to framework like Pydantic, in terms of expressing some TypeSpec features (visibility, XML annotations, wire name VS property name, etc.). The serialization and deserialization is quite weak, and will require extensive runtime implementation. I feel at this point, the community chose Pydantic for model, but it's an external dependency (and potentially an heavy one). Nullability VS optionalityI see right now the proposal is to use
DefaultDefault value is server side in TypeSpec. So if you generated server code, it's fair to use GenericIt makes sense to me to map TypeSpec template to generic. Inheritance and spreeadMakes sense to just use the Python inheritance for UnionModel union as TypeAlias makes sense to me. For named union, I don't see the value in creating the I'm curious about what is planned for:
We call that an "extensible enum" in the Azure world, meaning "If something is typed Breed, those are the known values, but there may be more. InterfaceImplementing interface as I wonder if there is a gap here. Interface doesn't really mean in TypeSpec what we use it in usual object languages. Interface is a group of operations, so if we plan to generate operation a certain way, a group of operations should look like a generation of a group of operation (not sure I'm explaining this right :D). Operation signatureMakes all sense to me. EnumMakes all sense to me. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
TSP → Python conversions
We are writing this simple doc in an effort to try to imagine the main conversion scenarios we need to take into consideration for the emitter-framework Python implementation.
These conversion patterns aim to cover Python 3.10+.
Models with arbitrary number of properties (Record spreads)
Due to limitations on how to handle a dynamic number of properties, we should use TypedDicts for these scenarios exclusively.
Record model
Model with is operator to Record
Model that extends a Record
Model that has a spread to a Record
Models (Non-Record)
For these, use dataclasses.
Model
Model with a field that can be null
Model with an optional field
Model with a field that has a default
Model with an optional field that can be null
Model with a special property type - never
TSP Python
Model that extends a generic with never
TSP Python
Model Inheritance - Extends
TSP Python
Model Inheritance - Spreads
TSP Python
Model Inheritance - Is
TSP Python
Other
Union
TSP Python
Named unions
TSP Python
Intersections
TSP Python
Arrays and Records
TSP Python
Interface
TSP Python
Interface with inheritance
TSP Python
Operation
TSP Python
Operations with params spread
TSP Python
Operations with Union return type
TSP Python
Operation reuse
TSP Python
Basic Enum
With values
Enum with spread Enum
TSP Python
Beta Was this translation helpful? Give feedback.
All reactions