TSP → Python conversions #8220
Replies: 2 comments 1 reply
-
|
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.
-
|
Thanks for all the discussion: the result of the discussions above (and others more) resulted on the Python Emitter Framework implementation that was landed yesterday in the Typespec repo: https://github.com/microsoft/typespec/tree/main/packages/emitter-framework/src/python |
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
This document reflects the current implementation proposed in #8966.
These conversion patterns aim to cover Python 3.11+.
Models with arbitrary number of properties (Record spreads)
Status: ❌ NOT IMPLEMENTED
Due to limitations on how to handle a dynamic number of properties, Record-based models are currently not supported.
Record model
TSP:
Python:
Model with is operator to Record
TSP:
Python:
Model that extends a Record
TSP:
Python:
Model that has a spread to a Record
TSP:
Python:
Basic Models
Model
TSP:
Python:
Model with a field that can be null
TSP:
Python:
Model with an optional field
TSP:
Python:
Model with a field that has a default
TSP:
Python:
Model with an optional field that can be null
TSP:
Python:
Model with literal union types
TSP:
Python:
Never Type
Model with never-typed property
TSP:
Python:
Generic model instantiated with never (using 'is')
TSP:
Python:
Generic model instantiated with never (using 'extends')
TSP:
Python:
Model Inheritance
Model Inheritance - Extends
TSP:
Python:
Model Inheritance - Spreads
TSP:
Python:
Model Inheritance - Is
TSP:
Python:
Model Inheritance - Extends with generic
TSP:
Python:
Special Model Types
Model with Array base (is Array)
TSP:
Python (using
TypeDeclaration):Python (using
ClassDeclaration):Unions
TSP:
Python:
Multiple Model Spreads
TSP:
Python:
Arrays and Records in properties
Arrays and Records in properties
TSP:
Python:
Interfaces
Interface
TSP:
Python:
Interface with inheritance
TSP:
Python:
Operations
Operation
TSP:
Python:
Named operation reference
TSP:
Python:
Enums
Enum with integer values
TSP:
Python:
Enum with string values
TSP:
Python:
Enum extension
TSP:
Python:
Union as Enum
TSP:
Python:
Protocols (Alternative Interface Representation)
Operation as Protocol callback
When using
ProtocolDeclarationfor operations:TSP:
Python:
Interface as Protocol
When using
ProtocolDeclarationfor interfaces:TSP:
Python:
Tuples
Tuple type
TSP:
Python:
Template Constraints (Bounded TypeVars)
Template parameter with upper bound constraint
TSP:
Python:
Multiple template parameters with mixed constraints
TSP:
Python:
Beta Was this translation helpful? Give feedback.
All reactions