From 003362c0dc4a37a8985d3fac9c98a254197a4eed Mon Sep 17 00:00:00 2001 From: antazoey Date: Thu, 3 Oct 2024 11:05:49 -0500 Subject: [PATCH] chore: add missing tests and update pydantic lint items (#139) --- .pre-commit-config.yaml | 2 +- ethpm_types/contract_type.py | 18 ++++++++++-------- ethpm_types/manifest.py | 4 ++-- setup.cfg | 2 +- setup.py | 3 ++- tests/test_abi.py | 2 +- tests/test_basemodel.py | 30 ++++++++++++++++++++++++++++++ 7 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 tests/test_basemodel.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b3df952f..07eeaaca 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,7 +21,7 @@ repos: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.11.1 + rev: v1.11.2 hooks: - id: mypy additional_dependencies: [types-requests, types-setuptools, pydantic] diff --git a/ethpm_types/contract_type.py b/ethpm_types/contract_type.py index ccddcea2..cba26af6 100644 --- a/ethpm_types/contract_type.py +++ b/ethpm_types/contract_type.py @@ -86,12 +86,14 @@ class Bytecode(BaseModel): A string containing the 0x prefixed hexadecimal representation of the bytecode. """ - link_references: Optional[list[LinkReference]] = Field(None, alias="linkReferences") + link_references: Optional[list[LinkReference]] = Field(default=None, alias="linkReferences") """ The locations in the corresponding bytecode which require linking. """ - link_dependencies: Optional[list[LinkDependency]] = Field(None, alias="linkDependencies") + link_dependencies: Optional[list[LinkDependency]] = Field( + default=None, alias="linkDependencies" + ) """ The link values that have been used to link the corresponding bytecode. """ @@ -113,7 +115,7 @@ def to_bytes(self) -> Optional[HexBytes]: class ContractInstance(BaseModel): - contract_type: str = Field(..., alias="contractType") + contract_type: str = Field(default=..., alias="contractType") """ Any of the contract type names included in this Package or any of the contract type names found in any of the package dependencies @@ -132,7 +134,7 @@ class ContractInstance(BaseModel): contract instance was mined. """ - runtime_bytecode: Optional[Bytecode] = Field(None, alias="runtimeBytecode") + runtime_bytecode: Optional[Bytecode] = Field(default=None, alias="runtimeBytecode") """ The runtime portion of bytecode for this Contract Instance. When present, the value from this field supersedes the ``runtimeBytecode`` @@ -249,21 +251,21 @@ class ContractType(BaseModel): then ``MyContract`` would be the type. """ - name: Optional[str] = Field(None, alias="contractName") + name: Optional[str] = Field(default=None, alias="contractName") """ The name of the contract type. The field is optional if ``ContractAlias`` is the same as ``ContractName``. """ - source_id: Optional[str] = Field(None, alias="sourceId") + source_id: Optional[str] = Field(default=None, alias="sourceId") """ The global source identifier for the source file from which this contract type was generated. """ - deployment_bytecode: Optional[Bytecode] = Field(None, alias="deploymentBytecode") + deployment_bytecode: Optional[Bytecode] = Field(default=None, alias="deploymentBytecode") """The bytecode for the ContractType.""" - runtime_bytecode: Optional[Bytecode] = Field(None, alias="runtimeBytecode") + runtime_bytecode: Optional[Bytecode] = Field(default=None, alias="runtimeBytecode") """The unlinked 0x-prefixed runtime portion of bytecode for this ContractType.""" abi: list[ABI] = [] diff --git a/ethpm_types/manifest.py b/ethpm_types/manifest.py index b47891fb..dd73f2a4 100644 --- a/ethpm_types/manifest.py +++ b/ethpm_types/manifest.py @@ -117,7 +117,7 @@ class PackageManifest(BaseModel): necessary to recompile the contracts contained in this release. """ - contract_types: Optional[dict[str, ContractType]] = Field(None, alias="contractTypes") + contract_types: Optional[dict[str, ContractType]] = Field(default=None, alias="contractTypes") """ :class:`~ethpm_types.contract_type.ContractType` objects that have been included in this release. @@ -146,7 +146,7 @@ class PackageManifest(BaseModel): """ dependencies: Optional[dict[PackageName, AnyUrl]] = Field( # type: ignore[valid-type] - None, alias="buildDependencies" + default=None, alias="buildDependencies" ) """ A mapping of EthPM packages that this project depends on. diff --git a/setup.cfg b/setup.cfg index 4399e484..666d8ae7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [flake8] max-line-length = 100 -extend-ignore = E203,E701 +extend-ignore = E203,E701,PYD002 exclude = venv* .eggs diff --git a/setup.py b/setup.py index b0d28da3..f41d8d6b 100644 --- a/setup.py +++ b/setup.py @@ -14,10 +14,11 @@ # Test-only deps "PyGithub>=1.54,<2.0", # Necessary to pull official schema from github "hypothesis-jsonschema==0.19.0", # Fuzzes based on a json schema + "eth-hash[pysha3]", # For eth-utils address checksumming ], "lint": [ "black>=24.8.0,<25", # Auto-formatter and linter - "mypy>=1.11.1,<2", # Static type analyzer + "mypy>=1.11.2,<2", # Static type analyzer "types-setuptools", # Needed for mypy type shed "types-requests", # Needed for mypy type shed "flake8>=7.1.1,<8", # Style linter diff --git a/tests/test_abi.py b/tests/test_abi.py index 40e53353..e322f54d 100644 --- a/tests/test_abi.py +++ b/tests/test_abi.py @@ -77,7 +77,7 @@ def test_schema(self): "type": "object", } }, - "allOf": [{"$ref": "#/$defs/ABIType"}], + "$ref": "#/$defs/ABIType", } assert actual == expected diff --git a/tests/test_basemodel.py b/tests/test_basemodel.py new file mode 100644 index 00000000..4a10a2a4 --- /dev/null +++ b/tests/test_basemodel.py @@ -0,0 +1,30 @@ +from typing import Any + +import pytest +from eth_pydantic_types import HexBytes + +from ethpm_types import BaseModel + + +@pytest.fixture +def MyModel(): + class _MyModel(BaseModel): + name: str + input_types: dict[str, Any] + + return _MyModel + + +@pytest.mark.parametrize("mode", ("python", "json")) +def test_model_dump(mode, MyModel): + model = MyModel(name="foo", input_types={"name": [HexBytes(123)]}) + actual = model.model_dump(mode=mode) + assert isinstance(actual, dict) + assert actual["name"] == "foo" + assert len(actual["input_types"]) == 1 + + +def test_model_dump_json(MyModel): + model = MyModel(name="foo", input_types={"name": [HexBytes(123)]}) + actual = model.model_dump_json() + assert actual == '{"input_types":{"name":["{"]},"name":"foo"}'