From 12ba0bcc63e082b0e3dd633aa41bd8f7a7843ae4 Mon Sep 17 00:00:00 2001 From: "Davin K. Tanabe" Date: Mon, 14 Mar 2022 10:39:42 -0400 Subject: [PATCH] python: Fix a few broken typing rules. --- VERSION | 2 +- pyproject.toml | 2 +- python/dazl/ledger/__init__.pyi | 7 +++++++ python/dazl/ledger/aio/__init__.pyi | 6 ++++++ python/dazl/ledger/api_types.py | 6 ++++-- python/dazl/ledger/blocking/__init__.pyi | 3 +++ python/dazl/ledger/config/access.py | 2 ++ python/dazl/ledger/grpc/conn_aio.py | 22 ++++++++++++---------- 8 files changed, 36 insertions(+), 14 deletions(-) diff --git a/VERSION b/VERSION index 1985849f..5942a0d3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -7.7.0 +7.7.1 diff --git a/pyproject.toml b/pyproject.toml index 07707832..9b078077 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ [tool.poetry] name = "dazl" -version = "7.7.0" +version = "7.7.1" description = "high-level Ledger API client for Daml ledgers" license = "Apache-2.0" authors = ["Davin K. Tanabe "] diff --git a/python/dazl/ledger/__init__.pyi b/python/dazl/ledger/__init__.pyi index 4f70b5fc..4233faae 100644 --- a/python/dazl/ledger/__init__.pyi +++ b/python/dazl/ledger/__init__.pyi @@ -39,6 +39,7 @@ from .api_types import ( ExerciseResponse, ParticipantMeteringReport, PartyInfo, + Right, SubmitResponse, User, ) @@ -306,7 +307,13 @@ class Connection(PackageService, Protocol): read_as: Union[None, Party, Collection[Party]] = None, ) -> QueryStream: ... def get_user(self, user_id: Optional[str] = None) -> Union[User, Awaitable[User]]: ... + def create_user( + self, user: User, rights: Optional[Sequence[Right]] = ... + ) -> Union[User, Awaitable[User]]: ... def list_users(self) -> Union[Sequence[User], Awaitable[Sequence[User]]]: ... + def list_user_rights( + self, user_id: "Optional[str]" = None + ) -> Union[Sequence[Right], Awaitable[Sequence[Right]]]: ... def allocate_party( self, *, identifier_hint: str = None, display_name: str = None ) -> Union[PartyInfo, Awaitable[PartyInfo]]: ... diff --git a/python/dazl/ledger/aio/__init__.pyi b/python/dazl/ledger/aio/__init__.pyi index abfe78bc..594eca99 100644 --- a/python/dazl/ledger/aio/__init__.pyi +++ b/python/dazl/ledger/aio/__init__.pyi @@ -35,7 +35,9 @@ from ..api_types import ( ExerciseResponse, ParticipantMeteringReport, PartyInfo, + Right, SubmitResponse, + User, ) from .pkgloader import PackageLoader @@ -192,6 +194,10 @@ class Connection(_Connection, PackageService, Protocol): offset: Optional[str] = None, read_as: Union[None, Party, Collection[Party]] = None, ) -> QueryStream: ... + async def get_user(self, user_id: Optional[str] = None) -> User: ... + async def create_user(self, user: User, rights: Optional[Sequence[Right]] = ...) -> User: ... + async def list_users(self) -> Sequence[User]: ... + async def list_user_rights(self, user_id: Optional[str] = None) -> Sequence[Right]: ... async def allocate_party( self, *, identifier_hint: Optional[str] = None, display_name: Optional[str] = None ) -> PartyInfo: ... diff --git a/python/dazl/ledger/api_types.py b/python/dazl/ledger/api_types.py index e0ce814a..53345b81 100644 --- a/python/dazl/ledger/api_types.py +++ b/python/dazl/ledger/api_types.py @@ -602,8 +602,6 @@ def __repr__(self): class User: """ Full information about a ``User``. - - Note: This is part of a Daml 2.x pre-release API and is subject to change. """ __slots__ = ("id", "primary_party") @@ -616,6 +614,10 @@ def __init__(self, id: str, primary_party: Party): class Right(abc.ABC): + """ + Information about an individual right for a :class:`User`. + """ + def __setattr__(self, key, value): """ Overridden to make Right objects read-only. diff --git a/python/dazl/ledger/blocking/__init__.pyi b/python/dazl/ledger/blocking/__init__.pyi index e52be76e..2e9985a0 100644 --- a/python/dazl/ledger/blocking/__init__.pyi +++ b/python/dazl/ledger/blocking/__init__.pyi @@ -20,6 +20,7 @@ from ..api_types import ( ExerciseResponse, ParticipantMeteringReport, PartyInfo, + Right, User, ) from .pkgloader import PackageLoader @@ -142,7 +143,9 @@ class Connection(_Connection, PackageService, Protocol): read_as: Union[None, Party, Collection[Party]] = None, ) -> QueryStream: ... def get_user(self, user_id: Optional[str] = None) -> User: ... + def create_user(self, user: User, rights: Optional[Sequence[Right]] = ...) -> User: ... def list_users(self) -> Sequence[User]: ... + def list_user_rights(self, user_id: Optional[str] = None) -> Sequence[Right]: ... def allocate_party( self, *, identifier_hint: Optional[str] = None, display_name: Optional[str] = None ) -> PartyInfo: ... diff --git a/python/dazl/ledger/config/access.py b/python/dazl/ledger/config/access.py index afd19e59..51460f4b 100644 --- a/python/dazl/ledger/config/access.py +++ b/python/dazl/ledger/config/access.py @@ -245,6 +245,8 @@ def token(self, value: str) -> None: self._application_name = v1_claims.get("applicationId", None) self._token_version = 1 else: + self._ledger_id = None + self._application_name = None self._token_version = 2 def _set(self, *, read_as: Collection[Party], act_as: Collection[Party], admin: bool): diff --git a/python/dazl/ledger/grpc/conn_aio.py b/python/dazl/ledger/grpc/conn_aio.py index 52266a73..fe9d9cc7 100644 --- a/python/dazl/ledger/grpc/conn_aio.py +++ b/python/dazl/ledger/grpc/conn_aio.py @@ -99,16 +99,6 @@ async def open(self) -> None: Does final validation of the token, including possibly fetching the ledger ID if it is not yet known. """ - if not self._config.access.ledger_id: - # most calls require a ledger ID; if it wasn't supplied as part of our token or we were - # never given a token in the first place, fetch the ledger ID from the destination - stub = lapipb.LedgerIdentityServiceStub(self._channel) - response = await stub.GetLedgerIdentity(lapipb.GetLedgerIdentityRequest()) - if isinstance(self._config.access, PropertyBasedAccessConfig): - self._logger.info("Connected to gRPC Ledger API, ledger ID: %s", response.ledger_id) - self._config.access.ledger_id = response.ledger_id - else: - raise ValueError("when using token-based access, the token must contain ledger ID") if self._config.access.token_version == 2: # Daml 2.0 tokens do not contain party information, so an extra call to the server is # required in order to resolve our current set of rights @@ -127,6 +117,18 @@ async def open(self) -> None: read_as=read_as, act_as=act_as, admin=admin ) + elif not self._config.access.ledger_id: + # most calls to Daml 1.x ledgers require a ledger ID; if it wasn't supplied as part of + # our token or we were never given a token in the first place, fetch the ledger ID from + # the destination + stub = lapipb.LedgerIdentityServiceStub(self._channel) + response = await stub.GetLedgerIdentity(lapipb.GetLedgerIdentityRequest()) + if isinstance(self._config.access, PropertyBasedAccessConfig): + self._logger.info("Connected to gRPC Ledger API, ledger ID: %s", response.ledger_id) + self._config.access.ledger_id = response.ledger_id + else: + raise ValueError("when using token-based access, the token must contain ledger ID") + async def close(self) -> None: """ Close the underlying channel. Once the channel is closed, future command submissions,