From 9b3cb0a0c6e9c03c0b7845a0cebe33bf53d75cde Mon Sep 17 00:00:00 2001 From: "Davin K. Tanabe" Date: Fri, 23 Dec 2022 12:08:16 -0500 Subject: [PATCH] python: Fix various mypy violations. --- python/_dazl_pb/syntax/python/symbols.py | 2 +- python/dazl/client/_party_client_impl.py | 8 +++++++- python/dazl/client/state.py | 2 +- python/dazl/damlast/_builtins_meta.py | 22 +++++++++++++--------- python/dazl/damlast/builtins.py | 5 +++++ python/dazl/damlast/daml_lf_1.py | 2 +- python/dazl/damlast/eval_scope.py | 2 +- python/dazl/damlast/pkgfile.py | 6 ++++-- python/dazl/ledger/__init__.pyi | 4 ++-- python/dazl/ledger/grpc/conn_aio.py | 4 ++-- python/dazl/model/types.py | 7 +------ python/dazl/model/types_store.py | 2 +- python/dazl/pretty/table/model.py | 2 +- python/dazl/protocols/_base.py | 1 + python/dazl/testing/connect.pyi | 10 +++++----- python/dazl/util/asyncio_util.py | 10 ++-------- python/dazl/util/dar_repo.py | 6 +++--- 17 files changed, 51 insertions(+), 44 deletions(-) diff --git a/python/_dazl_pb/syntax/python/symbols.py b/python/_dazl_pb/syntax/python/symbols.py index ee6bca82..e0c44f78 100644 --- a/python/_dazl_pb/syntax/python/symbols.py +++ b/python/_dazl_pb/syntax/python/symbols.py @@ -44,7 +44,7 @@ class SymbolTable: messages within the same file can be referenced without being fully-qualified. """ - def __init__(self): + def __init__(self) -> None: self._packages = {} # type: Dict[str, str] self._imports = {} # type: Dict[str, str] self._map_types = {} # type: Dict[str, Tuple[FieldDescriptorProto, FieldDescriptorProto]] diff --git a/python/dazl/client/_party_client_impl.py b/python/dazl/client/_party_client_impl.py index 6fce50b9..dc720bbe 100644 --- a/python/dazl/client/_party_client_impl.py +++ b/python/dazl/client/_party_client_impl.py @@ -663,11 +663,17 @@ def _process_command_finished(self, pending_command, ignore_errors): else: LOG.debug("Command finished: %s", pending_command) - async def main_writer(self): + async def main_writer(self) -> None: """ Main coroutine for submitting commands. """ LOG.info("Writer loop for party %s is starting...", self.party) + if self._pool is None: + raise RuntimeError("unexpected start to main_writer with undefined pool") + if self._client_fut is None: + raise RuntimeError("unexpected start to main_writer with undefined client") + if self._config is None: + raise RuntimeError("unexpected start to main_writer with undefined config") ledger_fut = ensure_future(self._pool.ledger()) client = await self._client_fut # type: LedgerClient diff --git a/python/dazl/client/state.py b/python/dazl/client/state.py index 2b17d182..d3ad88f6 100644 --- a/python/dazl/client/state.py +++ b/python/dazl/client/state.py @@ -174,7 +174,7 @@ class TemplateContractData: Storage for state related to a specific :class:`Template`. """ - def __init__(self): + def __init__(self) -> None: # raw ContractId to ContractContextualData self._data = dict() # type: Dict[str, ContractContextualData] # outstanding ACS queries diff --git a/python/dazl/damlast/_builtins_meta.py b/python/dazl/damlast/_builtins_meta.py index e39e460f..5b2c950d 100644 --- a/python/dazl/damlast/_builtins_meta.py +++ b/python/dazl/damlast/_builtins_meta.py @@ -6,7 +6,7 @@ Daml-LF files. """ -from typing import Any, Optional, Sequence, Type as PyType, Union +from typing import Any, Dict, Optional, Sequence, Type as PyType, Union import warnings from .daml_lf_1 import BuiltinFunction, Expr, Type, ValName @@ -16,6 +16,10 @@ warnings.simplefilter("ignore", DeprecationWarning) from ..model.types import TypeReference +warnings.warn( + "The symbols in dazl.damlast.builtin are deprecated", DeprecationWarning, stacklevel=2 +) + class _BuiltinMeta(type): def __new__(mcs, name, bases, dct): @@ -52,15 +56,15 @@ def simplify(self, type_args: "Sequence[Type]", args: "Sequence[Expr]") -> "Opti class BuiltinTable: - def __init__(self): - self.by_name = dict() # type: [BuiltinFunction, PyType[Builtin]] - self.by_builtin = dict() # type: [str, PyType[Builtin]] + def __init__(self) -> None: + self.by_name = dict() # type: Dict[BuiltinFunction, PyType[Builtin]] + self.by_builtin = dict() # type: Dict[str, PyType[Builtin]] def add(self, builtin: "PyType[Builtin]"): if builtin.builtin is not None: - self.by_builtin[builtin.builtin] = builtin + self.by_builtin[builtin.builtin] = builtin # type: ignore elif builtin.name is not None: - self.by_name[builtin.name] = builtin + self.by_name[builtin.name] = builtin # type: ignore else: raise ValueError(f"A builtin could not be registered! {builtin!r}") @@ -72,11 +76,11 @@ def resolve( """ if isinstance(ref, BuiltinFunction): # All BuiltinFunctions MUST be defined - return self.by_builtin[ref] + return self.by_builtin[ref] # type: ignore elif isinstance(ref, (ValName, TypeReference)): - return self.by_name.get(package_local_name(ref)) + return self.by_name.get(package_local_name(ref)) # type: ignore elif isinstance(ref, str): - return self.by_name.get(ref) + return self.by_name.get(ref) # type: ignore else: raise TypeError(f"unexpected key type: {ref!r}") diff --git a/python/dazl/damlast/builtins.py b/python/dazl/damlast/builtins.py index 22546e1f..87e7286e 100644 --- a/python/dazl/damlast/builtins.py +++ b/python/dazl/damlast/builtins.py @@ -7,11 +7,16 @@ """ from typing import Any, Optional, Sequence +import warnings from . import daml_types as daml from ._builtins_meta import Builtin, BuiltinTable from .daml_lf_1 import BuiltinFunction, Expr, PrimLit, Type +warnings.warn( + "The symbols in dazl.damlast.builtin are deprecated", DeprecationWarning, stacklevel=2 +) + builtins = BuiltinTable() diff --git a/python/dazl/damlast/daml_lf_1.py b/python/dazl/damlast/daml_lf_1.py index b3a30af8..c1c96b5d 100644 --- a/python/dazl/damlast/daml_lf_1.py +++ b/python/dazl/damlast/daml_lf_1.py @@ -1477,7 +1477,7 @@ def Sum_match( from_required_interface: "_typing.Callable[[FromRequiredInterface], _T]", unsafe_from_required_interface: "_typing.Callable[[UnsafeFromRequiredInterface], _T]", experimental: "_typing.Callable[[Expr.Experimental], _T]", - ) -> "T": + ) -> "_T": if self._Sum_name == "var": return var(self.var) # type: ignore elif self._Sum_name == "val": diff --git a/python/dazl/damlast/eval_scope.py b/python/dazl/damlast/eval_scope.py index 74f06f3b..26d183cc 100644 --- a/python/dazl/damlast/eval_scope.py +++ b/python/dazl/damlast/eval_scope.py @@ -68,7 +68,7 @@ def get_bound(self, var_name) -> "Expr": return self._vars[var_name] def resolve_type(self, type_ref) -> "Type": - pass + raise RuntimeError("this function is not implemented") def resolve_value(self, value_ref) -> "Optional[Expr]": if value_ref in self._blocked_value_refs: diff --git a/python/dazl/damlast/pkgfile.py b/python/dazl/damlast/pkgfile.py index 3f8494f6..3c29bf91 100644 --- a/python/dazl/damlast/pkgfile.py +++ b/python/dazl/damlast/pkgfile.py @@ -5,7 +5,7 @@ from os import PathLike from pathlib import Path import threading -from typing import AbstractSet, BinaryIO, Collection, Generator, Mapping, Optional, Union +from typing import AbstractSet, BinaryIO, Collection, Generator, Mapping, Optional, TypeVar, Union import warnings from zipfile import ZipFile @@ -19,6 +19,8 @@ __all__ = ["Dar", "DarFile", "CachedDarFile", "get_dar_package_ids"] +Self = TypeVar("Self") + class DarFile: """ @@ -62,7 +64,7 @@ def __init__(self, dar: "Dar"): else: raise TypeError("DarFile only understands file paths or binary blobs") - def __enter__(self) -> "DarFile": + def __enter__(self: Self) -> "Self": return self def __exit__(self, exc_type, exc_val, exc_tb): diff --git a/python/dazl/ledger/__init__.pyi b/python/dazl/ledger/__init__.pyi index 46bd8106..061e23fd 100644 --- a/python/dazl/ledger/__init__.pyi +++ b/python/dazl/ledger/__init__.pyi @@ -384,8 +384,8 @@ class Connection(PackageService, Protocol): def allocate_party( self, *, - identifier_hint: str = None, - display_name: str = None, + identifier_hint: Optional[str] = None, + display_name: Optional[str] = None, timeout: Optional[TimeDeltaLike] = ..., ) -> Union[PartyInfo, Awaitable[PartyInfo]]: ... def list_known_parties( diff --git a/python/dazl/ledger/grpc/conn_aio.py b/python/dazl/ledger/grpc/conn_aio.py index 842aa8fd..9745a619 100644 --- a/python/dazl/ledger/grpc/conn_aio.py +++ b/python/dazl/ledger/grpc/conn_aio.py @@ -852,8 +852,8 @@ async def list_user_rights( async def allocate_party( self, *, - identifier_hint: str = None, - display_name: str = None, + identifier_hint: Optional[str] = None, + display_name: Optional[str] = None, timeout: Optional[TimeDeltaLike] = None, ) -> PartyInfo: """ diff --git a/python/dazl/model/types.py b/python/dazl/model/types.py index 142f7e24..4cc34fb0 100644 --- a/python/dazl/model/types.py +++ b/python/dazl/model/types.py @@ -722,14 +722,9 @@ def __init__( self._controllers = controllers @property - def type(self): + def type(self) -> Type: return self.data_type - def controllers(self, cdata: ContractData) -> Collection[Party]: - """ - Return every :class:`Party` that can exercise this choice given the specified contract data. - """ - def as_commands(commands_ish, allow_callables=False): """ diff --git a/python/dazl/model/types_store.py b/python/dazl/model/types_store.py index 984f1dd9..08ad2d46 100644 --- a/python/dazl/model/types_store.py +++ b/python/dazl/model/types_store.py @@ -61,7 +61,7 @@ class PackageStoreBuilder: Convenience class for building up a :class:`PackageStore`. """ - def __init__(self): + def __init__(self) -> None: warnings.warn( "PackageStoreBuilder is deprecated; there is no direct replacement.", DeprecationWarning, diff --git a/python/dazl/pretty/table/model.py b/python/dazl/pretty/table/model.py index ea0d9d52..04d6b24c 100644 --- a/python/dazl/pretty/table/model.py +++ b/python/dazl/pretty/table/model.py @@ -35,7 +35,7 @@ class TableBuilder: State built up during a ledger run. """ - def __init__(self): + def __init__(self) -> None: self.entries = dict() # type: Dict[ContractId, RowBuilder] def add( diff --git a/python/dazl/protocols/_base.py b/python/dazl/protocols/_base.py index bafd5937..2df7c322 100644 --- a/python/dazl/protocols/_base.py +++ b/python/dazl/protocols/_base.py @@ -150,6 +150,7 @@ async def events_end(self) -> str: """ Return the (current) last offset of the ledger. """ + raise NotImplementedError("events_end must be implemented") class _LedgerConnection: diff --git a/python/dazl/testing/connect.pyi b/python/dazl/testing/connect.pyi index 40338f19..eb4099d5 100644 --- a/python/dazl/testing/connect.pyi +++ b/python/dazl/testing/connect.pyi @@ -35,7 +35,7 @@ def connect_with_new_party( act_as: Union[None, Party, Collection[Party]] = None, admin: Optional[bool] = False, ledger_id: Optional[str] = None, - dar: Union[str, bytes, PathLike, BinaryIO] = None, + dar: Union[None, str, bytes, PathLike, BinaryIO] = None, identifier_hint: Union[None, str, NameGenFn] = None, display_name: Union[None, str, NameGenFn] = None ) -> AsyncContextManager[ConnectionWithParty]: ... @@ -48,7 +48,7 @@ def connect_with_new_party( act_as: Union[None, Party, Collection[Party]] = None, admin: Optional[bool] = False, ledger_id: Optional[str] = None, - dar: Union[str, bytes, PathLike, BinaryIO] = None, + dar: Union[None, str, bytes, PathLike, BinaryIO] = None, identifier_hint: Union[None, str, NameGenFn] = None, display_name: Union[None, str, NameGenFn] = None ) -> AsyncContextManager[Tuple[ConnectionWithParty, ConnectionWithParty]]: ... @@ -61,7 +61,7 @@ def connect_with_new_party( act_as: Union[None, Party, Collection[Party]] = None, admin: Optional[bool] = False, ledger_id: Optional[str] = None, - dar: Union[str, bytes, PathLike, BinaryIO] = None, + dar: Union[None, str, bytes, PathLike, BinaryIO] = None, identifier_hint: Union[None, str, NameGenFn] = None, display_name: Union[None, str, NameGenFn] = None ) -> AsyncContextManager[Tuple[ConnectionWithParty, ConnectionWithParty, ConnectionWithParty]]: ... @@ -74,7 +74,7 @@ def connect_with_new_party( act_as: Union[None, Party, Collection[Party]] = None, admin: Optional[bool] = False, ledger_id: Optional[str] = None, - dar: Union[str, bytes, PathLike, BinaryIO] = None, + dar: Union[None, str, bytes, PathLike, BinaryIO] = None, identifier_hint: Union[None, str, NameGenFn] = None, display_name: Union[None, str, NameGenFn] = None ) -> AsyncContextManager[ @@ -89,7 +89,7 @@ def connect_with_new_party( act_as: Union[None, Party, Collection[Party]] = None, admin: Optional[bool] = False, ledger_id: Optional[str] = None, - dar: Union[str, bytes, PathLike, BinaryIO] = None, + dar: Union[None, str, bytes, PathLike, BinaryIO] = None, identifier_hint: Union[None, str, NameGenFn] = None, display_name: Union[None, str, NameGenFn] = None ) -> AsyncContextManager[Sequence[ConnectionWithParty]]: ... diff --git a/python/dazl/util/asyncio_util.py b/python/dazl/util/asyncio_util.py index dcc704c5..c041b87e 100644 --- a/python/dazl/util/asyncio_util.py +++ b/python/dazl/util/asyncio_util.py @@ -371,7 +371,7 @@ def __await__(self): class ServiceQueue(Generic[T]): # noinspection PyDeprecation - def __init__(self): + def __init__(self) -> None: warnings.warn( "ServiceQueue is deprecated; there is no planned replacement", DeprecationWarning, @@ -415,12 +415,6 @@ def stop(self): self._q.put_nowait((None, fut)) return fut - def abort(self) -> Sequence[T_co]: - """ - Gracefully terminate the open iterator as quickly as possible and return all remaining - elements in the queue. - """ - async def next(self) -> Optional[T_co]: if not self._service_fut.done(): await self._service_fut @@ -435,7 +429,7 @@ async def next(self) -> Optional[T_co]: fut.set_result(None) return None else: - self._prev_fut = fut + self._prev_fut = fut # type: ignore return value def __aiter__(self): diff --git a/python/dazl/util/dar_repo.py b/python/dazl/util/dar_repo.py index be7146be..b50d46ad 100644 --- a/python/dazl/util/dar_repo.py +++ b/python/dazl/util/dar_repo.py @@ -4,14 +4,14 @@ from os import PathLike, fspath from pathlib import Path import time -from typing import List, Sequence, Union +from typing import List, Sequence, Set, Union import warnings from .. import LOG class LocalDarRepository: - def __init__(self): + def __init__(self) -> None: warnings.warn( "LocalDarRepository is deprecated; use PackageLookup instead", DeprecationWarning, @@ -19,7 +19,7 @@ def __init__(self): ) self._context = ExitStack() self._dar_paths = [] # type: List[Path] - self._files = set() + self._files = set() # type: Set[Path] with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning)