-
Notifications
You must be signed in to change notification settings - Fork 751
A way to override Python.NET .NET <-> Python wrapping for custom classes #823
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
Comments
It would be great to have a feature like this. I'd be in favour of option 3), as redesigning this to be extensible would probably allow us to fix a ton of open bugs that are related to either too eager or incomplete conversions. Regarding 2), I've implemented this in a hacky way from Python's side for classes deriving directly from |
@filmor , can you link the bugs, that you think might benefit from the updated overload resolution? |
Fixed in #1022 |
Environment
Details
I am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (see Gradient).
Since I am automatically generating .NET-side types for wrappers, I can make their methods to use my custom converter written in C# to convert function arguments to
PyObject
instances. And then for any result values, I can convertPyObject
instances back into my wrapper types.Problem I am facing is when a user of my wrapper wants to inherit from one of the Python-derived classes, and override and/or extend its behavior.
An artificial sample:
The last line will fail, because Python will attempt to call
MyBetterSession.Run
with Python's classtf.Tensor
, but the method actually expects wrapped classTensor
. I need to somehow tell Python or rather Python.NET to invoke myConvertFromPyObject
on the argument, before trying to find a matching overload.I looked into Python.NET source, and the corresponding objects are
MethodBinding
andMethodObject
, however, neither seem to provide any extensibility.Now I am considering several approaches to the problem, and I just wanted to discuss them with Python.NET team, as some of them involve modification of Python.NET.
MyBetterSession
generate a pythonic wrapper with the same set of methods, but replacing all parameter and return types withPyObject
. It would require one of the following:System.Reflection.Emit
, which is a very heavy dependency, and also not very pleasant to work withI noticed, that while Python.NET supports representing any Python object as
dynamic
, it does not actually support passingdynamic
objects (e.g.IDynamicMetaObjectProvider
instances) back to Python. I mean, it would pass them, but if Python would try to access a dynamic attribute, it would not attempt to callTryGetMember
. I could potentially implement something similar toClassBase
specifically for wrappingIDynamicMetaObjectProvider
instances. Then it is much easier to implement its members, that would simply wrap PyObject arguments before forwarding them to an instance ofMyBetterSession
.Have Python.NET directly expose some low-level interface, that would enable hooking into Python.NET's marshaling and, possibly, also method binding processes.
The text was updated successfully, but these errors were encountered: