-
Notifications
You must be signed in to change notification settings - Fork 17
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
Casting numbers #55
Comments
Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! 🤗 |
IIRC these are workarounds for the limitation of Yjs to keep track of integers. I remember a discussion with @dmonad and @Waidhoferj, where we concluded that once an integer lands on the JS side, it comes back to Python as a float and there is no way to remember it was an integer. |
Yjs operates with JavaScript types, which means that it doesn't make a distinction between We talked about adding a wrapper type or marker to the protocol in order to distinguish between the types, but it is not actively being worked on. |
Thanks John. |
Just a bit more background: It's not very unusual for message formats to generalize the concept of Technically, we can't even represent all int numbers. We can only represent integers that can be encoded as a double (int53 + a few more). We could actually discuss whether we only want to return floats (double) in Ypy. If I remember correctly, there is some reason why we don't do that already. |
It is true that in the JavaScript world, not making a difference between an integer and a float is usually not a problem, because the language is quite permissive, e.g. for indexing: a = [1, 2]
a[1.0]
// 2 But it is not the case with Python: a = [1, 2]
a[1.0]
# TypeError: list indices must be integers or slices, not float The notebook format says that |
I'm not denying that this isn't a problem. However, there is no solution for it that makes everyone happy. If we only had Ypy to think about. I'd definitely agree with you to distinguish between float and integer (as this is what python does). However, JupyterLab still uses Yjs on the frontend. Once a client increments // this might be a value set by python (integer)
const oldValue = ycell.get('execution_count')
// Once we increment the value in Javascript, we will set it as a double.
ycell.set('execution_count', oldValue + 1) Since this value passes through JavaScript, we can't determine anymore whether we want to interpret this as an integer or a float. All numbers in JS are doubles. The same is true for a lot of other programming languages. Now, we could argue whether we want to return the "closest interpretation". Obviously, the number is still a natural number, even though it's stored as a double. So we could simply return an integer in Ypy. But would the result of Ultimately, this leads to confusion, and you still have to do type casting to get the expected results. We can probably solve this problem for Jupyterlab and the execution_count indexing. In the larger scope of things, however, we want to have cross-compatible Y* implementations that enable applications written in different programming languages to communicate. Other languages support many more types of number: My point is that there is a difference between the concept of "number" and how we store that number (float, double, int32, ..). The protocol (Y*) specifies that numbers must be doubles as it is the least common denominator that all languages support. It is up to the application-developer to interpret, cast, and use it. Hence, I think the only course of action is to make double the only default number type. I don't see any other solution that doesn't exclude some programming languages. Unfortunately, you have to do number conversions, but that needs to happen anyway at some point. At least the result is deterministic, and you will always know what to expect when you receive a number. |
Thanks Kevin for the arguments, that makes sense.
I remember that for the latter, if we don't cast to float, Ypy tries to pass the value to Yjs as a |
Closing as fixed by #57. |
Could we add code comments for the reason to cast number
float
<->int
? Like atjupyter_ydoc/jupyter_ydoc/ydoc.py
Line 89 in 043163b
Is it possible to reduce the scope of conversion to reduce the load and avoid troubles? At first glance, that cast sounds like a source of hard-to-find errors in the future especially when applying it to cell serialization.
The text was updated successfully, but these errors were encountered: