Skip to content

Conversation

@syntonym
Copy link
Contributor

@syntonym syntonym commented Sep 3, 2025

The classes loro.LoroMap, loro.LoroList and loro.LoroMovableList indicate by name that they are Mappings resp. Sequences. However, some methods that are expected of python mappings/sequences are not implemented, especially setitem, getitem and delitem, which allow to use syntax of the form map["x"] = value, map["x"] and del map["x"] (similar for the sequences). This commit implements these three methods, making the API a bit more pythonic.

Even with these methods implemented, the classes are not considered Mappings or Sequences by python, as indicated by isinstance(loro.LoroMap(), collections.abc.Mapping) and isinstance(loro.LoroList(), collections.abc.Sequence). This requires at least implementing __iter__.

The python syntax l[0:3] calls __getitem__ with a slice object, while l[3] calls __getitem__ with an isize. To support both I introduced SliceOrInt. I wasn't quite sure where this struct should live, currently it is defined in both list.rs and mutable_list.rs, but I assume it should only be defined once.

The code currently also produces the warning type `list::SliceOrInt<'py>` is more private than the item `list::LoroList::__getitem__.

@Leeeon233
Copy link
Member

So sorry I missed this! I just saw the notification for this PR.
Really appreciate you making loro-py more Pythonic - thank you!

@Leeeon233 Leeeon233 merged commit 83f35fa into loro-dev:main Sep 29, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants