Skip to content

Commit e8774be

Browse files
committed
Add default .create(…) class method to all datastores that just delegates to the synchronous __init__
`FileSystemDatastore` already overwrites this for instance, to provide its async-only construction method instead.
1 parent a78fa77 commit e8774be

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

datastore/core/binarystore.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
class util: # noqa
12+
from .util import decorator
1213
from .util import metadata
1314
from .util import stream
1415

@@ -54,6 +55,14 @@ class Datastore(trio.abc.AsyncResource):
5455
METADATA_T: type
5556
RECEIVE_T: type
5657

58+
_DS = typing.TypeVar("_DS", bound="Datastore")
59+
60+
61+
@classmethod
62+
@util.decorator.awaitable_to_context_manager
63+
async def create(cls: typing.Type[_DS], *args: typing.Any, **kwargs: typing.Any) -> _DS:
64+
return cls(*args, **kwargs) # type: ignore[call-arg]
65+
5766
# Main API. Datastore implementations MUST implement these methods.
5867

5968

datastore/core/objectstore.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
class util: # noqa
12+
from .util import decorator
1213
from .util import metadata
1314
from .util import stream
1415

@@ -56,6 +57,15 @@ class Datastore(typing.Generic[T_co], trio.abc.AsyncResource):
5657
ADAPTER_T: type
5758
METADATA_T: type
5859
RECEIVE_T: type
60+
61+
_DS = typing.TypeVar("_DS", bound="Datastore[T_co]")
62+
63+
64+
@classmethod
65+
@util.decorator.awaitable_to_context_manager
66+
async def create(cls: typing.Type[_DS], *args: typing.Any, **kwargs: typing.Any) -> _DS:
67+
return cls(*args, **kwargs) # type: ignore[call-arg]
68+
5969

6070
# Main API. Datastore implementations MUST implement these methods.
6171

0 commit comments

Comments
 (0)