Repository: ethereum/web3.py

Issue:
Rename middleware_stack to middleware_onion
### How can it be fixed?

see #1020  



Discussion:

Resolving patch:
diff --git a/web3/__init__.py b/web3/__init__.py
--- a/web3/__init__.py
+++ b/web3/__init__.py
@@ -1,8 +1,17 @@
 import pkg_resources
 import sys
+import warnings
+
+if (3, 5) <= sys.version_info < (3, 6):
+    warnings.warn(
+        "Support for Python 3.5 will be removed in web3.py v5",
+        category=DeprecationWarning,
+        stacklevel=2)
 
 if sys.version_info < (3, 5):
-    raise EnvironmentError("Python 3.5 or above is required")
+    raise EnvironmentError(
+        "Python 3.5 or above is required. "
+        "Note that support for Python 3.5 will be remove in web3.py v5")
 
 from eth_account import Account  # noqa: E402
 from web3.main import Web3  # noqa: E402
diff --git a/web3/main.py b/web3/main.py
--- a/web3/main.py
+++ b/web3/main.py
@@ -4,7 +4,7 @@
     from_wei,
     is_address,
     is_checksum_address,
-    keccak,
+    keccak as eth_utils_keccak,
     remove_0x_prefix,
     to_checksum_address,
     to_wei,
