0

In pythonnet, I'm trying to implement an interface with a generic method. To be more precise, I'm trying to implement an custom decoder in python, so I would like to implement IPyObjectDecoder, which has a generic TryDecode method.

Here is my skeleton for a DateTime <=> datetime toy sample:

from System import DateTime
from Python.Runtime import PyObjectConversions, IPyObjectEncoder, IPyObjectDecoder
from datetime import datetime

class DatetimeDecoder(IPyObjectDecoder):
    __namespace__ = "Tests.Codecs"

    def CanDecode(self, object_type, clr_type):
        return (
            object_type == datetime
            and clr_type.Name == "DateTime"
            and clr_type.Namespace == "System"
        )

    def TryDecode(self, value, out):
        return (False, 1)

datetime_decoder = DatetimeDecoder()
PyObjectConversions.RegisterDecoder(datetime_decoder)

But this code fails on class definition with:

Failed: [undefined]TypeError: Method 'TryDecode' in type 'Tests.Codecs.DatetimeDecoder' from assembly 'Python.Runtime.Dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

I assume it comes from the genericity of the method (but maybe it's the out parameter as well - but for this one I understood I should have a dummy parameter and return a tuple), is it possible to do that in python ?

Thank you

0 Answers0