Skip to content

List conversion fails on new version of pythonnet compared to 3.0.0.post1 #2004

@screig

Description

@screig

Environment

  • Pythonnet version:
    Works with pythonnet==3.0.0.post1
    Failes with pythonnet>=3.0.0.post1

  • Python version:
    Python 3.8.0

  • Operating System:
    Windows

  • .NET Runtime:
    netstandard2.0;net471

Details

I followed the notes to write a codec to convert a C# List to a Python list, here it is:

  /// <summary>
   /// This class will convert a C# list of strings to a Python list [] of strings.
   /// </summary>
   public class ListStringConversions : IPyObjectEncoder
   {
       bool IPyObjectEncoder.CanEncode(Type type)
       {
           return type == typeof(IList<string>);
       }

       PyObject IPyObjectEncoder.TryEncode(object value)
       {
           using (var scope = Py.CreateScope())
           {
               scope.Exec($"a = []");
               var input = (List<string>)value;
               foreach (var item in input)
                   scope.Exec($"a.append('{item}')");  // {item}
               return scope.Get("a");
           }
       }
   }

I have a simple test which calls in to a C# assembly and returns a list. With pythonnet version 3.0.0.post1, the C# list is successfully converted to a python list.

In my requirements file, I had this
pythonnet>=3.0.0.post1

Now on the CICD process it has updated the pythonnet package and the same test fails:

>       assert isinstance(codes, list)
E       assert False
E        +  where False = isinstance(<System.Collections.Generic.IList[String] object at 0x0000023C1D9D5180>, list)

The code no longer is returning a python list. I switched the requirements file to
pythonnet==3.0.0.post1

and the test passes. As such I think there is a breaking change in the latest release that prevents the conversions been executed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions