Skip to content

Inconsistent Conversion of Sequence Types #909

Closed

Description

@rmadsen-ks

Environment

  • Pythonnet version: 2.4 (build from df0574d)
  • Python version: 2.7, 3.6, 3.7
  • Operating System: Windows 10

Details

I am trying to convert from List to PyList and back without losing type information.

            // this works:
            int[] values2 = new int[] { 1, 2, 3 };
            var pyvalues2 = values2.ToPython();
            pyvalues2.As<int[]>();

            // this does not work.
            List<int> values = new List<int> { 1, 2, 3 };
            var pyvalues = values.ToPython();
            pyvalues.As<List<int>>(); // unable to convert back to List<int>

It seems ToPython will just take any IEnumerable or IList and turn it into a python list. Even though it is not generally possible to convert it back. In this case I'd rather keep it as the original type, because eventually I'll need to get it back to C#.

I understand why the current behavior makes things easier, but it would be great if it was possible to disable or alter this behavior.

Activity

  1. filmor commented on Jul 10, 2019

    @filmor
    Member

    I agree, the whole conversion story of Python.NET is currently quite inconsistent. Before we apply more band-aids, I'd like to have an overall plan, though, s.t. if we have to break the API we make it count :)

    Essentially, we need a set of wrapping/unwrapping functions and additionally a (separate) set of functions for conversion (for convenience).

  2. rmadsen-ks commented on Jul 10, 2019

    @rmadsen-ks
    ContributorAuthor

    I think this could be done without breaking the API.

    I was thinking something in the style of:

     values.ToPython(ImplicitConvert: false); 
  3. lostmsu commented on Sep 23, 2021

    @lostmsu
    Member

    The particular issue with List<T> should have been fixed in 3.0

  4. added this to the 3.0.0 milestone on Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions