Skip to content

Commit 12eb7a1

Browse files
committed
Add type hints
1 parent 5f77eea commit 12eb7a1

File tree

8 files changed

+47
-40
lines changed

8 files changed

+47
-40
lines changed

tronapi/base/datatypes.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,18 @@
1111
)
1212

1313

14+
@curry
15+
def verify_attr(class_name, key, namespace):
16+
if key not in namespace:
17+
raise AttributeError(
18+
"Property {0} not found on {1} class. "
19+
"`{1}.factory` only accepts keyword arguments which are "
20+
"present on the {1} class".format(key, class_name)
21+
)
22+
23+
1424
class PropertyCheckingFactory(type):
15-
def __init__(cls, name, bases, namespace):
25+
def __init__(cls, name, bases, namespace, **kargs):
1626
# see PEP487. To accept kwargs in __new__, they need to be
1727
# filtered out here.
1828
super().__init__(name, bases, namespace)
@@ -31,13 +41,3 @@ def __new__(mcs, name, bases, namespace, normalizers=None):
3141
processed_namespace = namespace
3242

3343
return super().__new__(mcs, name, bases, processed_namespace)
34-
35-
36-
@curry
37-
def verify_attr(class_name, key, namespace):
38-
if key not in namespace:
39-
raise AttributeError(
40-
"Property {0} not found on {1} class. "
41-
"`{1}.factory` only accepts keyword arguments which are "
42-
"present on the {1} class".format(key, class_name)
43-
)

tronapi/base/decorators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import functools
22
import threading
33
import warnings
4+
from typing import Callable
45

56

67
class combomethod:
78
def __init__(self, method):
89
self.method = method
910

10-
def __get__(self, obj=None, objtype=None):
11+
def __get__(self, obj=None, obj_type=None) -> Callable:
1112
@functools.wraps(self.method)
1213
def _wrapper(*args, **kwargs):
1314
if obj is not None:
1415
return self.method(obj, *args, **kwargs)
1516
else:
16-
return self.method(objtype, *args, **kwargs)
17+
return self.method(obj_type, *args, **kwargs)
1718

1819
return _wrapper
1920

tronapi/base/encoding.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
from typing import Optional, Union
2+
13
from eth_utils import hexstr_if_str, to_hex
24

35
from tronapi.utils.hexadecimal import remove_0x_prefix, decode_hex, encode_hex, add_0x_prefix
46
from tronapi.utils.types import is_boolean, is_integer
57
from tronapi.utils.validation import assert_one_val
68

79

8-
def to_bytes(primitive=None, hexstr=None, text=None):
10+
def to_bytes(primitive: Optional[Union[bytes, int]] = None,
11+
hexstr: Optional[str] = None,
12+
text: Optional[str] = None) -> bytes:
13+
914
assert_one_val(primitive, hexstr=hexstr, text=text)
1015

1116
if is_boolean(primitive):
@@ -23,15 +28,15 @@ def to_bytes(primitive=None, hexstr=None, text=None):
2328
raise TypeError("expected an int in first arg, or keyword of hexstr or text")
2429

2530

26-
def pad_hex(value, bit_size):
31+
def pad_hex(value: str, bit_size: int) -> str:
2732
"""
2833
Pads a hex string up to the given bit_size
2934
"""
3035
value = remove_0x_prefix(value)
3136
return add_0x_prefix(value.zfill(int(bit_size / 4)))
3237

3338

34-
def to_4byte_hex(hex_or_str_or_bytes):
39+
def to_4byte_hex(hex_or_str_or_bytes: Union[int, str, bytes]) -> str:
3540
size_of_4bytes = 4 * 8
3641
byte_str = hexstr_if_str(to_bytes, hex_or_str_or_bytes)
3742
if len(byte_str) > 4:

tronapi/constants.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# --------------------------------------------------------------------------------------------
1+
# --------------------------------------------------------------------
22
# Copyright (c) iEXBase. All rights reserved.
3-
# Licensed under the MIT License. See License.txt in the project root for license information.
4-
# --------------------------------------------------------------------------------------------
3+
# Licensed under the MIT License.
4+
# See License.txt in the project root for license information.
5+
# --------------------------------------------------------------------
56

