Skip to content

Add black to ethpm #2345

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

Merged
merged 1 commit into from
Jun 15, 2022
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
4 changes: 2 additions & 2 deletions ethpm/_utils/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

@to_tuple
def get_translatable_backends_for_uri(
uri: URI
uri: URI,
) -> Generator[Type[BaseURIBackend], None, None]:
# type ignored because of conflict with instantiating BaseURIBackend
for backend in ALL_URI_BACKENDS:
Expand All @@ -56,7 +56,7 @@ def get_translatable_backends_for_uri(

@to_tuple
def get_resolvable_backends_for_uri(
uri: URI
uri: URI,
) -> Generator[Type[BaseURIBackend], None, None]:
# special case the default IPFS backend to the first slot.
default_ipfs = get_ipfs_backend_class()
Expand Down
2 changes: 1 addition & 1 deletion ethpm/_utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class cached_property:
def __init__(self, func: Callable[..., Any], name: str = None) -> None:
self.wrapped_func = func
self.name = name
self.__doc__ = getattr(func, '__doc__')
self.__doc__ = getattr(func, "__doc__")

def __get__(self, instance: Any, cls: Any = None) -> Any:
"""
Expand Down
6 changes: 5 additions & 1 deletion ethpm/_utils/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def parse_BIP122_uri(blockchain_uri: URI) -> Tuple[HexStr, str, HexStr]:
if match is None:
raise ValueError(f"Invalid URI format: '{blockchain_uri}'")
chain_id, resource_type, resource_hash = match.groups()
return (add_0x_prefix(HexStr(chain_id)), resource_type, add_0x_prefix(HexStr(resource_hash)))
return (
add_0x_prefix(HexStr(chain_id)),
resource_type,
add_0x_prefix(HexStr(resource_hash)),
)


def is_BIP122_block_uri(value: URI) -> bool:
Expand Down
19 changes: 9 additions & 10 deletions ethpm/backends/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
# TODO: Update registry ABI once ERC is finalized.
REGISTRY_ABI = fetch_standard_registry_abi()
RegistryURI = namedtuple(
"RegistryURI",
["address", "chain_id", "name", "version", "namespaced_asset", "ens"]
"RegistryURI", ["address", "chain_id", "name", "version", "namespaced_asset", "ens"]
)


Expand All @@ -48,6 +47,7 @@ class RegistryURIBackend(BaseURIBackend):

def __init__(self) -> None:
from web3.auto.infura import w3

self.w3 = w3

def can_translate_uri(self, uri: str) -> bool:
Expand All @@ -61,11 +61,9 @@ def fetch_uri_contents(self, uri: str) -> URI:
Return content-addressed URI stored at registry URI.
"""
address, chain_id, pkg_name, pkg_version, _, _ = parse_registry_uri(uri)
if chain_id != '1':
if chain_id != "1":
# todo: support all testnets
raise CannotHandleURI(
"Currently only mainnet registry uris are supported."
)
raise CannotHandleURI("Currently only mainnet registry uris are supported.")
self.w3.enable_unstable_package_management_api()
self.w3.pm.set_registry(address)
_, _, manifest_uri = self.w3.pm.get_release_data(pkg_name, pkg_version)
Expand All @@ -90,6 +88,7 @@ def parse_registry_uri(uri: str) -> RegistryURI:
Validate and return (authority, chain_id, pkg_name, version) from a valid registry URI.
"""
from web3.auto.infura import w3

validate_registry_uri(uri)
parsed_uri = parse.urlparse(uri)
if ":" in parsed_uri.netloc:
Expand All @@ -104,14 +103,14 @@ def parse_registry_uri(uri: str) -> RegistryURI:
address = ns.address(address_or_ens)
ens = address_or_ens
else:
raise CannotHandleURI(
f"Invalid address or ENS domain found in uri: {uri}."
)
raise CannotHandleURI(f"Invalid address or ENS domain found in uri: {uri}.")
pkg_name, pkg_version, namespaced_asset = _process_pkg_path(parsed_uri.path)
return RegistryURI(address, chain_id, pkg_name, pkg_version, namespaced_asset, ens)


def _process_pkg_path(raw_pkg_path: str) -> Tuple[Optional[str], Optional[str], Optional[str]]:
def _process_pkg_path(
raw_pkg_path: str,
) -> Tuple[Optional[str], Optional[str], Optional[str]]:
pkg_path = raw_pkg_path.strip("/")
if not pkg_path:
return None, None, None
Expand Down
12 changes: 5 additions & 7 deletions ethpm/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def __init__(self, address: bytes, **kwargs: Any) -> None:
super().__init__(address=address, **kwargs) # type: ignore

@classmethod
def factory(
cls, w3: "Web3", class_name: str = None, **kwargs: Any
) -> Contract:
def factory(cls, w3: "Web3", class_name: str = None, **kwargs: Any) -> Contract:
dep_link_refs = kwargs.get("unlinked_references")
bytecode = kwargs.get("bytecode")
needs_bytecode_linking = False
Expand Down Expand Up @@ -97,9 +95,7 @@ def link_bytecode(cls, attr_dict: Dict[str, str]) -> Type["LinkableContract"]:
runtime = apply_all_link_refs(
cls.bytecode_runtime, cls.linked_references, attr_dict
)
linked_class = cls.factory(
cls.w3, bytecode_runtime=runtime, bytecode=bytecode
)
linked_class = cls.factory(cls.w3, bytecode_runtime=runtime, bytecode=bytecode)
if linked_class.needs_bytecode_linking:
raise BytecodeLinkingError(
"Expected class to be fully linked, but class still needs bytecode linking."
Expand Down Expand Up @@ -176,6 +172,8 @@ def apply_link_ref(offset: int, length: int, value: bytes, bytecode: bytes) -> b
address = value if is_canonical_address(value) else to_canonical_address(value)
new_bytes = (
# Ignore linting error b/c conflict b/w black & flake8
bytecode[:offset] + address + bytecode[offset + length:] # noqa: E201, E203
bytecode[:offset]
+ address
+ bytecode[offset + length :] # noqa: E201, E203
)
return new_bytes
18 changes: 7 additions & 11 deletions ethpm/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ def contract_types(self) -> List[str]:
"""
All contract types included in this package.
"""
if 'contractTypes' in self.manifest:
return sorted(self.manifest['contractTypes'].keys())
if "contractTypes" in self.manifest:
return sorted(self.manifest["contractTypes"].keys())
else:
raise ValueError("No contract types found in manifest; {self.__repr__()}.")

Expand Down Expand Up @@ -306,9 +306,7 @@ def get_contract_instance(self, name: ContractName, address: Address) -> Contrac
contract_kwargs = generate_contract_factory_kwargs(
self.manifest["contractTypes"][name]
)
contract_instance = self.w3.eth.contract(
address=address, **contract_kwargs
)
contract_instance = self.w3.eth.contract(address=address, **contract_kwargs)
# TODO: type ignore may be able to be removed after more of AsyncContract is finished
return contract_instance # type: ignore

Expand Down Expand Up @@ -372,9 +370,7 @@ def deployments(self) -> Union["Deployments", Dict[None, None]]:
linked_deployments = get_linked_deployments(deployments)
if linked_deployments:
for deployment_data in linked_deployments.values():
on_chain_bytecode = self.w3.eth.get_code(
deployment_data["address"]
)
on_chain_bytecode = self.w3.eth.get_code(deployment_data["address"])
unresolved_linked_refs = normalize_linked_references(
deployment_data["runtimeBytecode"]["linkDependencies"]
)
Expand All @@ -392,15 +388,15 @@ def _get_all_contract_instances(
self, deployments: Dict[str, DeploymentData]
) -> Iterable[Tuple[str, Contract]]:
for deployment_name, deployment_data in deployments.items():
if deployment_data['contractType'] not in self.contract_types:
if deployment_data["contractType"] not in self.contract_types:
raise EthPMValidationError(
f"Contract type: {deployment_data['contractType']} for alias: "
f"{deployment_name} not found. Available contract types include: "
f"{self.contract_types}."
)
contract_instance = self.get_contract_instance(
ContractName(deployment_data['contractType']),
deployment_data['address'],
ContractName(deployment_data["contractType"]),
deployment_data["address"],
)
yield deployment_name, contract_instance

Expand Down
5 changes: 4 additions & 1 deletion ethpm/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .get_manifest import get_ethpm_local_manifest, get_ethpm_spec_manifest # noqa: F401
from .get_manifest import ( # noqa: F401
get_ethpm_local_manifest,
get_ethpm_spec_manifest,
)
16 changes: 10 additions & 6 deletions ethpm/tools/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def _contract_type(
contract_type_data = all_type_data

if "compiler" in contract_type_data:
compiler_info = contract_type_data.pop('compiler')
compiler_info = contract_type_data.pop("compiler")
contract_type_ref = alias if alias else name
manifest_with_compilers = add_compilers_to_manifest(
compiler_info, contract_type_ref, manifest
Expand All @@ -422,7 +422,9 @@ def _contract_type(
["contractTypes", alias],
assoc(contract_type_data, "contractType", name),
)
return assoc_in(manifest_with_compilers, ["contractTypes", name], contract_type_data)
return assoc_in(
manifest_with_compilers, ["contractTypes", name], contract_type_data
)


def add_compilers_to_manifest(
Expand All @@ -432,7 +434,7 @@ def add_compilers_to_manifest(
Adds a compiler information object to a manifest's top-level `compilers`.
"""
if "compilers" not in manifest:
compiler_info['contractTypes'] = [contract_type]
compiler_info["contractTypes"] = [contract_type]
return assoc_in(manifest, ["compilers"], [compiler_info])

updated_compiler_info = update_compilers_object(
Expand All @@ -443,7 +445,9 @@ def add_compilers_to_manifest(

@to_list
def update_compilers_object(
new_compiler: Dict[str, Any], contract_type: str, previous_compilers: List[Dict[str, Any]]
new_compiler: Dict[str, Any],
contract_type: str,
previous_compilers: List[Dict[str, Any]],
) -> Iterable[Dict[str, Any]]:
"""
Updates a manifest's top-level `compilers` with a new compiler information object.
Expand Down Expand Up @@ -528,9 +532,9 @@ def normalize_contract_type(
contract_type_data["evm"]["deployedBytecode"]
)
if "devdoc" in contract_type_data:
yield "devdoc", contract_type_data['devdoc']
yield "devdoc", contract_type_data["devdoc"]
if "userdoc" in contract_type_data:
yield "userdoc", contract_type_data['userdoc']
yield "userdoc", contract_type_data["userdoc"]
# make sure metadata isn't an empty string in solc output
if "metadata" in contract_type_data and contract_type_data["metadata"]:
yield "compiler", normalize_compiler_object(
Expand Down
2 changes: 1 addition & 1 deletion ethpm/tools/get_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def get_ethpm_spec_manifest(use_case: str, filename: str) -> Dict[str, Any]:
ethpm_spec_dir = get_ethpm_spec_dir()
return json.loads((ethpm_spec_dir / 'examples' / use_case / filename).read_text())
return json.loads((ethpm_spec_dir / "examples" / use_case / filename).read_text())


def get_ethpm_local_manifest(use_case: str, filename: str) -> Dict[str, Any]:
Expand Down
4 changes: 3 additions & 1 deletion ethpm/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def create_latest_block_uri(w3: "Web3", from_blocks_ago: int = 3) -> URI:
"""
chain_id = to_hex(get_genesis_block_hash(w3))
latest_block_tx_receipt = w3.eth.get_block("latest")
target_block_number = BlockNumber(latest_block_tx_receipt["number"] - from_blocks_ago)
target_block_number = BlockNumber(
latest_block_tx_receipt["number"] - from_blocks_ago
)
if target_block_number < 0:
raise Exception(
f"Only {latest_block_tx_receipt['number']} blocks avaible on provided w3, "
Expand Down
4 changes: 1 addition & 3 deletions ethpm/validation/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ def validate_manifest_deployments(manifest: Dict[str, Any]) -> None:
all_contract_types = manifest["contractTypes"].keys()
all_deployments = manifest["deployments"].values()
all_deployment_names = extract_contract_types_from_deployments(all_deployments)
missing_contract_types = all_deployment_names.difference(
all_contract_types
)
missing_contract_types = all_deployment_names.difference(all_contract_types)
if missing_contract_types:
raise EthPMValidationError(
f"Manifest missing references to contracts: {missing_contract_types}."
Expand Down
8 changes: 5 additions & 3 deletions ethpm/validation/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def validate_registry_uri(uri: str) -> None:
if pkg_name:
validate_package_name(pkg_name)
if not pkg_name and pkg_version:
raise EthPMValidationError("Registry URIs cannot provide a version without a package name.")
raise EthPMValidationError(
"Registry URIs cannot provide a version without a package name."
)
if pkg_version:
validate_escaped_string(pkg_version)

Expand All @@ -88,9 +90,9 @@ def validate_registry_uri_authority(auth: str) -> None:
f"{auth} is not a valid registry URI authority. "
"Please try again with a valid registry URI."
)
address, chain_id = auth.split(':')
address, chain_id = auth.split(":")
else:
address, chain_id = auth, '1'
address, chain_id = auth, "1"

if is_ens_domain(address) is False and not is_checksum_address(address):
raise EthPMValidationError(
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2345.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add black linting to ethpm module
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ showcontent=true
directory = "misc"
name="Misc"
showcontent=false

[tool.isort]
profile = "black"

[flake8]
max-line-length=88
extend-ignore="E203"
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"py-geth>=3.8.0,<4",
],
'linter': [
"black>=22.1.0,<23.0",
"flake8==3.8.3",
"isort>=4.2.15,<4.3.5",
"mypy==0.910",
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ basepython=python
extras=linter
commands=
flake8 {toxinidir}/web3 {toxinidir}/ens {toxinidir}/ethpm {toxinidir}/tests --exclude {toxinidir}/ethpm/ethpm-spec
black {toxinidir}/ethpm --exclude {toxinidir}/ethpm/ethpm-spec --check
isort --recursive --check-only --diff {toxinidir}/web3/ {toxinidir}/ens/ {toxinidir}/ethpm/ {toxinidir}/tests/
mypy -p web3 -p ethpm -p ens --config-file {toxinidir}/mypy.ini

Expand Down