Skip to content

Commit

Permalink
refactor: pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 17, 2023
1 parent f2511ae commit f83271f
Show file tree
Hide file tree
Showing 65 changed files with 411 additions and 396 deletions.
2 changes: 1 addition & 1 deletion docs/userguides/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ In the example above, the bytes value returned contains the method ID selector p
Alternatively, you can decode input:

```python
from ethpm_types import HexBytes
from eth_pydantic_types import HexBytes
from ape import Contract

contract = Contract("0x...")
Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
"packaging>=23.0,<24",
"pandas>=1.3.0,<2",
"pluggy>=1.3,<2",
"pydantic>=1.10.8,<3",
"pydantic>=2.4.0,<3",
"pydantic-settings>=2.0.3,<3",
"PyGithub>=1.59,<2",
"pytest>=6.0,<8.0",
"python-dateutil>=2.8.2,<3",
Expand All @@ -124,8 +125,8 @@
"web3[tester]>=6.7.0,<7",
# ** Dependencies maintained by ApeWorX **
"eip712>=0.2.1,<0.3",
"ethpm-types>=0.5.10,<0.6",
"evm-trace>=0.1.0a23",
"ethpm-types>=0.6.0,<0.7",
"evm-trace>=0.1.0a26",
],
entry_points={
"console_scripts": ["ape=ape._cli:cli"],
Expand Down
2 changes: 1 addition & 1 deletion src/ape/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def display_config(ctx, param, value):
from ape import project

click.echo("# Current configuration")
click.echo(yaml.dump(project.config_manager.dict()))
click.echo(yaml.dump(project.config_manager.model_dump(mode="json")))

ctx.exit() # NOTE: Must exit to bypass running ApeCLI

Expand Down
47 changes: 0 additions & 47 deletions src/ape/_pydantic_compat.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/ape/api/address.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, Any, List

from ethpm_types import HexBytes
from eth_pydantic_types import HexBytes

from ape.exceptions import ConversionError
from ape.types import AddressType, ContractCode
Expand Down
3 changes: 2 additions & 1 deletion src/ape/api/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from pathlib import Path
from typing import Dict, Iterator, List, Optional, Set, Tuple

from ethpm_types import ContractType, HexBytes
from eth_pydantic_types import HexBytes
from ethpm_types import ContractType
from ethpm_types.source import Content, ContractSource
from evm_trace.geth import TraceFrame as EvmTraceFrame
from evm_trace.geth import create_call_node_data
Expand Down
13 changes: 4 additions & 9 deletions src/ape/api/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from enum import Enum
from typing import Any, Dict, Optional, TypeVar

from ape._pydantic_compat import BaseModel, BaseSettings
from pydantic import ConfigDict
from pydantic_settings import BaseSettings

T = TypeVar("T")

Expand All @@ -14,10 +15,6 @@ class ConfigEnum(str, Enum):
"""


class ConfigDict(BaseModel):
__root__: dict = {}


class PluginConfig(BaseSettings):
"""
A base plugin configuration class. Each plugin that includes
Expand All @@ -26,7 +23,7 @@ class PluginConfig(BaseSettings):

@classmethod
def from_overrides(cls, overrides: Dict) -> "PluginConfig":
default_values = cls().dict()
default_values = cls().model_dump()

def update(root: Dict, value_map: Dict):
for key, val in value_map.items():
Expand Down Expand Up @@ -54,9 +51,7 @@ def get(self, key: str, default: Optional[T] = None) -> T:
return self.__dict__.get(key, default)


class GenericConfig(PluginConfig):
class GenericConfig(ConfigDict):
"""
The default class used when no specialized class is used.
"""

__root__: dict = {}
11 changes: 5 additions & 6 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
encode_transaction,
serializable_unsigned_transaction_from_dict,
)
from eth_pydantic_types import HexBytes
from eth_utils import keccak, to_int
from ethpm_types import ContractType, HexBytes
from ethpm_types import BaseModel, ContractType
from ethpm_types.abi import ABIType, ConstructorABI, EventABI, MethodABI

from ape._pydantic_compat import BaseModel
from ape.exceptions import (
NetworkError,
NetworkMismatchError,
Expand Down Expand Up @@ -151,8 +151,7 @@ def serialize_transaction(self, transaction: "TransactionAPI") -> bytes:
if not self.signature:
raise SignatureError("The transaction is not signed.")

txn_data = self.dict(exclude={"sender"})

txn_data = self.model_dump(exclude={"sender"})
unsigned_txn = serializable_unsigned_transaction_from_dict(txn_data)
signature = (
self.signature.v,
Expand Down Expand Up @@ -284,7 +283,7 @@ def default_network(self) -> str:
return self.networks[0]

# Very unlikely scenario.
raise ValueError("No networks found.")
raise NetworkError("No networks found.")

def set_default_network(self, network_name: str):
"""
Expand Down Expand Up @@ -505,7 +504,7 @@ def get_method_selector(self, abi: MethodABI) -> HexBytes:
Override example::
from ape.api import EcosystemAPI
from ethpm_types import HexBytes
from eth_pydantic_types import HexBytes
class MyEcosystem(EcosystemAPI):
def get_method_selector(self, abi: MethodABI) -> HexBytes:
Expand Down
21 changes: 9 additions & 12 deletions src/ape/api/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, Union

from ethpm_types import Checksum, ContractType, PackageManifest, Source
from ethpm_types.manifest import PackageName
from ethpm_types.source import Content
from ethpm_types.utils import Algorithm, AnyUrl, compute_checksum
from ethpm_types.utils import Algorithm, compute_checksum
from packaging.version import InvalidVersion, Version
from pydantic import AnyUrl, ValidationError

from ape._pydantic_compat import ValidationError
from ape.exceptions import ProjectError
from ape.logging import logger
from ape.utils import (
Expand Down Expand Up @@ -112,7 +111,7 @@ def cached_manifest(self) -> Optional[PackageManifest]:
continue

path = self._cache_folder / f"{contract_type.name}.json"
path.write_text(contract_type.json())
path.write_text(contract_type.model_dump_json())

# Rely on individual cache files.
self._contracts = manifest.contract_types
Expand Down Expand Up @@ -147,10 +146,8 @@ def contracts(self) -> Dict[str, ContractType]:
continue

contract_name = p.stem
contract_type = ContractType.parse_file(p)
if contract_type.name is None:
contract_type.name = contract_name

contract_type = ContractType.model_validate_json(p.read_text())
contract_type.name = contract_name if contract_type.name is None else contract_type.name
contracts[contract_type.name] = contract_type

self._contracts = contracts
Expand Down Expand Up @@ -182,7 +179,7 @@ def _create_manifest(
initial_manifest: Optional[PackageManifest] = None,
) -> PackageManifest:
manifest = initial_manifest or PackageManifest()
manifest.name = PackageName(__root__=name.lower()) if name is not None else manifest.name
manifest.name = name.lower() if name is not None else manifest.name
manifest.version = version or manifest.version
manifest.sources = cls._create_source_dict(source_paths, contracts_path)
manifest.contract_types = contract_types
Expand Down Expand Up @@ -219,7 +216,7 @@ def _create_source_dict(
hash=compute_checksum(source_path.read_bytes()),
),
urls=[],
content=Content(__root__={i + 1: x for i, x in enumerate(text.splitlines())}),
content=Content(root={i + 1: x for i, x in enumerate(text.splitlines())}),
imports=source_imports.get(key, []),
references=source_references.get(key, []),
)
Expand Down Expand Up @@ -470,7 +467,7 @@ def _get_sources(self, project: ProjectAPI) -> List[Path]:
def _write_manifest_to_cache(self, manifest: PackageManifest):
self._target_manifest_cache_file.unlink(missing_ok=True)
self._target_manifest_cache_file.parent.mkdir(exist_ok=True, parents=True)
self._target_manifest_cache_file.write_text(manifest.json())
self._target_manifest_cache_file.write_text(manifest.model_dump_json())
self._cached_manifest = manifest


Expand All @@ -479,7 +476,7 @@ def _load_manifest_from_file(file_path: Path) -> Optional[PackageManifest]:
return None

try:
return PackageManifest.parse_file(file_path)
return PackageManifest.model_validate_json(file_path.read_text())
except ValidationError as err:
logger.warning(f"Existing manifest file '{file_path}' corrupted. Re-building.")
logger.debug(str(err))
Expand Down
Loading

0 comments on commit f83271f

Please sign in to comment.