-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Python unions #2427
Implement Python unions #2427
Conversation
A new generated diff is ready to view.
A new doc preview is ready to view. |
Wonder if we can leverage type narrowing here. So we generate the following: #[pyo3]
struct Square;
#[pyo3]
struct Circle;
// :type typing.Union[Square, Circle]:
enum Shape {
Square(Square),
Circle(Circle),
}
impl IntoPy<PyObject> for Shape {
fn into_py(self, py: Python<'_>) -> PyObject {
match self {
Shape::Square(s) => s,
Shape::Circle(c) => c,
}
}
}
impl FromPyObject<'_> for Shape {
fn extract(obj: &PyAny) -> PyResult<Self>{
if let Ok(s) = obj.extract::<Square> {
Ok(Shape::Square(s))
} else if let Ok(c) = obj.extract::<Circle> {
Ok(Shape::Circle(c))
} else {
Err("invalid shape")
}
}
} and users can write: # We will generate this type-stub for enums:
Shape = Square | Circle
def stringfy(shape: Shape) -> str:
if isinstance(shape, Square):
return "■"
# Here type-checker knows `shape` must be `Circle`
return "●" I believe with Python 3.10 they can even write: def stringfy(shape: Shape) -> str:
match shape:
case Square:
return "■"
case Circle:
return "●"
case _:
raise NotReachable() 🤯 |
A new generated diff is ready to view.
A new doc preview is ready to view. |
Unfortunately that is not enough as it will not cover nested unions like union Something {
member_a: AnotherUnion
member_b: Integer
}
union AnotherUnion {
member_a: Integer
member_b: String
} |
A new generated diff is ready to view.
A new doc preview is ready to view. |
...otlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt
Outdated
Show resolved
Hide resolved
...in/software/amazon/smithy/rust/codegen/server/python/smithy/RustServerCodegenPythonPlugin.kt
Show resolved
Hide resolved
...in/software/amazon/smithy/rust/codegen/server/python/smithy/RustServerCodegenPythonPlugin.kt
Show resolved
Hide resolved
A new generated diff is ready to view.
A new doc preview is ready to view. |
A new generated diff is ready to view.
A new doc preview is ready to view. |
A new generated diff is ready to view.
A new doc preview is ready to view. |
A new generated diff is ready to view.
A new doc preview is ready to view. |
A new generated diff is ready to view.
A new doc preview is ready to view. |
A new generated diff is ready to view.
A new doc preview is ready to view. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for doing this 🎉
...otlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt
Show resolved
Hide resolved
...otlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerSymbolProvider.kt
Show resolved
Hide resolved
...are/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerUnionGenerator.kt
Outdated
Show resolved
Hide resolved
...are/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerUnionGenerator.kt
Outdated
Show resolved
Hide resolved
...otlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt
Show resolved
Hide resolved
...otlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerSymbolProvider.kt
Show resolved
Hide resolved
...are/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerUnionGenerator.kt
Outdated
Show resolved
Hide resolved
...are/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerUnionGenerator.kt
Outdated
Show resolved
Hide resolved
A new generated diff is ready to view.
A new doc preview is ready to view. |
A new generated diff is ready to view.
A new doc preview is ready to view. |
A new generated diff is ready to view.
A new doc preview is ready to view. |
...are/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerUnionGenerator.kt
Outdated
Show resolved
Hide resolved
A new generated diff is ready to view.
A new doc preview is ready to view. |
Signed-off-by: Bigo <[email protected]>
A new generated diff is ready to view.
A new doc preview is ready to view. |
Motivation and Context
Continue work on #1367.
Description
Implement support for the
union
shape in the Python server.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.