| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
Implicit casts to float were disabled intentionally. For instance, float(torch.tensor(42)) is a float. However, if implicit casts are enabled, Python.NET can not choose an overload between float and Tensor if the C# side has a Tensor wrapper via a custom codec. |
Sorry, something went wrong.
|
The (expected) order here is:
This should lead to a sensible overload resolution. |
Sorry, something went wrong.
This can never happen with a custom Python type. Also, we do not really perform overload resolution prioritization like that. E.g. the __call__ impl just passes the object to Convert and checks if conversion succeeded. |
Sorry, something went wrong.
|
Can you please provide a test-case that you expect to fail with this PR? I'm fine with working on the overload resolution, but I think making CsharpFunc(float_like) fail is complicating things on the user side in a very error-prone manner. |
Sorry, something went wrong.
|
I described the test case above.
See it pick the overload based on the weather. This is because we actually do not perform any overload resolution, but this behavior prevents user from forcing exact type match with codecs when the type on Python side has __float__. If somebody wants this behavior, they can register a codec for types implementing __float__ or just explicitly cast their floaty types to float. What we might want is for System.Double(np_float64_val) to produce a Double value (which I am not sure it does currently)`. |
Sorry, something went wrong.
|
@filmor I think we still should not just enable __float__ for all types that have it, because there are bogus ones that do that and should never be converted without an explicit cast. Implicit conversion should be reserved only for types, that explicitly represent identically sized numbers, e.g. numpy.float64, ctypes.int32, etc |
Sorry, something went wrong.
|
That effectively boils down to just using the "real" Runtime.PyFloat_Check instead of our botched version. I'd be completely fine with that. |
Sorry, something went wrong.
|
Just to recap: This will now check before attempting a conversion whether the object identifies as a float using PyFloat_Check, which has been adjusted in the way @rzindler proposed in #1936. All other changes are just cosmetics (plus one minor bug-fix adjustment for the Int64 conversion) and test-cases. |
Sorry, something went wrong.
|
@lostmsu If this is mergeable for you, I think that would solve the reported issue sufficiently that we can continue with the releases without having to touch method resolution right now (which I also really don't want to). PyTorch's tensor does not derive from float64, so this particular case should not be a problem. |
Sorry, something went wrong.
|
My problem with this approach is that it does not allow us to do: string Foo(float x) => "float32";
string Foo(double x) => "float64";Foo(numpy.float32(42.0)) |
Sorry, something went wrong.
It does, you just need to be explicit. But I get your point, I just think that the reasonable default is more important than optimising this overload. We can document this particular issue. |
Sorry, something went wrong.
|
Thanks guys! |
Sorry, something went wrong.
|
I think we can do better than document, and actually special case ctypes, NumPy, PyTorch, and TensorFlow types that have exact precision. Preferably also leave a room for users to special case other precise types. It is not that hard, and if we don't do it now we'd have to wait until 4.0 to make another breaking change. |
Sorry, something went wrong.
|
I'm strongly against special-casing particular library types. If you want to allow proper float32, that's an entirely new issue. Allowing modifying primitive type conversion as well. |
Sorry, something went wrong.
|
That's exactly what I want to do in a week or two. |
Sorry, something went wrong.
I'll merge this in the meantime. Feel free to revert or adjust the behaviour when you are attempting your refactoring. In this state, I'd be fine to publish it. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
What does this implement/fix? Explain your changes.
Uses __float__ when available if a Python object is passed to a function with a Double or Single parameter.
Does this close any currently open issues?
#1833
Any other comments?
This is not yet supported for integer types, which would use __int__
or __index__ (for Python >=3.10), as the respective logic is for some
reason only implemented for AsLongLong and AsLong, not for the
unsigned counterparts and also not for the AsSize_t variants that we
are using.
Checklist
Check all those that are applicable and complete.