Skip to content

Commit f58fb36

Browse files
authored
Merge pull request #41 from digital-asset/python-typing-cleanup
python: Clean up some type signatures.
2 parents cb9ade8 + 75a3f05 commit f58fb36

File tree

4 files changed

+47
-50
lines changed

4 files changed

+47
-50
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.3.0
1+
6.3.1

python/dazl/model/types.py

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
DottedNameish = Union[str, Sequence[str]]
4040

41+
T_co = TypeVar('T_co', covariant=True)
42+
4143
if TYPE_CHECKING:
4244
from .types_store import PackageStore
4345
from ..damlast.daml_lf_1 import Expr
@@ -73,22 +75,19 @@ def names(self):
7375
return set(name for name, _ in self)
7476

7577

76-
T = TypeVar('T')
77-
78-
7978
def type_dispatch_table(
80-
on_type_ref: Callable[['TypeReference'], T],
81-
on_type_var: Callable[['TypeVariable'], T],
82-
on_type_app: Callable[['TypeApp'], T],
83-
on_scalar: Callable[['ScalarType'], T],
84-
on_contract_id: Callable[['ContractIdType'], T],
85-
on_optional: Callable[['OptionalType'], T],
86-
on_list: Callable[['ListType'], T],
87-
on_text_map: 'Callable[[TextMapType], T]',
88-
on_record: Callable[['RecordType'], T],
89-
on_variant: Callable[['VariantType'], T],
90-
on_enum: 'Callable[[EnumType], T]',
91-
on_unsupported: Callable[['UnsupportedType'], T]) -> Callable[['Type'], T]:
79+
on_type_ref: Callable[['TypeReference'], T_co],
80+
on_type_var: Callable[['TypeVariable'], T_co],
81+
on_type_app: Callable[['TypeApp'], T_co],
82+
on_scalar: Callable[['ScalarType'], T_co],
83+
on_contract_id: Callable[['ContractIdType'], T_co],
84+
on_optional: Callable[['OptionalType'], T_co],
85+
on_list: Callable[['ListType'], T_co],
86+
on_text_map: 'Callable[[TextMapType], T_co]',
87+
on_record: Callable[['RecordType'], T_co],
88+
on_variant: Callable[['VariantType'], T_co],
89+
on_enum: 'Callable[[EnumType], T_co]',
90+
on_unsupported: Callable[['UnsupportedType'], T_co]) -> Callable[['Type'], T_co]:
9291
def _impl(tt: Type):
9392
if isinstance(tt, TypeReference):
9493
return on_type_ref(tt)
@@ -123,15 +122,15 @@ def _impl(tt: Type):
123122

124123

125124
def scalar_type_dispatch_table(
126-
on_unit: Callable[[], T],
127-
on_bool: Callable[[], T],
128-
on_text: Callable[[], T],
129-
on_int: Callable[[], T],
130-
on_decimal: Callable[[], T],
131-
on_party: Callable[[], T],
132-
on_date: Callable[[], T],
133-
on_datetime: Callable[[], T],
134-
on_timedelta: Callable[[], T]) -> Callable[['ScalarType'], T]:
125+
on_unit: 'Callable[[], T_co]',
126+
on_bool: 'Callable[[], T_co]',
127+
on_text: 'Callable[[], T_co]',
128+
on_int: 'Callable[[], T_co]',
129+
on_decimal: 'Callable[[], T_co]',
130+
on_party: 'Callable[[], T_co]',
131+
on_date: 'Callable[[], T_co]',
132+
on_datetime: 'Callable[[], T_co]',
133+
on_timedelta: 'Callable[[], T_co]') -> 'Callable[[ScalarType], T_co]':
135134
def _impl(tt: ScalarType):
136135
st = safe_cast(ScalarType, tt)
137136
if st == SCALAR_TYPE_UNIT:
@@ -677,8 +676,6 @@ def controllers(self, cdata: ContractData) -> Collection[Party]:
677676
Return every :class:`Party` that can exercise this choice given the specified contract data.
678677
"""
679678

680-
C = TypeVar('C', RecordType, VariantType)
681-
682679

683680
def as_commands(commands_ish, allow_callables=False):
684681
"""
@@ -795,16 +792,16 @@ def with_vars(self, new_vars: Dict[TypeVariable, Type]) -> 'TypeEvaluationContex
795792

796793

