You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
InvalidCastException when calling FirstOrDefault on a PythonList in .NET 6 #812
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.
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?
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]))}
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.
Activity
Simon900225 commentedon Dec 13, 2022
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?
slozier commentedon Dec 14, 2022
.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:Simon900225 commentedon Dec 8, 2023
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
FuncCallInstructionis 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]))}Simon900225 commentedon Dec 13, 2023
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 NoneThis 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.