Skip to content

Commit 29085d0

Browse files
authored
Merge pull request ethereum#471 from jasonrhaas/python-moderization-2
Python modernization 2
2 parents 7b14f83 + 4c52f79 commit 29085d0

File tree

11 files changed

+29
-50
lines changed

11 files changed

+29
-50
lines changed

tests/core/contracts/test_concise_contract.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from unittest.mock import Mock
23

34
from eth_utils import (
45
decode_hex,
@@ -10,8 +11,6 @@
1011
ConciseMethod,
1112
)
1213

13-
from unittest.mock import Mock
14-
1514

1615
@pytest.fixture()
1716
def EMPTY_ADDR():

tests/core/eth-module/test_accounts.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ def test_eth_account_recover_message(acct):
152152
b'\x0cu0\x84\xe5\xa8)\x02\x192L\x1a:\x86\xd4\x06M\xed-\x15\x97\x9b\x1e\xa7\x90sJ\xaa,\xea\xaf\xc1"\x9c\xa4S\x81\x06\x81\x9f\xd3\xa5P\x9d\xd3\x83\xe8\xfeKs\x1chp3\x95V\xa5\xc0o\xeb\x9c\xf30\xbb\x00', # noqa: E501
153153
# test signature bytes with chain-naive v (27 in this case)
154154
b'\x0cu0\x84\xe5\xa8)\x02\x192L\x1a:\x86\xd4\x06M\xed-\x15\x97\x9b\x1e\xa7\x90sJ\xaa,\xea\xaf\xc1"\x9c\xa4S\x81\x06\x81\x9f\xd3\xa5P\x9d\xd3\x83\xe8\xfeKs\x1chp3\x95V\xa5\xc0o\xeb\x9c\xf30\xbb\x1b', # noqa: E501
155-
]
155+
],
156+
ids=['test_sig_bytes_standard_v', 'test_sig_bytes_chain_naive_v']
156157
)
157158
def test_eth_account_recover_signature_bytes(acct, signature_bytes):
158159
msg_hash = b'\xbb\r\x8a\xba\x9f\xf7\xa1<N,s{i\x81\x86r\x83{\xba\x9f\xe2\x1d\xaa\xdd\xb3\xd6\x01\xda\x00\xb7)\xa1' # noqa: E501
@@ -201,7 +202,8 @@ def test_eth_account_recover_vrs_standard_v(acct):
201202
'Hello World',
202203
HexBytes('0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2'),
203204
),
204-
]
205+
],
206+
ids=['message_to_sign', 'hexstr_as_text', 'hello_world']
205207
)
206208
def test_eth_account_hash_message_text(acct, message, expected):
207209
assert acct.hashMessage(text=message) == expected
@@ -218,7 +220,8 @@ def test_eth_account_hash_message_text(acct, message, expected):
218220
'0x29d9f7d6a1d1e62152f314f04e6bd4300ad56fd72102b6b83702869a089f470c',
219221
HexBytes('0xe709159ef0e6323c705786fc50e47a8143812e9f82f429e585034777c7bf530b'),
220222
),
221-
]
223+
],
224+
ids=['hexbytes_1', 'hexbytes_2']
222225
)
223226
def test_eth_account_hash_message_hexstr(acct, message, expected):
224227
assert acct.hashMessage(hexstr=message) == expected
@@ -238,6 +241,7 @@ def test_eth_account_hash_message_hexstr(acct, message, expected):
238241
HexBytes('0xb91467e570a6466aa9e9876cbcd013baba02900b8979d43fe208a4a4f339f5fd6007e74cd82e037b800186422fc2da167c747ef045e5d18a5f5d4300f8e1a0291c'), # noqa: E501
239242
),
240243
),
244+
ids=['account_1']
241245
)
242246
def test_eth_account_sign(acct, message, key, expected_bytes, expected_hash, v, r, s, signature):
243247
signed = acct.sign(message_text=message, private_key=key)
@@ -272,6 +276,7 @@ def test_eth_account_sign(acct, message, key, expected_bytes, expected_hash, v,
272276
37,
273277
),
274278
),
279+
ids=['account_1']
275280
)
276281
def test_eth_account_sign_transaction(acct, txn, private_key, expected_raw_tx, tx_hash, r, s, v):
277282
signed = acct.signTransaction(txn, private_key)

tests/core/eth-module/test_eth_contract.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from unittest.mock import Mock
43

54

tests/core/middleware/test_stalecheck.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import pytest
3+
from unittest.mock import Mock, patch
34

45
from web3.middleware import make_stalecheck_middleware
56
from web3.middleware.stalecheck import (
@@ -8,8 +9,6 @@
89
)
910
from web3.utils.datastructures import AttributeDict
1011

11-
from unittest.mock import Mock, patch
12-
1312

1413
@pytest.fixture
1514
def now():

tests/core/web3-module/test_conversions.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
(256, b'\x01\x00'),
1919
(True, b'\x01'),
2020
(False, b'\x00'),
21-
)
21+
),
2222
)
2323
def test_to_bytes_primitive(val, expected):
2424
assert Web3.toBytes(val) == expected
@@ -36,7 +36,7 @@ def test_to_bytes_primitive(val, expected):
3636
('0x100', b'\x01\x00'),
3737
('0x0000', b'\x00\x00'),
3838
('0000', b'\x00\x00'),
39-
)
39+
),
4040
)
4141
def test_to_bytes_hexstr(val, expected):
4242
assert Web3.toBytes(hexstr=val) == expected
@@ -47,7 +47,7 @@ def test_to_bytes_hexstr(val, expected):
4747
(
4848
('cowmö', b'cowm\xc3\xb6'),
4949
('', b''),
50-
)
50+
),
5151
)
5252
def test_to_bytes_text(val, expected):
5353
assert Web3.toBytes(text=val) == expected
@@ -66,7 +66,7 @@ def test_to_text_identity():
6666
('0x636f776dc3b6', 'cowmö'),
6767
(0x636f776dc3b6, 'cowmö'),
6868
('0xa', '\n'),
69-
)
69+
),
7070
)
7171
def test_to_text(val, expected):
7272
assert Web3.toText(val) == expected
@@ -79,7 +79,7 @@ def test_to_text(val, expected):
7979
('0xa', '\n'),
8080
('0x636f776dc3b6', 'cowmö'),
8181
('636f776dc3b6', 'cowmö'),
82-
)
82+
),
8383
)
8484
def test_to_text_hexstr(val, expected):
8585
assert Web3.toText(hexstr=val) == expected
@@ -98,7 +98,7 @@ def test_to_text_hexstr(val, expected):
9898
('-1', TypeError),
9999
('0x0', TypeError),
100100
('0x1', TypeError),
101-
)
101+
),
102102
)
103103
def test_to_int(val, expected):
104104
if isinstance(expected, type):
@@ -118,7 +118,7 @@ def test_to_int(val, expected):
118118
('0x1', ValueError),
119119
('1.1', ValueError),
120120
('a', ValueError),
121-
)
121+
),
122122
)
123123
def test_to_int_text(val, expected):
124124
if isinstance(expected, type):
@@ -139,7 +139,7 @@ def test_to_int_text(val, expected):
139139
('1', 1),
140140
('01', 1),
141141
('10', 16),
142-
)
142+
),
143143
)
144144
def test_to_int_hexstr(val, expected):
145145
assert Web3.toInt(hexstr=val) == expected
@@ -162,7 +162,7 @@ def test_to_int_hexstr(val, expected):
162162
(0x0F, '0xf'),
163163
(False, '0x0'),
164164
(True, '0x1'),
165-
)
165+
),
166166
)
167167
def test_to_hex(val, expected):
168168
assert Web3.toHex(val) == expected
@@ -173,7 +173,7 @@ def test_to_hex(val, expected):
173173
(
174174
('', '0x'),
175175
('cowmö', '0x636f776dc3b6'),
176-
)
176+
),
177177
)
178178
def test_to_hex_text(val, expected):
179179
assert Web3.toHex(text=val) == expected
@@ -188,7 +188,7 @@ def test_to_hex_text(val, expected):
188188
('0x10', '0x10'),
189189
('0xF', '0xf'),
190190
('F', '0xf'),
191-
)
191+
),
192192
)
193193
def test_to_hex_cleanup_only(val, expected):
194194
assert Web3.toHex(hexstr=val) == expected

tests/core/web3-module/test_sha3.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[
1212
('cowmö', HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')),
1313
('', HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')),
14-
]
14+
],
1515
)
1616
def test_sha3_text(message, digest):
1717
assert Web3.sha3(text=message) == digest
@@ -36,7 +36,7 @@ def test_sha3_text(message, digest):
3636
'',
3737
HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
3838
),
39-
]
39+
],
4040
)
4141
def test_sha3_hexstr(hexstr, digest):
4242
assert Web3.sha3(hexstr=hexstr) == digest
@@ -48,7 +48,7 @@ def test_sha3_hexstr(hexstr, digest):
4848
('0x0', TypeError),
4949
('', TypeError),
5050
(-1, ValueError),
51-
]
51+
],
5252
)
5353
def test_sha3_primitive_invalid(primitive, exception):
5454
with pytest.raises(exception):
@@ -66,7 +66,7 @@ def test_sha3_primitive_invalid(primitive, exception):
6666
b'',
6767
HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
6868
),
69-
]
69+
],
7070
)
7171
def test_sha3_primitive(primitive, digest):
7272
assert Web3.sha3(primitive) == digest
@@ -78,7 +78,7 @@ def test_sha3_primitive(primitive, digest):
7878
{'text': ''},
7979
{'hexstr': '0x'},
8080
{'text': '', 'hexstr': '0x'},
81-
]
81+
],
8282
)
8383
def test_sha3_raise_if_primitive_and(kwargs):
8484
# must not set more than one input

web3/account.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from collections import (
32
Mapping,
43
)
@@ -135,8 +134,6 @@ def setKeyBackend(self, backend):
135134
def sign(self, message=None, private_key=None, message_hexstr=None, message_text=None):
136135
'''
137136
@param private_key in bytes, str, or int.
138-
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
139-
In Python 3, only a str object will be interpreted as hexstr
140137
'''
141138
msg_bytes = to_bytes(message, hexstr=message_hexstr, text=message_text)
142139
msg_hash = self.hashMessage(msg_bytes)
@@ -156,8 +153,6 @@ def sign(self, message=None, private_key=None, message_hexstr=None, message_text
156153
def signTransaction(self, transaction_dict, private_key):
157154
'''
158155
@param private_key in bytes, str, or int.
159-
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
160-
In Python 3, only a str object will be interpreted as hexstr
161156
'''
162157
assert isinstance(transaction_dict, Mapping)
163158

web3/middleware/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import absolute_import
21
import functools
32

43
from .abi import ( # noqa: F401

web3/providers/eth_tester/main.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
from __future__ import (
2-
absolute_import,
3-
unicode_literals,
4-
)
5-
61
import operator
72
import random
83
import sys

web3/utils/encoding.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ def to_hex(value=None, hexstr=None, text=None):
147147
return to_hex(text=value)
148148

149149
if is_integer(value):
150-
# python2 longs end up with an `L` hanging off the end of their hexidecimal
151-
# representation.
152-
return hex(value).rstrip('L')
150+
return hex(value)
153151

154152
raise TypeError(
155153
"Unsupported type: '{0}'. Must be one of Boolean, Dictionary, String, "
@@ -193,7 +191,6 @@ def to_bytes(primitive=None, hexstr=None, text=None):
193191
elif is_integer(primitive):
194192
return to_bytes(hexstr=to_hex(primitive))
195193
elif hexstr is not None:
196-
hexstr = hexstr.rstrip('L') # handle longs in Python 2
197194
if len(hexstr) % 2:
198195
hexstr = '0x0' + remove_0x_prefix(hexstr)
199196
return decode_hex(hexstr)
@@ -203,10 +200,6 @@ def to_bytes(primitive=None, hexstr=None, text=None):
203200

204201

205202
def to_text(primitive=None, hexstr=None, text=None):
206-
if bytes is str:
207-
# must be able to tell the difference between bytes and a hexstr
208-
raise NotImplementedError("This method only works in Python 3+.")
209-
210203
assert_one_val(primitive, hexstr=hexstr, text=text)
211204

212205
if hexstr is not None:
@@ -230,9 +223,7 @@ def text_if_str(to_type, text_or_primitive):
230223
231224
@param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text),
232225
eg~ to_bytes, to_text, to_hex, to_int, etc
233-
@param hexstr_or_primitive in bytes, str, or int. (or unicode in py2)
234-
In Python 2, only a unicode object will be interpreted as unicode text
235-
In Python 3, only a str object will be interpreted as interpreted as unicode text
226+
@param hexstr_or_primitive in bytes, str, or int.
236227
'''
237228
if isinstance(text_or_primitive, str):
238229
(primitive, text) = (None, text_or_primitive)
@@ -248,9 +239,7 @@ def hexstr_if_str(to_type, hexstr_or_primitive):
248239
249240
@param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text),
250241
eg~ to_bytes, to_text, to_hex, to_int, etc
251-
@param text_or_primitive in bytes, str, or int. (or unicode in py2)
252-
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
253-
In Python 3, only a str object will be interpreted as hexstr
242+
@param text_or_primitive in bytes, str, or int.
254243
'''
255244
if isinstance(hexstr_or_primitive, str):
256245
(primitive, hexstr) = (None, hexstr_or_primitive)

web3/utils/signing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from web3.utils.encoding import (
32
to_bytes,
43
to_int,

0 commit comments

Comments
 (0)