Skip to content
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

Python modernization 2 #471

Merged
merged 10 commits into from
Nov 29, 2017
8 changes: 1 addition & 7 deletions tests/core/contracts/test_concise_contract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import sys
from unittest.mock import Mock

import pytest

Expand All @@ -13,9 +14,6 @@
ConciseMethod,
)

if sys.version_info >= (3, 3):
from unittest.mock import Mock


@pytest.fixture()
def EMPTY_ADDR():
Expand All @@ -35,7 +33,6 @@ def zero_address_contract(web3, WithConstructorAddressArgumentsContract, EMPTY_A
return ConciseContract(_address_contract)


@pytest.mark.skipif(sys.version_info < (3, 3), reason="needs Mock library from 3.3")
def test_concisecontract_call_default():
contract = Mock()
sweet_method = ConciseMethod(contract, 'grail')
Expand All @@ -44,7 +41,6 @@ def test_concisecontract_call_default():
contract.call().grail.assert_called_once_with(1, 2)


@pytest.mark.skipif(sys.version_info < (3, 3), reason="needs Mock library from 3.3")
def test_concisecontract_custom_transact():
contract = Mock()
sweet_method = ConciseMethod(contract, 'grail')
Expand All @@ -53,15 +49,13 @@ def test_concisecontract_custom_transact():
contract.transact().grail.assert_called_once_with(1, 2)


@pytest.mark.skipif(sys.version_info < (3, 3), reason="needs Mock library from 3.3")
def test_concisecontract_two_keywords_fail():
contract = Mock()
sweet_method = ConciseMethod(contract, 'grail')
with pytest.raises(TypeError):
sweet_method(1, 2, transact={'holy': 3}, call={'count_to': 4})


@pytest.mark.skipif(sys.version_info < (3, 3), reason="needs Mock library from 3.3")
def test_concisecontract_unknown_keyword_fails():
contract = Mock()
sweet_method = ConciseMethod(contract, 'grail')
Expand Down
5 changes: 0 additions & 5 deletions tests/integration/test_ethereum_tester.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import sys

import pytest

Expand Down Expand Up @@ -173,10 +172,6 @@ def _check_web3_clientVersion(self, client_version):
def not_implemented(method, exc_type=NotImplementedError):
@functools.wraps(method)
def inner(*args, **kwargs):
if sys.version_info.major == 2:
# pytest doesn't do the right thing with the fixture arguments in
# python2 so just don't run it.
return
with pytest.raises(exc_type):
method(*args, **kwargs)
return inner
Expand Down
5 changes: 0 additions & 5 deletions tests/integration/test_ethtestrpc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import sys

import pytest

Expand Down Expand Up @@ -133,10 +132,6 @@ def _check_web3_clientVersion(self, client_version):
def not_implemented(method, exc_type=AttributeError):
@functools.wraps(method)
def inner(*args, **kwargs):
if sys.version_info.major == 2:
# pytest doesn't do the right thing with the fixture arguments in
# python2 so just don't run it.
return
with pytest.raises(exc_type):
method(*args, **kwargs)
return inner
Expand Down
5 changes: 0 additions & 5 deletions tests/integration/test_goethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import signal
import socket
import subprocess
import sys
import shutil
import time
import tempfile
Expand All @@ -26,10 +25,6 @@
)


if sys.version_info.major == 2:
FileNotFoundError = OSError


KEYFILE_PW = 'web3py-test'


Expand Down
4 changes: 0 additions & 4 deletions web3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import pkg_resources
import sys

if sys.version_info.major < 3:
raise EnvironmentError("Python 3 is required")

from web3.account import Account # noqa: E402
from web3.main import Web3 # noqa: E402
Expand Down
13 changes: 2 additions & 11 deletions web3/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
)
import json
import os
import sys

