-
Notifications
You must be signed in to change notification settings - Fork 747
Stricter coversion of primitive Python types to System.Object
#1958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…, and `str` to corresponding .NET primitive types fixes pythonnet#1957
This should go into 3.0.0 |
3.0.0 ia released already and I disagree with the proposed behavior. I don't get why you are insisting that this is the "correct" way. If a type is derived from another that clearly indicates an "is a" relation, and evidently people got really confused with the behavior we had in the RCs. I totally agree that we should allow overriding this behavior, I disagree with this approach. |
@filmor I don't think you got the scenario. It is not about converting Without this change codecs can't be applied because the conversion to primitives takes priority. Converting |
@filmor any comments? Due to the above type information loss either this change needs to be accepted to be able to avoid the issue, or the checks need to move below codec application, which can lead to poorly predictable behavior for codecs that claim to be able to decode |
I'm on vacation right now, will check once I'm back. I find it unlikely that I will accept the PR as it stands, but I'll need to be a bit around with it. |
@filmor I think I came up with a good demonstration why this PR should be accepted as is. Without it the following code will work for any numpy value except [ForbidPythonThreads]
dynamic Centroid(dynamic value) => value - value.mean(); v = numpy.float32(42)
print(Centroid(v)) # OK
v = numpy.float64(42)
# RuntimeBinderException: System.Double does not have .mean()
print(Centroid(v)) Codecs are not involved here, so the conversion should not happen regardless of the state of the codecs. |
With that argument, we should forbid /any/ conversion of primitive Python types to |
@filmor I don't think it applies to primitive types, as
|
I'm not sure what you mean by that. How many would be "too many"?
Exactly. And I think instead of breaking working code further, we should just make this an option. There are (to me) two kinds of functions for which this is relevant:
For the latter, I don't see any reason why these shouldn't just take a For the former, things might be a bit more tricky, but I'd suggest (in general) to just expose more of the conversion machinery publicly and solve this case using a |
Then you proceed to name a reason. I'd also say that the only good reason to have a |
That is not the point.
And I also give a way to make this work by being more explicit. That is the gist of a lot of the changes for Python.NET 3 (that you also introduced yourself!). There is no way to distinguish |
Point is why even do it this way in the first place? What is the scenario where |
We agree here. I just want to not break the behaviour right now, and if we break it, remove all special-casing in one go instead of keeping it for (some) primitives. If we want to keep things simple, we can make this a runtime configuration parameter. |
@filmor as far as I am concerned, this issue is a consequence of introducing last minute changes to 3.0 before release. And considering its use case rarity, it should be considered a bug in 3.0.0, so should be fixed in 3.0.1. If you are unhappy about interface breakage, 3.0.0 should be yanked. |
It is not. As said, with the change as it stands, I'm not okay and I think we have discussed it enough now. The correct thing to do would be to not convert at all for |
@filmor I don't think we need a global toggle for this. There was another option:
That way this could be fixed with a codec. |
…s when target type is System.Object useful to be able to change what numpy.float64 is converted to related to pythonnet#1957 this is an alternative to pythonnet#1958
Closing this in favour of #1986. |
…s when target type is System.Object useful to be able to change what numpy.float64 is converted to related to pythonnet#1957 this is an alternative to pythonnet#1958
When trying to convert to
System.Object
, only convertfloat
,bool
, andstr
to corresponding .NET primitives types. Do NOT convert instances of types derived from them.What does this implement/fix? Explain your changes.
This changes
Converter
to perform exact type checks when target type isSystem.Object
Does this close any currently open issues?
fixes #1957
Checklist
Check all those that are applicable and complete.