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

Add support for external modules to web3 instance #2288

Merged
merged 2 commits into from Jan 5, 2022

Conversation

fselmo
Copy link
Collaborator

@fselmo fselmo commented Jan 4, 2022

What was wrong?

  • We don't have an easy way to attach modules to the Web3 instance. This will help users customize their web3 experience but can also help to support things like attaching layer 2 modules from a separate library or any other modules that can prove useful.

  • Closes Difficult to attach one extra module to a web3 instance.. #2231

How was it fixed?

The idea is to just expose the internal api for now. Perhaps improve on this in v6.

  • Added support for attaching external modules at Web3 instantiation. This is separate from the internal default modules already attached at instantiation.
  • Added support for attaching modules via a new attach_modules() method
  • Added support for passing in a module as the class itself if there are no submodules, rather than having to pass in a tuple with a single module class.
  • Tests added

Todo:

  • Add entry to the release notes
  • Add documentation
  • Add cute animal picture

Cute Animal Picture

20220104_155122

@fselmo fselmo force-pushed the attach-external-modules branch 4 times, most recently from b26de80 to d550b85 Compare January 5, 2022 00:13
@fselmo fselmo marked this pull request as ready for review January 5, 2022 00:18
@fselmo fselmo requested review from wolovim and pacrob January 5, 2022 00:18
@fselmo fselmo changed the title [WIP] Add support for external modules to web3 instance Add support for external modules to web3 instance Jan 5, 2022
web3/eth.py Show resolved Hide resolved
@@ -18,11 +18,13 @@

def attach_modules(
parent_module: Union["Web3", "Module"],
module_definitions: Dict[str, Sequence[Any]],
module_definitions: Dict[str, Any],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The recursive nature of this method makes it really hard to add any useful typing here so this has to be Any unfortunately. But I am new to typing in python, maybe there's a way to make this stricter 🤔 . At least all the uses of this method are able to be a tad more strict.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I found this documentation: https://mypy.readthedocs.io/en/stable/protocols.html#recursive-protocols, but I would have to play with it some to figure out how exactly we'd use it. If digging into that seems out of the scope of this PR, I'm fine leaving this as-is.

web3/main.py Outdated Show resolved Hide resolved
- 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
docs/web3.main.rst Show resolved Hide resolved
@@ -18,11 +18,13 @@

def attach_modules(
parent_module: Union["Web3", "Module"],
module_definitions: Dict[str, Sequence[Any]],
module_definitions: Dict[str, Any],
Copy link
Collaborator

Choose a reason for hiding this comment

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

I found this documentation: https://mypy.readthedocs.io/en/stable/protocols.html#recursive-protocols, but I would have to play with it some to figure out how exactly we'd use it. If digging into that seems out of the scope of this PR, I'm fine leaving this as-is.

web3/eth.py Show resolved Hide resolved
docs/web3.main.rst Outdated Show resolved Hide resolved
web3/main.py Outdated Show resolved Hide resolved
web3/_utils/module.py Show resolved Hide resolved
fselmo added a commit to fselmo/web3.py that referenced this pull request Jan 5, 2022
- Add a test to make sure a tuple with a single module class still works for backwards compatibility.
- Take out the docstring examples for `attach_module()` and leave them only in the docs. The consensus is that lengthy docstrings reiterating what is in the documentation would just crowd out our files and make the code harder to read. Instead, we should opt for brief yet useful despritions in the method docstrings.
fselmo added a commit to fselmo/web3.py that referenced this pull request Jan 5, 2022
- Add a test to make sure a tuple with a single module class still works for backwards compatibility.
- Take out the docstring examples for `attach_modules()` and leave them only in the docs. The consensus is that lengthy docstrings reiterating what is in the documentation would just crowd out our files and make the code harder to read. Instead, we should opt for brief yet useful despritions in the method docstrings.
- Change the method to allow for a dict as the argument, as is in the constructor, so that multiple modules may be attached if desired.
fselmo added a commit to fselmo/web3.py that referenced this pull request Jan 5, 2022
- Add a test to make sure a tuple with a single module class still works for backwards compatibility.
- Take out the docstring examples for `attach_modules()` and leave them only in the docs. The consensus is that lengthy docstrings reiterating what is in the documentation would just crowd out our files and make the code harder to read. Instead, we should opt for brief yet useful despritions in the method docstrings.
- Change the method to allow for a dict as the argument, as is in the constructor, so that multiple modules may be attached if desired.
fselmo added a commit to fselmo/web3.py that referenced this pull request Jan 5, 2022
- Add a test to make sure a tuple with a single module class still works for backwards compatibility.
- Take out the docstring examples for `attach_modules()` and leave them only in the docs. The consensus is that lengthy docstrings reiterating what is in the documentation would just crowd out our files and make the code harder to read. Instead, we should opt for brief yet useful despritions in the method docstrings.
- Change the method to allow for a dict as the argument, as is in the constructor, so that multiple modules may be attached if desired.
- Add a test to make sure a tuple with a single module class still works for backwards compatibility.
- Take out the docstring examples for `attach_modules()` and leave them only in the docs. The consensus is that lengthy docstrings reiterating what is in the documentation would just crowd out our files and make the code harder to read. Instead, we should opt for brief yet useful despritions in the method docstrings.
- Change the method to allow for a dict as the argument, as is in the constructor, so that multiple modules may be attached if desired.
- Use the attach_modules utility method as an underscore-aliased internal method for clarity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Difficult to attach one extra module to a web3 instance..
2 participants