from cytoolz import (
compose,
Expand Down Expand Up @@ -36,8 +35,7 @@
hexstr_if_str,
text_if_str,
to_bytes,
to_int,
to_hex,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

to_int
)
from web3.utils.signing import (
LocalAccount,
Expand All @@ -60,14 +58,11 @@ class Account(object):
def create(self, extra_entropy=''):
extra_key_bytes = text_if_str(to_bytes, extra_entropy)
key_bytes = keccak(os.urandom(32) + extra_key_bytes)
if sys.version_info.major < 3:
key_bytes = to_hex(key_bytes)
return self.privateKeyToAccount(key_bytes)

@staticmethod
def decrypt(keyfile_json, password):
if isinstance(keyfile_json, str) or (
sys.version_info.major < 3 and isinstance(keyfile_json, unicode)): # noqa: 821
if isinstance(keyfile_json, str): # noqa: 821
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why noqa here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@techtonik that was already in the code so I just left it there. I'm not sure what the comment is referencing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, these noqa comments are a way of telling flake8 not to issue the specified warning. Most of the time it's best to fix the thing causing the warning, but occasionally the best thing to do is suppress it.

The list of warnings are here: http://flake8.pycqa.org/en/3.4.1/user/error-codes.html#error-violation-codes

821 is usage of an undefined name, which is usually pretty bad, but in this case it was just because unicode isn't defined in py3, and we know a py3 codepath to that unicode in this line is impossible.

Now that unicode is removed, we can and should remove the warning supressor.

keyfile = json.loads(keyfile_json)
elif is_dict(keyfile_json):
keyfile = keyfile_json
Expand Down Expand Up @@ -123,8 +118,6 @@ def recoverTransaction(self, serialized_transaction):
txn_bytes = HexBytes(serialized_transaction)
txn = Transaction.from_bytes(txn_bytes)
msg_hash = hash_of_signed_transaction(txn)
if sys.version_info.major < 3:
msg_hash = to_hex(msg_hash)
return self.recover(msg_hash, vrs=vrs_from(txn))

def setKeyBackend(self, backend):
Expand All @@ -133,7 +126,6 @@ def setKeyBackend(self, backend):
def sign(self, message=None, private_key=None, message_hexstr=None, message_text=None):
'''
@param private_key in bytes, str, or int.
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
In Python 3, only a str object will be interpreted as hexstr
'''
msg_bytes = to_bytes(message, hexstr=message_hexstr, text=message_text)
Expand All @@ -153,7 +145,6 @@ def sign(self, message=None, private_key=None, message_hexstr=None, message_text
def signTransaction(self, transaction_dict, private_key):
'''
@param private_key in bytes, str, or int.
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
In Python 3, only a str object will be interpreted as hexstr
'''
assert isinstance(transaction_dict, Mapping)
Expand Down
23 changes: 6 additions & 17 deletions web3/utils/encoding.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# String encodings and numeric representations
import json
import re
import sys

import rlp
from rlp.sedes import big_endian_int
Expand Down Expand Up @@ -73,7 +72,7 @@ def hex_encode_abi_type(abi_type, value, force_size=None):
elif is_address_type(abi_type):
return pad_hex(value, data_size)
elif is_bytes_type(abi_type):
if sys.version_info.major >= 3 and is_bytes(value):
if is_bytes(value):
return encode_hex(value)
else:
return value
Expand Down Expand Up @@ -194,7 +193,6 @@ def to_bytes(primitive=None, hexstr=None, text=None):
elif is_integer(primitive):
return to_bytes(hexstr=to_hex(primitive))
elif hexstr is not None:
hexstr = hexstr.rstrip('L') # handle longs in Python 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if len(hexstr) % 2:
hexstr = '0x0' + remove_0x_prefix(hexstr)
return decode_hex(hexstr)
Expand Down Expand Up @@ -231,16 +229,10 @@ def text_if_str(to_type, text_or_primitive):

@param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text),
eg~ to_bytes, to_text, to_hex, to_int, etc
@param hexstr_or_primitive in bytes, str, or int. (or unicode in py2)
In Python 2, only a unicode object will be interpreted as unicode text
In Python 3, only a str object will be interpreted as interpreted as unicode text
@param hexstr_or_primitive in bytes, str, or int.
In Python 3, only a str object will be interpreted as unicode text
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is all Python 3 now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess its safe to delete all the "python 3" comments?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

'''
if sys.version_info.major < 3:
if isinstance(text_or_primitive, unicode): # noqa: F821
(primitive, text) = (None, text_or_primitive)
else:
(primitive, text) = (text_or_primitive, None)
elif isinstance(text_or_primitive, str):
if isinstance(text_or_primitive, str):
(primitive, text) = (None, text_or_primitive)
else:
(primitive, text) = (text_or_primitive, None)
Expand All @@ -254,13 +246,10 @@ def hexstr_if_str(to_type, hexstr_or_primitive):

@param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text),
eg~ to_bytes, to_text, to_hex, to_int, etc
@param text_or_primitive in bytes, str, or int. (or unicode in py2)
In Python 2, a bytes, unicode or str object will be interpreted as hexstr
@param text_or_primitive in bytes, str, or int.
In Python 3, only a str object will be interpreted as hexstr
'''
if isinstance(hexstr_or_primitive, str) or (
sys.version_info.major < 3 and isinstance(hexstr_or_primitive, unicode) # noqa: F821
):
if isinstance(hexstr_or_primitive, str): # noqa: F821
(primitive, hexstr) = (None, hexstr_or_primitive)
if remove_0x_prefix(hexstr) and not is_hex(hexstr):
raise ValueError(
Expand Down
8 changes: 1 addition & 7 deletions web3/utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Iterable,
Mapping,
)
import sys

from cytoolz.functoolz import (
curry,
Expand All @@ -23,12 +22,7 @@
def hex_to_integer(value):
return int(value, 16)


if sys.version_info.major == 2:
def integer_to_hex(value):
return hex(value).rstrip('L')
else:
integer_to_hex = hex
integer_to_hex = hex


@curry
Expand Down
10 changes: 0 additions & 10 deletions web3/utils/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from web3.utils.encoding import (
to_bytes,
to_int,
to_hex,
)

from web3.utils.transactions import (
Expand Down Expand Up @@ -144,8 +143,6 @@ def __init__(self, key, account):
self.address = key.public_key.to_checksum_address()

key_raw = key.to_bytes()
if sys.version_info.major < 3:
key_raw = to_hex(key_raw)
self.privateKey = key_raw

self._key_obj = key
Expand All @@ -167,10 +164,3 @@ def signTransaction(self, transaction_dict):

def __bytes__(self):
return self.privateKey

# Python 2 support
def __str__(self):
if sys.version_info.major < 3:
return self.privateKey
else:
return super().__str__()
20 changes: 5 additions & 15 deletions web3/utils/six/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import sys


if sys.version_info.major == 2:
from .six_py2 import (
urlparse,
urlunparse,
Generator,
)
else:
from .six_py3 import ( # noqa: #401
urlparse,
urlunparse,
Generator,
)
from .six_py3 import ( # noqa: #401
urlparse,
urlunparse,
Generator,
)
3 changes: 1 addition & 2 deletions web3/utils/validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import itertools
import sys

from eth_utils import (
is_0x_prefixed,
Expand Down Expand Up @@ -87,7 +86,7 @@ def validate_abi_value(abi_type, value):
validate_address(value)
return
elif is_bytes_type(abi_type):
if sys.version_info.major >= 3 and is_bytes(value):
if is_bytes(value):
return
elif is_string(value):
if is_0x_prefixed(value):
Expand Down