797794
def type_evaluate_dispatch(
798-
on_scalar: 'Callable[[TypeEvaluationContext, ScalarType], T]',
799-
on_contract_id: 'Callable[[TypeEvaluationContext, ContractIdType], T]',
800-
on_optional: 'Callable[[TypeEvaluationContext, OptionalType], T]',
801-
on_list: 'Callable[[TypeEvaluationContext, ListType], T]',
802-
on_text_map: 'Callable[[TypeEvaluationContext, TextMapType], T]',
803-
on_record: 'Callable[[TypeEvaluationContext, RecordType], T]',
804-
on_variant: 'Callable[[TypeEvaluationContext, VariantType], T]',
805-
on_enum: 'Callable[[TypeEvaluationContext, EnumType], T]',
806-
on_unsupported: 'Callable[[TypeEvaluationContext, UnsupportedType], T]') \
807-
-> 'Callable[[TypeEvaluationContext, Type], T]':
795+
on_scalar: 'Callable[[TypeEvaluationContext, ScalarType], T_co]',
796+
on_contract_id: 'Callable[[TypeEvaluationContext, ContractIdType], T_co]',
797+
on_optional: 'Callable[[TypeEvaluationContext, OptionalType], T_co]',
798+
on_list: 'Callable[[TypeEvaluationContext, ListType], T_co]',
799+
on_text_map: 'Callable[[TypeEvaluationContext, TextMapType], T_co]',
800+
on_record: 'Callable[[TypeEvaluationContext, RecordType], T_co]',
801+
on_variant: 'Callable[[TypeEvaluationContext, VariantType], T_co]',
802+
on_enum: 'Callable[[TypeEvaluationContext, EnumType], T_co]',
803+
on_unsupported: 'Callable[[TypeEvaluationContext, UnsupportedType], T_co]') \
804+
-> 'Callable[[TypeEvaluationContext, Type], T_co]':
808805
"""
809806
Produce a function that defers handling of core types to the passed in functions.
810807
@@ -821,7 +818,7 @@ def _impl(context, tt):
821818
if resolve_depth > 10:
822819
raise Exception('hit our max resolve depth, which is probably not so great')
823820

824-
def error(_: Any) -> T: raise Exception()
821+
def error(_: Any) -> 'T_co': raise Exception()
825822

826823
context, tt = annotate_context(context, tt)
827824

@@ -844,15 +841,15 @@ def _type_evaluate_dispatch_error(_, __):
844841

845842

846843
def type_evaluate_dispatch_default_error(
847-
on_scalar: Callable[['TypeEvaluationContext', 'ScalarType'], T] = _type_evaluate_dispatch_error,
848-
on_contract_id: Callable[['TypeEvaluationContext', 'ContractIdType'], T] = _type_evaluate_dispatch_error,
849-
on_optional: Callable[['TypeEvaluationContext', 'OptionalType'], T] = _type_evaluate_dispatch_error,
850-
on_list: Callable[['TypeEvaluationContext', 'ListType'], T] = _type_evaluate_dispatch_error,
851-
on_text_map: Callable[['TypeEvaluationContext', 'TextMapType'], T] = _type_evaluate_dispatch_error,
852-
on_record: Callable[['TypeEvaluationContext', 'RecordType'], T] = _type_evaluate_dispatch_error,
853-
on_variant: Callable[['TypeEvaluationContext', 'VariantType'], T] = _type_evaluate_dispatch_error,
854-
on_enum: 'Callable[[TypeEvaluationContext, EnumType], T]' = _type_evaluate_dispatch_error,
855-
on_unsupported: 'Callable[[TypeEvaluationContext, UnsupportedType], T]' = _type_evaluate_dispatch_error):
844+
on_scalar: 'Callable[[TypeEvaluationContext, ScalarType], T_co]' = _type_evaluate_dispatch_error,
845+
on_contract_id: 'Callable[[TypeEvaluationContext, ContractIdType], T_co]' = _type_evaluate_dispatch_error,
846+
on_optional: 'Callable[[TypeEvaluationContext, OptionalType], T_co]' = _type_evaluate_dispatch_error,
847+
on_list: 'Callable[[TypeEvaluationContext, ListType], T_co]' = _type_evaluate_dispatch_error,
848+
on_text_map: 'Callable[[TypeEvaluationContext, TextMapType], T_co]' = _type_evaluate_dispatch_error,
849+
on_record: 'Callable[[TypeEvaluationContext, RecordType], T_co]' = _type_evaluate_dispatch_error,
850+
on_variant: 'Callable[[TypeEvaluationContext, VariantType], T_co]' = _type_evaluate_dispatch_error,
851+
on_enum: 'Callable[[TypeEvaluationContext, EnumType], T_co]' = _type_evaluate_dispatch_error,
852+
on_unsupported: 'Callable[[TypeEvaluationContext, UnsupportedType], T_co]' = _type_evaluate_dispatch_error):
856853
return type_evaluate_dispatch(
857854
on_scalar, on_contract_id, on_optional, on_list, on_text_map, on_record, on_variant,
858855
on_enum, on_unsupported)

python/poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "dazl"
3-
version = "6.3.0"
3+
version = "6.3.1"
44
description = "high-level Ledger API client for DAML ledgers"
55
license = "Apache-2.0"
66
authors = ["Davin K. Tanabe <davin.tanabe@digitalasset.com>"]

0 commit comments

Comments
 (0)