From 71e19d967d2271502c2c721bb8868bd13b555738 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Sun, 5 Jun 2022 16:42:56 +0200
Subject: [PATCH] [mypy] #163: Add type annotation on tx_history
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
L242: error: "List[Any]" has no attribute "outputs"
Ignore this since I don’t know what’s wrong
---
silkaj/tx_history.py | 71 ++++++++++++++++++++++++++++++++------------
1 file changed, 52 insertions(+), 19 deletions(-)
diff --git a/silkaj/tx_history.py b/silkaj/tx_history.py
index 1b69842e..f7e63023 100644
--- a/silkaj/tx_history.py
+++ b/silkaj/tx_history.py
@@ -15,10 +15,13 @@
import shutil
from operator import eq, itemgetter, ne, neg
+from typing import Any, List, Optional, Tuple
from click import argument, command, echo_via_pager, option
from duniterpy.api.bma.tx import history
-from duniterpy.documents.transaction import Transaction
+from duniterpy.api.client import Client
+from duniterpy.documents.transaction import OutputSource, Transaction
+from duniterpy.grammars.output import Condition
from pendulum import from_timestamp, now
from texttable import Texttable
@@ -35,7 +38,7 @@ from silkaj.tui import gen_pubkey_checksum
@argument("pubkey")
@option("--uids", "-u", is_flag=True, help="Display uids")
@option("--full-pubkey", "-f", is_flag=True, help="Display full-length pubkeys")
-def transaction_history(pubkey, uids, full_pubkey):
+def transaction_history(pubkey: str, uids: bool, full_pubkey: bool) -> None:
if check_pubkey_format(pubkey):
pubkey = validate_checksum(pubkey)
@@ -44,7 +47,10 @@ def transaction_history(pubkey, uids, full_pubkey):
currency_symbol = get_currency_symbol()
header = generate_header(pubkey, currency_symbol, ud_value)
- received_txs, sent_txs = list(), list()
+ received_txs, sent_txs = (
+ list(),
+ list(),
+ ) # type: List[Transaction], List[Transaction]
get_transactions_history(client, pubkey, received_txs, sent_txs)
remove_duplicate_txs(received_txs, sent_txs)
txs_list = generate_table(
@@ -55,7 +61,7 @@ def transaction_history(pubkey, uids, full_pubkey):
echo_via_pager(header + table.draw())
-def generate_header(pubkey, currency_symbol, ud_value):
+def generate_header(pubkey: str, currency_symbol: str, ud_value: int) -> str:
try:
idty = wt.identity_of(pubkey)
except Exception:
@@ -67,7 +73,9 @@ def generate_header(pubkey, currency_symbol, ud_value):
Current balance: {balance[1] / 100} {currency_symbol}, {balance_ud} UD {currency_symbol} on {date}\n'
-def get_transactions_history(client, pubkey, received_txs, sent_txs):
+def get_transactions_history(
+ client: Client, pubkey: str, received_txs: List, sent_txs: List
+) -> None:
"""
Get transaction history
Store txs in Transaction object
@@ -81,7 +89,7 @@ def get_transactions_history(client, pubkey, received_txs, sent_txs):
sent_txs.append(Transaction.from_bma_history(sent, currency))
-def remove_duplicate_txs(received_txs, sent_txs):
+def remove_duplicate_txs(received_txs: List, sent_txs: List) -> None:
"""
Remove duplicate transactions from history
Remove received tx which contains output back return
@@ -94,8 +102,14 @@ def remove_duplicate_txs(received_txs, sent_txs):
def generate_table(
- received_txs, sent_txs, pubkey, ud_value, currency_symbol, uids, full_pubkey
-):
+ received_txs: List[Transaction],
+ sent_txs: List[Transaction],
+ pubkey: str,
+ ud_value: int,
+ currency_symbol: str,
+ uids: bool,
+ full_pubkey: bool,
+) -> List:
"""
Generate information in a list of lists for texttabe
Merge received and sent txs
@@ -103,7 +117,10 @@ def generate_table(
Sort txs temporarily
"""
- received_txs_table, sent_txs_table = list(), list()
+ received_txs_table, sent_txs_table = (
+ list(),
+ list(),
+ ) # type: List[Transaction], List[Transaction]
parse_received_tx(
received_txs_table, received_txs, pubkey, ud_value, uids, full_pubkey
)
@@ -124,8 +141,13 @@ def generate_table(
def parse_received_tx(
- received_txs_table, received_txs, pubkey, ud_value, uids, full_pubkey
-):
+ received_txs_table: List[Transaction],
+ received_txs: List[Transaction],
+ pubkey: str,
+ ud_value: int,
+ uids: bool,
+ full_pubkey: bool,
+) -> None:
"""
Extract issuers’ pubkeys
Get identities from pubkeys
@@ -154,7 +176,14 @@ def parse_received_tx(
received_txs_table.append(tx_list)
-def parse_sent_tx(sent_txs_table, sent_txs, pubkey, ud_value, uids, full_pubkey):
+def parse_sent_tx(
+ sent_txs_table: List[Transaction],
+ sent_txs: List[Transaction],
+ pubkey: str,
+ ud_value: int,
+ uids: bool,
+ full_pubkey: bool,
+) -> None:
"""
Extract recipients’ pubkeys from outputs
Get identities from pubkeys
@@ -202,32 +231,34 @@ def parse_sent_tx(sent_txs_table, sent_txs, pubkey, ud_value, uids, full_pubkey)
sent_txs_table.append(tx_list)
-def tx_amount(tx, pubkey, function):
+def tx_amount(
+ tx: List[Transaction], pubkey: str, function: Any
+) -> Tuple[int, List[OutputSource]]:
"""
Determine transaction amount from output sources
"""
amount = 0
outputs = list()
- for output in tx.outputs:
+ for output in tx.outputs: # type: ignore
if output_available(output.condition, ne, pubkey):
outputs.append(output)
amount += function(output, pubkey)
return amount, outputs
-def received_func(output, pubkey):
+def received_func(output: OutputSource, pubkey: str) -> int:
if output_available(output.condition, eq, pubkey):
return amount_in_current_base(output)
return 0
-def sent_func(output, pubkey):
+def sent_func(output: OutputSource, pubkey: str) -> int:
if output_available(output.condition, ne, pubkey):
return neg(amount_in_current_base(output))
return 0
-def output_available(condition, comparison, value):
+def output_available(condition: Condition, comparison: Any, value: str) -> bool:
"""
Check if output source is available
Currently only handle simple SIG condition
@@ -239,7 +270,7 @@ def output_available(condition, comparison, value):
return False
-def assign_idty_from_pubkey(pubkey, identities, full_pubkey):
+def assign_idty_from_pubkey(pubkey: str, identities: List, full_pubkey: bool) -> str:
idty = gen_pubkey_checksum(pubkey, short=not full_pubkey)
for identity in identities:
if pubkey == identity["pubkey"]:
@@ -248,7 +279,9 @@ def assign_idty_from_pubkey(pubkey, identities, full_pubkey):
return idty
-def prefix(tx_addresses, outputs, occurence):
+def prefix(
+ tx_addresses: Optional[str], outputs: Optional[List[OutputSource]], occurence: int
+) -> str:
"""
Pretty print with texttable
Break line when several values in a cell
--
GitLab