Skip to content

Commit

Permalink
Merge pull request #389 from digital-asset/python-mypy-violation-fixes
Browse files Browse the repository at this point in the history
python: Fix various mypy violations.
  • Loading branch information
da-tanabe authored Dec 23, 2022
2 parents 4a95761 + 9b3cb0a commit afe5d9a
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion python/_dazl_pb/syntax/python/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
8 changes: 7 additions & 1 deletion python/dazl/client/_party_client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/dazl/client/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions python/dazl/damlast/_builtins_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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}")

Expand All @@ -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}")

Expand Down
5 changes: 5 additions & 0 deletions python/dazl/damlast/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
2 changes: 1 addition & 1 deletion python/dazl/damlast/daml_lf_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion python/dazl/damlast/eval_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions python/dazl/damlast/pkgfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,6 +19,8 @@

__all__ = ["Dar", "DarFile", "CachedDarFile", "get_dar_package_ids"]

Self = TypeVar("Self")


class DarFile:
"""
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions python/dazl/ledger/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions python/dazl/ledger/grpc/conn_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
7 changes: 1 addition & 6 deletions python/dazl/model/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion python/dazl/model/types_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion python/dazl/pretty/table/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions python/dazl/protocols/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions python/dazl/testing/connect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand All @@ -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]]: ...
Expand All @@ -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]]: ...
Expand All @@ -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[
Expand All @@ -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]]: ...
Expand Down
10 changes: 2 additions & 8 deletions python/dazl/util/asyncio_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions python/dazl/util/dar_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
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,
stacklevel=2,
)
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)

Expand Down

0 comments on commit afe5d9a

Please sign in to comment.