Skip to content

InvalidCastException when calling FirstOrDefault on a PythonList in .NET 6 #812

Open

Description

@slozier

To reproduce:

import clr
import System
clr.AddReference("System.Linq")
clr.ImportExtensions(System.Linq)
[0].FirstOrDefault(lambda x: x == 2)

Fails with:

Error in IEnumeratorOfTWrapper.Current. Could not cast: from System.Int32 to IronPython.Runtime.PythonFunction

Works fine on .NET 5.0, fails on .NET 6.0. It looks like the T of IEnumeratorOfTWrapper<T> is PythonFunction instead of System.Object that we get on .NET 5.0.

Reported by @Simon900225 on gitter.

Activity

  1. Simon900225 commented on Dec 13, 2022

    @Simon900225
    Contributor

    Hi. I'd like to get this issue resolved in some way. Is there a way for my company to sponsor this? Or could I get some input on how I can troubleshoot, find and fix this issue myself?

  2. slozier commented on Dec 14, 2022

    @slozier
    ContributorAuthor

    .NET 6 added an overload FirstOrDefault<TSource>(IEnumerable<TSource>, TSource) so, other than being explicit about your type, I'm not sure there's a whole lot you can do for this one:

    import clr
    import System
    clr.AddReference("System.Linq")
    clr.ImportExtensions(System.Linq)
    [0].FirstOrDefault[System.Int32](lambda x: x == 2)
  3. Simon900225 commented on Dec 8, 2023

    @Simon900225
    Contributor

    I'm back in the project where I had this issue after a years break.
    Being explicit works, but we have a lot of code in different places so I'd have to create a regex that adds [System.Object] after every FirstOrDefault (and similar calls) to be sure that it works for every customer. The thing is I don't fully understand why the new overload should mess this up? If I run the new overload
    range(1,10).FirstOrDefault(lambda x: x == 20, 1) there is no issue.

    I have now downloaded the source of IronPython and managed to compile it so now I'm able to test a bit more. The thing is that I haven't manged to find where the FuncCallInstruction is generated where the return type is set to PythonFunction.

    The difference of the FuncCallInstruction between .NET 5 and .NET 8 looks like this
    {Call(PythonFunction FirstOrDefault[PythonFunction](System.Collections.Generic.IEnumerable1[IronPython.Runtime.PythonFunction], IronPython.Runtime.PythonFunction))}
    {Call(System.Object FirstOrDefault[Object](System.Collections.Generic.IEnumerable1[System.Object], System.Func2[System.Object,System.Boolean]))}

  4. Simon900225 commented on Dec 13, 2023

    @Simon900225
    Contributor

    Another error found both when using the implicit and the explicitly typed call
    List[Object]().FirstOrDefault[System.Object](lambda x: x == 10)
    List[Object]().FirstOrDefault(lambda x: x == 10)
    returns
    <function <lambda$29> at 0x0000000000000033> when it should return None

    This also stopped working from .NET 6 and forward.
    I think this is caused by the same error. It seems like it returns the lambda function and not the result of the lambda function. If you add the default value parameter as None it works but it should understand that implicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions