question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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<strings> 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.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:19 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
screigcommented, Nov 8, 2022

I made you a sample project

https://github.com/screig/pythonnet_test

It runs the tests under Github actions, there are two jobs. Which are identical apart from, one does this

   pip install pythonnet==3.0.0.post1

and the other, does this:

   pip install pythonnet==3.0.1

https://github.com/screig/pythonnet_test/actions/runs/3418546969

As you can see 3.0.1 fails

0reactions
lostmsucommented, Nov 10, 2022

I don’t think there’s anything for us to fix here. This is an unsupported situation. Detect and raise.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · pythonnet/pythonnet
pythonnet 3.0 not able to resolve default constructor for a struct in VB.net ... List conversion fails on new version of pythonnet compared...
Read more >
Basic Example for VS with Python - NI Community
Hello. Anyone got a basic script for non-deterministically reading an writing to a channel in Python using the verion 2.0.x?
Read more >
pythonnet
Python.NET is a package that gives Python programmers nearly seamless integration with the .NET Common Language Runtime (CLR) and provides a powerful ...
Read more >
pythonnet
Hello - this is my first post so apologies if this is the wrong forum. I'm trying to define a base class in...
Read more >
pythonnet | gives Python programmers nearly seamless ...
Implement pythonnet with how-to, Q&A, fixes, code snippets. kandi ratings - Medium support, 8 Bugs, 1001 Code smells, Permissive License, Build available.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found