Skip to content

Commit

Permalink
hopefully now actually py3.8 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Mar 19, 2024
1 parent 86e9040 commit 1da5f2f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ jobs:
release:
name: Release
runs-on: ubuntu-latest
if: "startsWith(github.ref, 'refs/tags/')"
if: ${{ startsWith(github.ref, 'refs/tags/') }}
needs: [linux, windows, macos, sdist]
steps:
- uses: actions/download-artifact@v4
Expand Down
11 changes: 6 additions & 5 deletions src/automerge/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from datetime import datetime
from typing import Union, Dict, List, Tuple
from .. import _automerge
from .._automerge import *

ScalarValue = str | bytes | int | float | bool | datetime | None
Thing = dict[str, 'Thing'] | list['Thing'] | ScalarValue
Value = ObjType | tuple[ScalarType, ScalarValue]
ScalarValue = Union[str, bytes, int, float, bool, datetime, None]
Thing = Union[Dict[str, 'Thing'], List['Thing'], ScalarValue]
Value = Union[ObjType, Tuple[ScalarType, ScalarValue]]

def extract(doc: Document, obj_id: bytes = ROOT) -> Thing:
ot = doc.object_type(obj_id)
if ot == ObjType.Map:
d: dict[str, Thing] = {}
d: Dict[str, Thing] = {}
for k in doc.keys(obj_id):
x = doc.get(obj_id, k)
assert x is not None
v, id = x
d[k] = extract(doc, id) if isinstance(v, ObjType) else v[1]
return d
elif ot == ObjType.List:
l: list[Thing] = []
l: List[Thing] = []
for k2 in range(0, doc.length(obj_id)):
x = doc.get(obj_id, k2)
assert x is not None
Expand Down
30 changes: 15 additions & 15 deletions src/automerge/document.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import automerge.core as core
from datetime import datetime
from typing import Iterator, cast, Mapping, Sequence, MutableMapping, MutableSequence, overload, TypeAlias, Iterable, KeysView
from typing import Iterator, cast, Mapping, Sequence, MutableMapping, MutableSequence, overload, Iterable, List, Union, Tuple, Optional
from contextlib import contextmanager

class ActorId:
Expand All @@ -19,14 +19,14 @@ class ChangeHash:
def __init__(self, hash: bytes):
self.hash = hash

ProxyThing: TypeAlias = 'core.ScalarValue | Mapping[str, ProxyThing] | Sequence[ProxyThing]'
ProxyThing = Union[core.ScalarValue, Mapping[str, 'ProxyThing'], Sequence['ProxyThing']]

class ReadProxy:
_doc: core.Document
_obj_id: bytes
_heads: list[bytes] | None
_heads: Union[List[bytes], None]

def __init__(self, doc: core.Document, obj_id: bytes, heads: list[bytes] | None):
def __init__(self, doc: core.Document, obj_id: bytes, heads: Optional[List[bytes]]):
self._doc = doc
self._obj_id = obj_id
self._heads = heads
Expand All @@ -37,7 +37,7 @@ def __len__(self) -> int:
def to_py(self) -> core.Thing:
return core.extract(self._doc, self._obj_id)

def _maybe_wrap(self, x: tuple[core.Value, bytes]) -> 'MapReadProxy | ListReadProxy | core.ScalarValue':
def _maybe_wrap(self, x: Tuple[core.Value, bytes]) -> 'MapReadProxy | ListReadProxy | core.ScalarValue':
value, obj_id = x
if isinstance(value, core.ObjType):
if value == core.ObjType.List:
Expand All @@ -62,7 +62,7 @@ class ListReadProxy(ReadProxy, Sequence[ProxyThing]):
def __getitem__(self, key: int) -> ProxyThing: ...
@overload
def __getitem__(self, key: slice) -> Sequence[ProxyThing]: ...
def __getitem__(self, key: int | slice) -> ProxyThing | Sequence[ProxyThing]:
def __getitem__(self, key: Union[int, slice]) -> Union[ProxyThing, Sequence[ProxyThing]]:
if not isinstance(key, int): raise NotImplemented
x = self._doc.get(self._obj_id, key, self._heads)
if x is None: raise IndexError()
Expand All @@ -71,22 +71,22 @@ def __getitem__(self, key: int | slice) -> ProxyThing | Sequence[ProxyThing]:
class WriteProxy:
_tx: core.Transaction
_obj_id: bytes
_heads: list[bytes] | None
_heads: Optional[List[bytes]]

def __init__(self, tx: core.Transaction, obj_id: bytes, heads: list[bytes] | None) -> None:
def __init__(self, tx: core.Transaction, obj_id: bytes, heads: Optional[List[bytes]]) -> None:
self._tx = tx
self._obj_id = obj_id
self._heads = heads

def __len__(self) -> int:
return self._tx.length(self._obj_id, self._heads)

MutableProxyThing: TypeAlias = 'core.ScalarValue | MutableMapping[str, MutableProxyThing] | MutableSequence[MutableProxyThing]'
MutableProxyThing = Union[core.ScalarValue, MutableMapping[str, 'MutableProxyThing'], MutableSequence['MutableProxyThing']]

class MapWriteProxy(WriteProxy, MutableMapping[str, MutableProxyThing]):
_tx: core.Transaction

def __getitem__(self, key: str | int) -> MutableProxyThing:
def __getitem__(self, key: Union[str, int]) -> MutableProxyThing:
x = self._tx.get(self._obj_id, key, self._heads)
if x is None: return None
value, obj_id = x
Expand Down Expand Up @@ -134,7 +134,7 @@ class ListWriteProxy(WriteProxy, MutableSequence[MutableProxyThing]):
def __getitem__(self, key: int) -> MutableProxyThing: ...
@overload
def __getitem__(self, key: slice) -> MutableSequence[MutableProxyThing]: ...
def __getitem__(self, key: int | slice) -> MutableProxyThing | MutableSequence[MutableProxyThing]:
def __getitem__(self, key: Union[int, slice]) -> Union[MutableProxyThing, MutableSequence[MutableProxyThing]]:
if not isinstance(key, int): raise NotImplemented
x = self._tx.get(self._obj_id, key, self._heads)
if x is None: return None
Expand All @@ -152,7 +152,7 @@ def __getitem__(self, key: int | slice) -> MutableProxyThing | MutableSequence[M
def __setitem__(self, key: int, value: MutableProxyThing) -> None: ...
@overload
def __setitem__(self, key: slice, value: Iterable[MutableProxyThing]) -> None: ...
def __setitem__(self, idx: int | slice, value: MutableProxyThing | Iterable[MutableProxyThing]) -> None:
def __setitem__(self, idx: Union[int, slice], value: Union[MutableProxyThing, Iterable[MutableProxyThing]]) -> None:
if not isinstance(idx, int): raise NotImplemented
if isinstance(value, MutableMapping):
if idx >= self._tx.length(self._obj_id, self._heads):
Expand Down Expand Up @@ -192,11 +192,11 @@ def __setitem__(self, idx: int | slice, value: MutableProxyThing | Iterable[Muta
def __delitem__(self, idx: int) -> None: ...
@overload
def __delitem__(self, idx: slice) -> None: ...
def __delitem__(self, idx: int | slice) -> None:
def __delitem__(self, idx: Union[int, slice]) -> None:
if not isinstance(idx, int): raise NotImplemented
self._tx.delete(self._obj_id, idx)

def insert(self, idx: int, value: core.ScalarValue | MutableMapping[str, MutableProxyThing] | MutableSequence[MutableProxyThing] | None) -> None:
def insert(self, idx: int, value: Union[core.ScalarValue, MutableMapping[str, MutableProxyThing], MutableSequence[MutableProxyThing], None]) -> None:
if not isinstance(idx, int): raise NotImplemented
if isinstance(value, MutableMapping):
obj_id = self._tx.insert_object(self._obj_id, idx, core.ObjType.Map)
Expand Down Expand Up @@ -243,7 +243,7 @@ def _infer_scalar_type(value: core.ScalarValue) -> core.ScalarType:


class Document(MapReadProxy):
def __init__(self, actor_id: ActorId | None = None) -> None:
def __init__(self, actor_id: Optional[ActorId] = None) -> None:
self._doc = core.Document(actor_id.id if actor_id else None)
super().__init__(self._doc, core.ROOT, None)

Expand Down

0 comments on commit 1da5f2f

Please sign in to comment.