Skip to content

Enable Mypy to check that enum match is exhaustive #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions bitcointx/core/psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from typing import (
TypeVar, Tuple, List, Dict, Set, Union, Type, Any, Optional, Generator,
NamedTuple, Callable
NamedTuple, Callable, NoReturn
)

import base64
Expand Down Expand Up @@ -389,6 +389,11 @@ def stream_serialize(self, f: ByteStream_Type, **kwargs: Any) -> None:
T_PSBT_Input = TypeVar('T_PSBT_Input', bound='PSBT_Input')


# See https://github.com/python/mypy/issues/6366#issuecomment-560369716
def _assert_never(x: NoReturn) -> NoReturn:
assert False, "Unhandled type: {}".format(type(x).__name__)


class PSBT_Input(PSBT_CoinClass, next_dispatch_final=True):
index: Optional[int]
_utxo: Optional[Union[CTransaction, CTxOut]]
Expand Down Expand Up @@ -1240,6 +1245,10 @@ def check_witness_and_nonwitness_utxo_in_sync(
for key_type, key_data, value in \
read_psbt_keymap(f, keys_seen, PSBT_InKeyType,
proprietary_fields, unknown_fields):
# This assertion is necessary because Mypy 0.782 can't see that the
# function is dependently typed, that the return type depends
# on the parameter.
assert isinstance(key_type, PSBT_InKeyType)

if key_type is PSBT_InKeyType.NON_WITNESS_UTXO:
ensure_empty_key_data(key_type, key_data, descr(''))
Expand Down Expand Up @@ -1295,10 +1304,13 @@ def check_witness_and_nonwitness_utxo_in_sync(
ensure_empty_key_data(key_type, key_data, descr(''))
proof_of_reserves_commitment = value
else:
raise AssertionError(
# It is useful to put the key_type in a tuple since the type
# error will then note the name of the case that is missing,
# when a case is missing.
_assert_never((key_type,
f'If key type {key_type} is recognized, '
f'it must be handled, and this statement '
f'should not be reached.')
f'should not be reached.'))

# non_witness_utxo is preferred over witness_utxo for `utxo` kwarg
# because non_witness_utxo is a full transaction,
Expand Down