@@ -51,6 +51,7 @@
 )
 from web3.utils.decorators import (
     combomethod,
+    deprecated_for,
 )
 from web3.utils.empty import empty
 from web3.utils.encoding import (
@@ -132,16 +133,22 @@ def providers(self, providers):
         self.manager.providers = providers
 
     @staticmethod
+    @deprecated_for("This method has been renamed to keccak")
     @apply_to_return_value(HexBytes)
     def sha3(primitive=None, text=None, hexstr=None):
+        return Web3.keccak(primitive, text, hexstr)
+
+    @staticmethod
+    @apply_to_return_value(HexBytes)
+    def keccak(primitive=None, text=None, hexstr=None):
         if isinstance(primitive, (bytes, int, type(None))):
             input_bytes = to_bytes(primitive, hexstr=hexstr, text=text)
-            return keccak(input_bytes)
+            return eth_utils_keccak(input_bytes)
 
         raise TypeError(
-            "You called sha3 with first arg %r and keywords %r. You must call it with one of "
-            "these approaches: sha3(text='txt'), sha3(hexstr='0x747874'), "
-            "sha3(b'\\x74\\x78\\x74'), or sha3(0x747874)." % (
+            "You called keccak with first arg %r and keywords %r. You must call it with one of "
+            "these approaches: keccak(text='txt'), keccak(hexstr='0x747874'), "
+            "keccak(b'\\x74\\x78\\x74'), or keccak(0x747874)." % (
                 primitive,
                 {'text': text, 'hexstr': hexstr}
             )


Test patch:
diff --git a/tests/core/contracts/test_extracting_event_data.py b/tests/core/contracts/test_extracting_event_data.py
--- a/tests/core/contracts/test_extracting_event_data.py
+++ b/tests/core/contracts/test_extracting_event_data.py
@@ -123,7 +123,7 @@ def test_dynamic_length_argument_extraction(web3,
     event_topic = emitter_log_topics.LogDynamicArgs
     assert event_topic in log_entry['topics']
 
-    string_0_topic = web3.sha3(text=string_0)
+    string_0_topic = web3.keccak(text=string_0)
     assert string_0_topic in log_entry['topics']
 
     event_data = get_event_data(event_abi, log_entry)
diff --git a/tests/core/contracts/test_extracting_event_data_old.py b/tests/core/contracts/test_extracting_event_data_old.py
--- a/tests/core/contracts/test_extracting_event_data_old.py
+++ b/tests/core/contracts/test_extracting_event_data_old.py
@@ -123,7 +123,7 @@ def test_dynamic_length_argument_extraction(web3,
     event_topic = emitter_log_topics.LogDynamicArgs
     assert event_topic in log_entry['topics']
 
-    string_0_topic = web3.sha3(text=string_0)
+    string_0_topic = web3.keccak(text=string_0)
     assert string_0_topic in log_entry['topics']
 
     event_data = get_event_data(event_abi, log_entry)
diff --git a/tests/core/web3-module/test_sha3.py b/tests/core/web3-module/test_keccak.py
similarity index 53%
rename from tests/core/web3-module/test_sha3.py
rename to tests/core/web3-module/test_keccak.py
--- a/tests/core/web3-module/test_sha3.py
+++ b/tests/core/web3-module/test_keccak.py
@@ -9,6 +9,17 @@
 from web3 import Web3
 
 
+@pytest.mark.parametrize(
+    'message, digest',
+    [
+        ('cowmö', HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')),
+        ('', HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')),
+    ],
+)
+def test_keccak_text(message, digest):
+    assert Web3.keccak(text=message) == digest
+
+
 @pytest.mark.parametrize(
     'message, digest',
     [
@@ -17,7 +28,33 @@
     ],
 )
 def test_sha3_text(message, digest):
-    assert Web3.sha3(text=message) == digest
+    with pytest.deprecated_call():
+        assert Web3.sha3(text=message) == digest
+
+
+@pytest.mark.parametrize(
+    'hexstr, digest',
+    [
+        (
+            '0x636f776dc3b6',
+            HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')
+        ),
+        (
+            '636f776dc3b6',
+            HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')
+        ),
+        (
+            '0x',
+            HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
+        ),
+        (
+            '',
+            HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
+        ),
+    ],
+)
+def test_keccak_hexstr(hexstr, digest):
+    assert Web3.keccak(hexstr=hexstr) == digest
 
 
 @pytest.mark.parametrize(
@@ -42,7 +79,8 @@ def test_sha3_text(message, digest):
     ],
 )
 def test_sha3_hexstr(hexstr, digest):
-    assert Web3.sha3(hexstr=hexstr) == digest
+    with pytest.deprecated_call():
+        assert Web3.sha3(hexstr=hexstr) == digest
 
 
 @pytest.mark.parametrize(
@@ -53,9 +91,9 @@ def test_sha3_hexstr(hexstr, digest):
         (-1, ValueError),
     ],
 )
-def test_sha3_primitive_invalid(primitive, exception):
+def test_keccak_primitive_invalid(primitive, exception):
     with pytest.raises(exception):
-        Web3.sha3(primitive)
+        Web3.keccak(primitive)
 
 
 @pytest.mark.parametrize(
@@ -71,8 +109,8 @@ def test_sha3_primitive_invalid(primitive, exception):
         ),
     ],
 )
-def test_sha3_primitive(primitive, digest):
-    assert Web3.sha3(primitive) == digest
+def test_keccak_primitive(primitive, digest):
+    assert Web3.keccak(primitive) == digest
 
 
 @pytest.mark.parametrize(
@@ -83,17 +121,17 @@ def test_sha3_primitive(primitive, digest):
         {'text': '', 'hexstr': '0x'},
     ],
 )
-def test_sha3_raise_if_primitive_and(kwargs):
+def test_keccak_raise_if_primitive_and(kwargs):
     # must not set more than one input
     with pytest.raises(TypeError):
-        Web3.sha3('', **kwargs)
+        Web3.keccak('', **kwargs)
 
 
-def test_sha3_raise_if_hexstr_and_text():
+def test_keccak_raise_if_hexstr_and_text():
     with pytest.raises(TypeError):
-        Web3.sha3(hexstr='0x', text='')
+        Web3.keccak(hexstr='0x', text='')
 
 
-def test_sha3_raise_if_no_args():
+def test_keccak_raise_if_no_args():
     with pytest.raises(TypeError):
-        Web3.sha3()
+        Web3.keccak()
diff --git a/tests/ens/conftest.py b/tests/ens/conftest.py
--- a/tests/ens/conftest.py
+++ b/tests/ens/conftest.py
@@ -107,7 +107,7 @@ def ens_setup():
     ens_key = accounts.pop()
 
     # create ENS contract
-    eth_labelhash = w3.sha3(text='eth')
+    eth_labelhash = w3.keccak(text='eth')
     eth_namehash = bytes32(0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae)
     resolver_namehash = bytes32(0xfdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5)
     reverse_tld_namehash = bytes32(0xa097f6721ce401e757d1223a763fef49b8b5f90bb18567ddb86fd205dff71d34)  # noqa: E501
@@ -126,7 +126,7 @@ def ens_setup():
 
     ens_contract.functions.setSubnodeOwner(
         eth_namehash,
-        w3.sha3(text='resolver'),
+        w3.keccak(text='resolver'),
         ens_key
     ).transact({'from': ens_key})
 
@@ -173,13 +173,13 @@ def ens_setup():
     # set 'addr.reverse' to resolve to reverse registrar
     ens_contract.functions.setSubnodeOwner(
         b'\0' * 32,
-        w3.sha3(text='reverse'),
+        w3.keccak(text='reverse'),
         ens_key
     ).transact({'from': ens_key})
 
     ens_contract.functions.setSubnodeOwner(
         reverse_tld_namehash,
-        w3.sha3(text='addr'),
+        w3.keccak(text='addr'),
         ens_key
     ).transact({'from': ens_key})
 
@@ -196,7 +196,7 @@ def ens_setup():
     # set owner of tester.eth to an account controlled by tests
     ens_contract.functions.setSubnodeOwner(
         eth_namehash,
-        w3.sha3(text='tester'),
+        w3.keccak(text='tester'),
         w3.eth.accounts[2]  # note that this does not have to be the default, only in the list
     ).transact({'from': ens_key})
 
@@ -210,7 +210,7 @@ def ens_setup():
     # make the reverse registrar the owner of the 'addr.reverse' name
     ens_contract.functions.setSubnodeOwner(
         reverse_tld_namehash,
-        w3.sha3(text='addr'),
+        w3.keccak(text='addr'),
         reverse_registrar.address
     ).transact({'from': ens_key})