-
Notifications
You must be signed in to change notification settings - Fork 114
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
numpy numbers aren't reported as numbers #354
Comments
Numpy defines |
Yes. I think we didn't do that because we can't tell in advance if the integer number fits into any of the fixed size type. Now that interop supports BigInteger, we could report Note that numpy integers would still always report |
Could we assume/expect |
Correction: for integers, we should only consider
Not sure. In most normal libraries I wouldn't expect visible side-effects, but there's no rule saying that there can't be any. For example @eregon how does ruby approach this? |
For purposes of Enso we just need to know the object is a number. Then we'll treat the object as number and not as a generic object with members/hash entries. How big/precise the number is then not important. We support |
We don't have a way to override fitsInInt() in Ruby currently, but we have similar things for array and hash predicates such as You could do the same, e.g. |
In general I guess |
Yes, having a special "numpy integration module" (and other integration modules for other popular Python libraries) would deliver more reliable results faster than trying to extract a general purpose solution based on random coding patterns that appear in such libraries and are often considered fragile ("can it side-effect or not?") to rely on. Just my 2 Kč from a user of GraalVM that seeks solutions and ignores elegance. |
Closes #7677 by eliminating the _stackoverflow execption_. In general it seems _too adventurous_ to walk members of random foreign objects. There can be anything including cycles. Rather than trying to be too smart in these cases, let's just rely on `InteropLibrary.isIdentical` message. # Important Notes Calling `sort` on the `numpy` array no longer yields an error, but the array isn't sorted - that needs a fix on the Python side: oracle/graalpython#354 - once it is in, the elements will be treated as numbers and the sorting happens automatically (without any changes in Enso code).
Actually, I've been thinking about the comments here and I've come up with a proposal: import polyglot
import numpy
@polyglot.register_polyglot_behavior(numpy.int32)
class NumpyInt32Behavior:
@staticmethod
def fits_in_int(obj):
return True
@staticmethod
def as_int(obj):
return obj.__index__()
... Passing in the instance would let you do additional checks that cannot be infered based on the type, i.e. to implement We would ship our own definitions for standard library types like The tricky part is - when exactly would the definitions would get loaded? In the example, I have a dependency on |
I'd like to get a callback when certain module (like |
Python's import system is very customizable, it already has extension points to do that. There's a package importhook that gives you a friendly interface to do exactly what you described. |
Please let me know when the system is ready for experimenting. Thank you. |
Hi @JaroslavTulach! We have just merged an api that allows you to specify the interop behavior for external types. It consists of the import polyglot
class MyType(object):
data = 10
polyglot.register_interop_behavior(MyType, is_string=True, as_string=lambda t: f"MyType({t.data})") We plan on providing default behavior extensions for |
Thanks a lot @cosminbasca for letting me know. What's the best way to test latest Python bits? We are using following Maven co-ordinates to download the GraalPy packages. Are there any co-ordinates to download latest snapshots? |
Snapshots artifacts are currenlty not published on maven, but can be downloaded from https://github.com/graalvm/graalvm-ce-dev-builds/releases/ ( I believe that the new API provides you with enough flexibility to achieve what you need, so I'm going to mark this issue as fixed. |
numpy
defines its own numeric types. However GraalPy doesn't report them asInteropLibrary.isNumber
. That causes interoperability confusion. To reproduce store following file asNumpy.java
:and execute following commands:
this is causing us problems when sorting as the content of NumPy array isn't recognized as numbers, but treated as complex objects with members.
The text was updated successfully, but these errors were encountered: