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

Disable pm api by default #1216

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions docs/web3.pm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@ To learn more about the EthPM spec, visit the `documentation <http://ethpm.githu
To learn more about the Py-EthPM library used in this module, visit the `documentation <https://py-ethpm.readthedocs.io/en/latest/>`__.


Attaching
---------
To use ``web3.pm``, attach it to your ``web3`` instance.

.. code-block:: python

from web3.pm import PM
PM.attach(web3, 'pm')

.. WARNING::

The ``web3.pm`` API is still under development and likely to change quickly.

Now is a great time to get familiar with the API, and test out writing
code that uses some of the great upcoming features.

By default, access to this module has been turned off in the stable version of Web3.py:

.. code-block:: python

>>> from web3.auto import w3
>>> w3.pm
...
AttributeError: The Package Management feature is disabled by default ...

In order to access these features, you can turn it on with...

.. code-block:: python

>>> web3.enable_unstable_package_management_api()
>>> w3.pm
<web3.pm.PM at 0x....>


Methods
-------
Expand Down
3 changes: 1 addition & 2 deletions tests/core/pm-module/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

from web3 import Web3
from web3.pm import (
PM,
SolidityReferenceRegistry,
VyperReferenceRegistry,
)
Expand Down Expand Up @@ -61,7 +60,7 @@ def setup_w3():
w3 = Web3(Web3.EthereumTesterProvider(ethereum_tester=t))
w3.eth.defaultAccount = w3.eth.accounts[0]
w3.eth.defaultContractFactory = LinkableContract
PM.attach(w3, 'pm')
w3.enable_unstable_package_management_api()
return w3


Expand Down
4 changes: 1 addition & 3 deletions tests/core/pm-module/test_ens_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
InvalidAddress,
)
from web3.pm import (
PM,
VyperReferenceRegistry,
)

Expand Down Expand Up @@ -119,19 +118,18 @@ def ens_setup(deployer):
def ens(ens_setup, mocker):
mocker.patch('web3.middleware.stalecheck._isfresh', return_value=True)
ens_setup.web3.eth.defaultAccount = ens_setup.web3.eth.coinbase
ens_setup.web3.enable_unstable_package_management_api()
return ens_setup


def test_ens_must_be_set_before_ens_methods_can_be_used(ens):
w3 = ens.web3
PM.attach(w3, 'pm')
with pytest.raises(InvalidAddress):
w3.pm.set_registry("tester.eth")


def test_web3_ens(ens):
w3 = ens.web3
PM.attach(w3, 'pm')
ns = ENS.fromWeb3(w3, ens.ens.address)
w3.ens = ns
registry = VyperReferenceRegistry.deploy_new_instance(w3)
Expand Down
3 changes: 1 addition & 2 deletions tests/core/pm-module/test_registry_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
PMError,
)
from web3.pm import (
PM,
ERCRegistry,
VyperReferenceRegistry,
get_vyper_registry_manifest,
Expand All @@ -28,7 +27,7 @@ def fresh_w3():
w3 = Web3(Web3.EthereumTesterProvider())
w3.eth.defaultAccount = w3.eth.accounts[0]
w3.eth.defaultContractFactory = LinkableContract
PM.attach(w3, "pm")
w3.enable_unstable_package_management_api()
return w3


Expand Down
15 changes: 15 additions & 0 deletions web3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,18 @@ def ens(self):
@ens.setter
def ens(self, new_ens):
self._ens = new_ens

@property
def pm(self):
if self._pm is not None:
return self._pm
else:
raise AttributeError(
"The Package Management feature is disabled by default until "
"its API stabilizes. To use these features, please enable them by running "
"`w3.enable_unstable_package_management_api()` and try again."
)

def enable_unstable_package_management_api(self):
from web3.pm import PM
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the quick and easy solution to avoiding the circular import this time around, but it seems to me like this won't be suitable long-term when we want to make the PM api available on the web3 object by default.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm missing it: which circular import? Does web3.pm import from web3.main somehow?

I don't follow how this will be different from any other modules that are attached on Web3 by default.

PM.attach(self, '_pm')