Skip to content

Commit abb0e41

Browse files
authored
Merge pull request #1440 from njgheorghita/ethpm-contract-types-property
Ethpm contract types property
2 parents 9da4053 + 8989750 commit abb0e41

File tree

7 files changed

+52
-61
lines changed

7 files changed

+52
-61
lines changed

docs/ethpm.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Properties
4747
Each ``Package`` exposes the following properties.
4848

4949
.. autoclass:: ethpm.Package
50-
:members: name, version, manifest_version, uri, __repr__, build_dependencies, deployments
50+
:members: name, version, manifest_version, uri, __repr__, contract_types, build_dependencies, deployments
5151

5252
.. py:attribute:: Package.w3
5353

ethpm/package.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Any,
77
Dict,
88
Generator,
9+
List,
910
Optional,
1011
Tuple,
1112
Union,
@@ -176,6 +177,16 @@ def uri(self) -> Optional[str]:
176177
"""
177178
return self._uri
178179

180+
@property
181+
def contract_types(self) -> List[str]:
182+
"""
183+
All contract types included in this package.
184+
"""
185+
if 'contract_types' in self.manifest:
186+
return sorted(self.manifest['contract_types'].keys())
187+
else:
188+
return ValueError("No contract types found in manifest; {self.__repr__()}.")
189+
179190
@classmethod
180191
def from_file(cls, file_path: Path, w3: Web3) -> "Package":
181192
"""
@@ -202,7 +213,7 @@ def from_uri(cls, uri: URI, w3: Web3) -> "Package":
202213
URI schemes supported:
203214
- IPFS `ipfs://Qm...`
204215
- HTTP `https://api.github.com/repos/:owner/:repo/git/blobs/:file_sha`
205-
- Registry `ercXXX://registry.eth/greeter?version=1.0.0`
216+
- Registry `erc1319://registry.eth:1/greeter?version=1.0.0`
206217
207218
.. code:: python
208219
@@ -253,7 +264,7 @@ def get_contract_factory(self, name: ContractName) -> Contract:
253264
raise InsufficientAssetsError(
254265
"This package does not contain any package data to generate "
255266
f"a contract factory for contract type: {name}. Available contract types include: "
256-
f"{ list(self.manifest['contract_types'].keys()) }."
267+
f"{self.contract_types}."
257268
)
258269

259270
validate_minimal_contract_factory_data(contract_data)
@@ -361,13 +372,12 @@ def deployments(self) -> Union["Deployments", Dict[None, None]]:
361372

362373
@to_dict
363374
def _get_all_contract_instances(self, deployments):
364-
contract_types = self.manifest['contract_types'].keys()
365375
for deployment_name, deployment_data in deployments.items():
366-
if deployment_data['contract_type'] not in contract_types:
376+
if deployment_data['contract_type'] not in self.contract_types:
367377
raise EthPMValidationError(
368378
f"Contract type: {deployment_data['contract_type']} for alias: "
369379
f"{deployment_name} not found. Available contract types include: "
370-
f"{list(sorted(contract_types))}."
380+
f"{self.contract_types}."
371381
)
372382
contract_instance = self.get_contract_instance(
373383
deployment_data['contract_type'],

newsfragments/1440.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``contract_types`` property to ``Package`` class.

tests/ethpm/test_package.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ def test_package_object_properties(safe_math_package):
8686
assert safe_math_package.manifest_version == "2"
8787
assert safe_math_package.uri is None
8888
assert safe_math_package.__repr__() == "<Package safe-math-lib==1.0.0>"
89+
assert safe_math_package.contract_types == ["SafeMathLib"]

tests/ethpm/test_package_init_from_file.py renamed to tests/ethpm/test_package_init.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Package,
1010
)
1111
from ethpm.exceptions import (
12+
CannotHandleURI,
1213
EthPMValidationError,
1314
)
1415

@@ -95,3 +96,36 @@ def test_from_file_succeeds_with_valid_manifest(valid_manifest_from_path, w3):
9596
def test_from_file_raises_type_error_with_invalid_param_type():
9697
with pytest.raises(TypeError):
9798
Package.from_file(1)
99+
100+
101+
#
102+
# From URI
103+
#
104+
105+
VALID_IPFS_PKG = "ipfs://QmeD2s7KaBUoGYTP1eutHBmBkMMMoycdfiyGMx2DKrWXyV"
106+
107+
108+
def test_package_from_uri_with_valid_uri(dummy_ipfs_backend, w3):
109+
package = Package.from_uri(VALID_IPFS_PKG, w3)
110+
assert package.name == "safe-math-lib"
111+
assert isinstance(package, Package)
112+
113+
114+
@pytest.mark.parametrize(
115+
"uri",
116+
(
117+
# Invalid
118+
"123",
119+
b"123",
120+
"ipfs://",
121+
"http://QmTKB75Y73zhNbD3Y73xeXGjYrZHmaXXNxoZqGCagu7r8u/readme",
122+
"ipfsQmTKB75Y73zhNbD3Y73xeXGjYrZHmaXXNxoZqGCagu7r8u/readme/",
123+
# Unsupported
124+
"erc111://packages.zeppelin.os/owned",
125+
"bzz://da6adeeb4589d8652bbe5679aae6b6409ec85a20e92a8823c7c99e25dba9493d",
126+
),
127+
)
128+
@pytest.mark.skipif('WEB3_INFURA_PROJECT_ID' not in os.environ, reason='Infura API key unavailable')
129+
def test_package_from_uri_rejects_invalid_ipfs_uri(uri, w3):
130+
with pytest.raises(CannotHandleURI):
131+
Package.from_uri(uri, w3)

tests/ethpm/test_package_init_from_registry_uri.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/ethpm/test_package_init_from_uri.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)