Draft
Conversation
3 tasks
- src/CSnakes.Runtime/Python/PyObjectImporters.cs - src/CSnakes.SourceGeneration/Parser/PythonParser.TypeDef.cs - src/CSnakes.SourceGeneration/Reflection/TypeReflection.cs - src/CSnakes.SourceGeneration/ResultConversionCodeGenerator.cs - src/CSnakes.Tests/GeneratedSignatureTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR is a super early draft of an API and a vision to eventually help address custom classes being discussed in #397. One idea builds on top of another and they can be forked and brought in separately. Meanwhile, I think it helps to see them in action together and where one feature can help drive the shape of another.
The two features are:
PyObjectconversions via LINQThe first abstraction being introduced is a
PyObjectreader:Then, a whole set of implementation methods are housed in
PyObjectReaderthat enable computational expressions overPyObject. The computational expressions are integrated into C# via LINQ such that one can express something as simple as the following:Note that
readeris defined before and independent of anyPyObjectinstance, which enables some interesting things. A second abstraction is for types that know how to read or convert themselves from aPyObject:Now suppose you have the following in Python (the
dataclassbelow could be a Pydantic model too):In C#, you can define the desired shape and provide a reader expressed in LINQ that knows exactly how to convert one into the other:
What's nice is that the C# type doesn't have to match the shape of the Python type. The names and types can differ as demonstrated above.
I also went ahead and update the source generator so that you can express in Python what the resulting C# class will be via an
Annotatedtype:The annotation is honoured in the return type position only, including being deeply nested in container types like
listanddict(shown above). If the source generator sees a return type with metadata that contains a string literal starting withc#:then the rest of the type name is taken verbatim to identify a C# class that implementsIPyObjectReadable<T>. The generated code will automatically use the reader of the type to return the strong-typed class! If you look at the integration test, the C# side only seesFooBarBazand natural .NET types:It is still possible to build on top of all of this where attributes can be decorated on a class:
and the source generator will complete with another partial definition holding the
IPyObjectReadable<Person>implementation.Pros of this design are:
class/struct,recordor not) and the source generator can tightly integrate itCons of this design are:
Historical design notes from before merge of PR #728
I also went ahead and update the source generator so that you can express in Python what the resulting C# class will be via a magic forward reference:
The parser is updated to allow [forward references] in the return type position (only), including being deeply nested in container types like
listanddict(shown above). If the source generator sees a forward reference (a quoted string) and it begins with the magic module name__extern__then the rest of the type name is taken verbatim to identify a C# class that implementsIPyObjectReadable<T>. The generated code will automatically use the reader of the type to return the strong-typed class! If you look at the integration test, the C# side only seesFooBarBazand natural .NET types:The forward reference is really a hack. If we go ahead with this overall design then the parser should be enhanced to support type annotation metadata via
typing.Annotated, so that the external reference is more naturally expressed as metadata (keeping Python type-checkers happy too):