From 1a718b6d4b8abcdb26890459ef3cc22d012409fc Mon Sep 17 00:00:00 2001 From: kclowes Date: Fri, 11 Feb 2022 15:39:07 -0700 Subject: [PATCH] Add black to ethpm --- ethpm/_utils/backend.py | 4 ++-- ethpm/_utils/cache.py | 2 +- ethpm/_utils/chains.py | 6 +++++- ethpm/backends/registry.py | 19 +++++++++---------- ethpm/contract.py | 12 +++++------- ethpm/package.py | 18 +++++++----------- ethpm/tools/__init__.py | 5 ++++- ethpm/tools/builder.py | 16 ++++++++++------ ethpm/tools/get_manifest.py | 2 +- ethpm/uri.py | 4 +++- ethpm/validation/manifest.py | 4 +--- ethpm/validation/uri.py | 8 +++++--- newsfragments/2345.misc.rst | 1 + pyproject.toml | 7 +++++++ setup.py | 1 + tox.ini | 1 + 16 files changed, 63 insertions(+), 47 deletions(-) create mode 100644 newsfragments/2345.misc.rst diff --git a/ethpm/_utils/backend.py b/ethpm/_utils/backend.py index ae9fdbdd4f..ec9f44d8a2 100644 --- a/ethpm/_utils/backend.py +++ b/ethpm/_utils/backend.py @@ -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: @@ -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() diff --git a/ethpm/_utils/cache.py b/ethpm/_utils/cache.py index 2f322b89df..699908b9de 100644 --- a/ethpm/_utils/cache.py +++ b/ethpm/_utils/cache.py @@ -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: """ diff --git a/ethpm/_utils/chains.py b/ethpm/_utils/chains.py index 3928a6b529..e4529627a1 100644 --- a/ethpm/_utils/chains.py +++ b/ethpm/_utils/chains.py @@ -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: diff --git a/ethpm/backends/registry.py b/ethpm/backends/registry.py index fe9e8828d8..5a316adcc8 100644 --- a/ethpm/backends/registry.py +++ b/ethpm/backends/registry.py @@ -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"] ) @@ -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: @@ -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) @@ -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: @@ -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 diff --git a/ethpm/contract.py b/ethpm/contract.py index e56d6670c9..977fef068d 100644 --- a/ethpm/contract.py +++ b/ethpm/contract.py @@ -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 @@ -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." @@ -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 diff --git a/ethpm/package.py b/ethpm/package.py index f7c8346ff9..37a16cf9b5 100644 --- a/ethpm/package.py +++ b/ethpm/package.py @@ -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__()}.") @@ -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 @@ -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"] ) @@ -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 diff --git a/ethpm/tools/__init__.py b/ethpm/tools/__init__.py index c92e448589..4ed335746a 100644 --- a/ethpm/tools/__init__.py +++ b/ethpm/tools/__init__.py @@ -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, +) diff --git a/ethpm/tools/builder.py b/ethpm/tools/builder.py index 6f38968402..d94d5953c9 100644 --- a/ethpm/tools/builder.py +++ b/ethpm/tools/builder.py @@ -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 @@ -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( @@ -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( @@ -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. @@ -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( diff --git a/ethpm/tools/get_manifest.py b/ethpm/tools/get_manifest.py index 4633f486d1..38a50ba6e7 100644 --- a/ethpm/tools/get_manifest.py +++ b/ethpm/tools/get_manifest.py @@ -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]: diff --git a/ethpm/uri.py b/ethpm/uri.py index 45de062618..753c31f9e1 100644 --- a/ethpm/uri.py +++ b/ethpm/uri.py @@ -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, " diff --git a/ethpm/validation/manifest.py b/ethpm/validation/manifest.py index eb94f86523..4dfa0d4fbc 100644 --- a/ethpm/validation/manifest.py +++ b/ethpm/validation/manifest.py @@ -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}." diff --git a/ethpm/validation/uri.py b/ethpm/validation/uri.py index 4c739e9dbf..6d9a4df988 100644 --- a/ethpm/validation/uri.py +++ b/ethpm/validation/uri.py @@ -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) @@ -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( diff --git a/newsfragments/2345.misc.rst b/newsfragments/2345.misc.rst new file mode 100644 index 0000000000..8eadb172ff --- /dev/null +++ b/newsfragments/2345.misc.rst @@ -0,0 +1 @@ +Add black linting to ethpm module diff --git a/pyproject.toml b/pyproject.toml index 2f5e3368a6..ca235487ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,3 +35,10 @@ showcontent=true directory = "misc" name="Misc" showcontent=false + +[tool.isort] +profile = "black" + +[flake8] +max-line-length=88 +extend-ignore="E203" diff --git a/setup.py b/setup.py index 73e5b2f9dd..093c087a47 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tox.ini b/tox.ini index e4d2e40465..08cd9a0e7e 100644 --- a/tox.ini +++ b/tox.ini @@ -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