jupyter-server / pycrdt

CRDTs based on Yrs.
https://jupyter-server.github.io/pycrdt
MIT License
37 stars 9 forks source link

Better syntax for accessing existing shared types #76

Closed dlqqq closed 6 months ago

dlqqq commented 6 months ago

Problem

Currently, one must bind an empty shared type to Ydoc keys before accessing their values:

    ydoc["cells"] = Array()
    assert ydoc["cells"].to_py() == [{"metadata": {"foo": "bar"}, "source": "1 + 2"}]
    #      ^- not equal to the empty Array() assigned to this key immediately before,
    #         but rather the value coming from another provider 

There are two drawbacks to only supporting this way of accessing a shared type:

  1. This requires 1 additional line of code per shared type for the assignment statement, and can get verbose if one is using many. However, in Yjs, ydoc.getArray(...) can be used inline.
/Users/dlq/micromamba/envs/rtcdev/lib/python3.11/ast.py:50: in parse
    return compile(source, filename, mode, flags,
E     File "/Volumes/workplace/pycrdt-websocket/tests/test_pycrdt_yjs.py", line 92
E       assert (ydoc["cells"] := Array()).to_py() == [{"metadata": {"foo": "bar"}, "source": "1 + 2"}]
E               ^^^^^^^^^^^^^
E   SyntaxError: cannot use assignment expressions with subscript
  1. The syntax requires assigning an empty shared type in order to access an existing, non-empty shared type. This is stateful and confusing, because the value of ydoc["cells"] is not the value of ydoc["cells"] that was just set in the immediately preceding line. This generally violates how most programming languages work. I understand that this is permitted by Python, but the public API should not rely on exotic behavior exclusive to Python.

Proposed Solution

TBD.

Additional context

welcome[bot] commented 6 months ago

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively. welcome You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! :wave:
Welcome to the Jupyter community! :tada:

davidbrochart commented 6 months ago

See #77.