67
# Tron nodes
78
DEFAULT_FULL_NODE = "https://api.trongrid.io"

tronapi/exceptions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# --------------------------------------------------------------------------------------------
1+
# --------------------------------------------------------------------
22
# Copyright (c) iEXBase. All rights reserved.
3-
# Licensed under the MIT License. See License.txt in the project root for license information.
4-
# --------------------------------------------------------------------------------------------
3+
# Licensed under the MIT License.
4+
# See License.txt in the project root for license information.
5+
# --------------------------------------------------------------------
56

67

78
class TronError(Exception):

tronapi/module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
class Module:
99
"""Module Class"""
1010

11-
def __init__(self, tron):
11+
def __init__(self, tron) -> None:
1212
self.tron = tron
1313

1414
@classmethod
15-
def attach(cls, target, module_name=None):
15+
def attach(cls, target, module_name: str = None) -> None:
1616
if not module_name:
1717
module_name = cls.__name__.lower()
1818

tronapi/transactionbuilder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# --------------------------------------------------------------------------------------------
1+
# --------------------------------------------------------------------
22
# Copyright (c) iEXBase. All rights reserved.
3-
# Licensed under the MIT License. See License.txt in the project root for license information.
4-
# --------------------------------------------------------------------------------------------
3+
# Licensed under the MIT License.
4+
# See License.txt in the project root for license information.
5+
# --------------------------------------------------------------------
56

67
from datetime import datetime
78
from typing import Any, Tuple, List

tronapi/trx.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@ class Trx(Module):
2727
defaultContractFactory = Contract
2828

2929
def get_current_block(self):
30-
"""Query the latest block
31-
32-
Returns:
33-
Latest block on full node
34-
"""
30+
"""Query the latest block"""
3531
return self.tron.manager.request(url='/wallet/getnowblock')
3632

3733
def get_block(self, block: Any = None):
3834
"""Get block details using HashString or blockNumber
3935
4036
Args:
41-
block (Any): Number or Hash Block
37+
block (Any): ID or height for the block
38+
4239
"""
4340

4441
# If the block identifier is not specified,
@@ -307,10 +304,11 @@ def send_trx(self, to, amount, options=None):
307304

308305
def send_transaction(self, to, amount, options=None):
309306
"""Send an asset to another account.
307+
Will create and broadcast the transaction if a private key is provided.
310308
311-
Parameters:
312-
to (str): Recipient
313-
amount (float): Amount to transfer
309+
Args:
310+
to (str): Address to send TRX to.
311+
amount (float): Amount of TRX to send.
314312
options (Any, optional): Options
315313
316314
"""
@@ -322,7 +320,6 @@ def send_transaction(self, to, amount, options=None):
322320
options = assoc(options, 'from', self.tron.default_address.hex)
323321

324322
tx = self.tron.transaction.send_transaction(to, amount, options['from'])
325-
326323
# If a comment is attached to the transaction,
327324
# in this case adding to the object
328325
if 'message' in options:
@@ -402,6 +399,10 @@ def sign(self, transaction: Any, use_tron: bool = True):
402399
"""Sign the transaction, the api has the risk of leaking the private key,
403400
please make sure to call the api in a secure environment
404401
402+
Warnings:
403+
Do not use this in any web / user-facing applications.
404+
This will expose the private key.
405+
405406
Args:
406407
transaction (Any): transaction details
407408
use_tron (bool): is Tron header
@@ -440,9 +441,6 @@ def broadcast(self, signed_transaction):
440441
Args:
441442
signed_transaction (object): signed transaction contract data
442443
443-
Returns:
444-
broadcast success or failure
445-
446444
"""
447445
if not is_object(signed_transaction):
448446
raise InvalidTronError('Invalid transaction provided')

0 commit comments

Comments
 (0)