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

chore: add missing tests and update pydantic lint items #139

Merged
merged 5 commits into from
Oct 3, 2024
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 10 additions & 8 deletions ethpm_types/contract_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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
Expand All @@ -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``
Expand Down Expand Up @@ -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] = []
Expand Down
4 changes: 2 additions & 2 deletions ethpm_types/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
max-line-length = 100
extend-ignore = E203,E701
extend-ignore = E203,E701,PYD002
exclude =
venv*
.eggs
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_schema(self):
"type": "object",
}
},
"allOf": [{"$ref": "#/$defs/ABIType"}],
"$ref": "#/$defs/ABIType",
}
assert actual == expected

Expand Down
30 changes: 30 additions & 0 deletions tests/test_basemodel.py
Original file line number Diff line number Diff line change
@@ -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"}'