Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for external modules to web3 instance
- added web3.attach_module() to attach a single module to a web3 instance e.g. w3.attach_module('module1', ModuleClass1) - added support for initializing the instance with external modules e.g. w3 = Web3(..., external_modules={'module1': ModuleClass1}) - modules can be passed in without needing to be a tuple or list if they don't contain submodules i.e. pass in the class by itself instead a tuple with 1 argument, (ModuleClass1,) - added tests for the above - updated docs for web3 main to include usage examples for the above changes
- Loading branch information
Showing
13 changed files
with
253 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support for attaching external modules to the ``Web3`` instance when instantiating the ``Web3`` instance, via the ``external_modules`` argument, or via the new ``attach_module()`` method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
|
||
from web3.module import ( | ||
Module, | ||
) | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def module1(): | ||
class Module1(Module): | ||
a = 'a' | ||
|
||
@property | ||
def b(self): | ||
return 'b' | ||
return Module1 | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def module2(): | ||
class Module2(Module): | ||
c = 'c' | ||
|
||
@staticmethod | ||
def d(): | ||
return 'd' | ||
return Module2 | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def module3(): | ||
class Module3(Module): | ||
e = 'e' | ||
return Module3 | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def module4(): | ||
class Module4(Module): | ||
f = 'f' | ||
return Module4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from eth_utils import ( | ||
is_integer, | ||
) | ||
|
||
|
||
def test_attach_module(web3, module1, module2, module3, module4): | ||
web3.attach_module('module1', module1) | ||
web3.attach_module( | ||
'module2', | ||
(module2, { | ||
'submodule1': (module3, { | ||
'submodule2': module4, | ||
}) | ||
}) | ||
) | ||
|
||
# assert module1 attached | ||
assert hasattr(web3, 'module1') | ||
assert web3.module1.a == 'a' | ||
assert web3.module1.b == 'b' | ||
|
||
# assert module2 + submodules attached | ||
assert hasattr(web3, 'module2') | ||
assert web3.module2.c == 'c' | ||
assert web3.module2.d() == 'd' | ||
|
||
assert hasattr(web3.module2, 'submodule1') | ||
assert web3.module2.submodule1.e == 'e' | ||
assert hasattr(web3.module2.submodule1, 'submodule2') | ||
assert web3.module2.submodule1.submodule2.f == 'f' | ||
|
||
# assert default modules intact | ||
assert hasattr(web3, 'geth') | ||
assert hasattr(web3, 'eth') | ||
assert is_integer(web3.eth.chain_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.