Skip to content
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

Better syntax for accessing existing shared types #76

Closed
dlqqq opened this issue Feb 8, 2024 · 2 comments · Fixed by #77
Closed

Better syntax for accessing existing shared types #76

dlqqq opened this issue Feb 8, 2024 · 2 comments · Fixed by #77
Labels
enhancement New feature or request

Comments

@dlqqq
Copy link
Collaborator

dlqqq commented Feb 8, 2024

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.
  • The inline assignment operator := (available in Python 3.8+) does not work either:
/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
  • From @davidbrochart:

    BTW you can write doc["my_array"] = my_array = Array() if you want a one-liner.

  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

@dlqqq dlqqq added the enhancement New feature or request label Feb 8, 2024
Copy link

welcome bot commented Feb 8, 2024

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! 🤗

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! 👋

Welcome to the Jupyter community! 🎉

@davidbrochart
Copy link
Collaborator

See #77.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants