Skip to content

Commit

Permalink
refactor: move pydantic imports to compat
Browse files Browse the repository at this point in the history
  • Loading branch information
banteg committed Sep 9, 2023
1 parent d943d74 commit 29e0e59
Show file tree
Hide file tree
Showing 21 changed files with 51 additions and 98 deletions.
31 changes: 31 additions & 0 deletions src/ape/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# support both pydantic v1 and v2
try:
from pydantic.v1 import (
BaseModel,
BaseSettings,
Extra,
Field,
FileUrl,
HttpUrl,
NonNegativeInt,
PositiveInt,
ValidationError,
root_validator,
validator,
)
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic import (
BaseModel,
BaseSettings,
Extra,
Field,
FileUrl,
HttpUrl,
NonNegativeInt,
PositiveInt,
ValidationError,
root_validator,
validator,
)
from pydantic.dataclasses import dataclass
6 changes: 1 addition & 5 deletions src/ape/api/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from enum import Enum
from typing import Any, Dict, Optional, TypeVar

try:
from pydantic.v1 import BaseModel, BaseSettings
except ImportError:
from pydantic import BaseModel, BaseSettings

from ape._compat import BaseModel, BaseSettings

T = TypeVar("T")

Expand Down
6 changes: 1 addition & 5 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
from ethpm_types import ContractType, HexBytes
from ethpm_types.abi import ABIType, ConstructorABI, EventABI, MethodABI

try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel

from ape._compat import BaseModel
from ape.exceptions import (
NetworkError,
NetworkMismatchError,
Expand Down
7 changes: 1 addition & 6 deletions src/ape/api/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
from ethpm_types.manifest import PackageName
from ethpm_types.utils import AnyUrl, compute_checksum
from packaging.version import InvalidVersion, Version

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError

from yaml import safe_dump

from ape._compat import ValidationError
from ape.exceptions import ApeAttributeError
from ape.logging import logger
from ape.utils import (
Expand Down
7 changes: 1 addition & 6 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@
from ethpm_types import HexBytes
from evm_trace import CallTreeNode as EvmCallTreeNode
from evm_trace import TraceFrame as EvmTraceFrame

try:
from pydantic.v1 import Field, root_validator, validator
except ImportError:
from pydantic import Field, root_validator, validator

from web3 import Web3
from web3.exceptions import ContractLogicError as Web3ContractLogicError
from web3.exceptions import MethodUnavailable, TimeExhausted, TransactionNotFound
from web3.types import RPCEndpoint, TxParams

from ape._compat import Field, root_validator, validator
from ape.api.config import PluginConfig
from ape.api.networks import LOCAL_NETWORK_NAME, NetworkAPI
from ape.api.query import BlockTransactionQuery
Expand Down
6 changes: 1 addition & 5 deletions src/ape/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

from ethpm_types.abi import EventABI, MethodABI

try:
from pydantic.v1 import BaseModel, NonNegativeInt, PositiveInt, root_validator
except ImportError:
from pydantic import BaseModel, NonNegativeInt, PositiveInt, root_validator

from ape._compat import BaseModel, NonNegativeInt, PositiveInt, root_validator
from ape.api.transactions import ReceiptAPI, TransactionAPI
from ape.logging import logger
from ape.types import AddressType
Expand Down
3 changes: 1 addition & 2 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
from eth_utils import is_0x_prefixed, is_hex, to_int
from ethpm_types import HexBytes
from ethpm_types.abi import EventABI, MethodABI
from pydantic import validator
from pydantic.fields import Field
from tqdm import tqdm # type: ignore

from ape._compat import Field, validator
from ape.api.explorers import ExplorerAPI
from ape.exceptions import (
NetworkError,
Expand Down
6 changes: 1 addition & 5 deletions src/ape/managers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Union

try:
from pydantic.v1 import root_validator
except ImportError:
from pydantic import root_validator

from ape._compat import root_validator
from ape.api import ConfigDict, DependencyAPI, PluginConfig
from ape.exceptions import ConfigError, NetworkError
from ape.logging import logger
Expand Down
2 changes: 1 addition & 1 deletion src/ape/managers/project/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

from ethpm_types import PackageManifest
from ethpm_types.utils import AnyUrl
from pydantic import FileUrl, HttpUrl, root_validator
from semantic_version import NpmSpec, Version # type: ignore

from ape._compat import FileUrl, HttpUrl, root_validator
from ape.api import DependencyAPI
from ape.exceptions import ProjectError, UnknownVersionError
from ape.logging import logger
Expand Down
7 changes: 1 addition & 6 deletions src/ape/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@
)
from ethpm_types.abi import EventABI
from ethpm_types.source import Closure

try:
from pydantic.v1 import BaseModel, root_validator, validator
except ImportError:
from pydantic import BaseModel, root_validator, validator

from web3.types import FilterParams

from ape._compat import BaseModel, root_validator, validator
from ape.exceptions import ApeAttributeError
from ape.types.address import AddressType, RawAddress
from ape.types.coverage import (
Expand Down
6 changes: 1 addition & 5 deletions src/ape/types/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
from ethpm_types import BaseModel
from ethpm_types.source import ContractSource, SourceLocation

try:
from pydantic.v1 import NonNegativeInt, validator
except ImportError:
from pydantic import NonNegativeInt, validator

from ape._compat import NonNegativeInt, validator
from ape.logging import logger
from ape.utils.misc import get_current_timestamp_ms
from ape.version import version as ape_version
Expand Down
6 changes: 1 addition & 5 deletions src/ape/types/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
from eth_account.messages import SignableMessage
from eth_utils import to_bytes, to_hex

try:
from pydantic.v1.dataclasses import dataclass
except ImportError:
from pydantic.dataclasses import dataclass

from ape._compat import dataclass
from ape.types import AddressType


Expand Down
7 changes: 1 addition & 6 deletions src/ape/types/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
from ethpm_types.ast import SourceLocation
from ethpm_types.source import Closure, Content, Function, SourceStatement, Statement
from evm_trace.gas import merge_reports

try:
from pydantic.v1 import Field
except ImportError:
from pydantic import Field

from rich.table import Table
from rich.tree import Tree

from ape._compat import Field
from ape.types.address import AddressType
from ape.utils.basemodel import BaseInterfaceModel
from ape.utils.misc import is_evm_precompile, is_zero_hex
Expand Down
6 changes: 1 addition & 5 deletions src/ape_compile/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from typing import Any, List, Optional

try:
from pydantic.v1 import Field, validator
except ImportError:
from pydantic import Field, validator

from ape import plugins
from ape._compat import Field, validator
from ape.api import PluginConfig
from ape.logging import logger

Expand Down
6 changes: 1 addition & 5 deletions src/ape_ethereum/ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
from ethpm_types import ContractType, HexBytes
from ethpm_types.abi import ABIType, ConstructorABI, EventABI, MethodABI

try:
from pydantic.v1 import Field, validator
except ImportError:
from pydantic import Field, validator

from ape._compat import Field, validator
from ape.api import BlockAPI, EcosystemAPI, PluginConfig, ReceiptAPI, TransactionAPI
from ape.api.networks import LOCAL_NETWORK_NAME
from ape.contracts.base import ContractCall
Expand Down
6 changes: 1 addition & 5 deletions src/ape_ethereum/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
from ethpm_types import ContractType, HexBytes
from ethpm_types.abi import EventABI, MethodABI

try:
from pydantic.v1 import BaseModel, Field, root_validator, validator
except ImportError:
from pydantic import BaseModel, Field, root_validator, validator

from ape._compat import BaseModel, Field, root_validator, validator
from ape.api import ReceiptAPI, TransactionAPI
from ape.contracts import ContractEvent
from ape.exceptions import OutOfGasError, SignatureError, TransactionError
Expand Down
7 changes: 1 addition & 6 deletions src/ape_geth/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
from geth.chain import initialize_chain # type: ignore
from geth.process import BaseGethProcess # type: ignore
from geth.wrapper import construct_test_chain_kwargs # type: ignore

try:
from pydantic.v1 import Extra
except ImportError:
from pydantic import Extra

from requests.exceptions import ConnectionError
from web3 import HTTPProvider, Web3
from web3.exceptions import ExtraDataLengthError
Expand All @@ -41,6 +35,7 @@
from web3.providers.auto import load_provider_from_environment
from yarl import URL

from ape._compat import Extra
from ape.api import (
PluginConfig,
SubprocessProvider,
Expand Down
6 changes: 1 addition & 5 deletions src/ape_plugins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
import sys
from typing import List, Optional, Tuple

try:
from pydantic.v1 import root_validator
except ImportError:
from pydantic import root_validator

from ape.__modules__ import __modules__
from ape._compat import root_validator
from ape.logging import CliLogger
from ape.plugins import clean_plugin_name
from ape.utils import BaseInterfaceModel, cached_property, get_package_version, github_client
Expand Down
6 changes: 1 addition & 5 deletions src/ape_test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from typing import Dict, List, NewType, Optional, Union

try:
from pydantic.v1 import NonNegativeInt
except ImportError:
from pydantic import NonNegativeInt

from ape import plugins
from ape._compat import NonNegativeInt
from ape.api import PluginConfig
from ape.api.networks import LOCAL_NETWORK_NAME
from ape.utils import DEFAULT_HD_PATH, DEFAULT_NUMBER_OF_TEST_ACCOUNTS, DEFAULT_TEST_MNEMONIC
Expand Down
6 changes: 1 addition & 5 deletions tests/functional/conversion/test_encode_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
from ethpm_types import HexBytes
from ethpm_types.abi import MethodABI

try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel

from ape._compat import BaseModel
from ape.types import AddressType

ABI = MethodABI.parse_obj(
Expand Down
6 changes: 1 addition & 5 deletions tests/functional/test_contract_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
from eth_utils import is_checksum_address, to_hex
from ethpm_types import ContractType, HexBytes

try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel

from ape._compat import BaseModel
from ape import Contract
from ape.api import TransactionAPI
from ape.contracts import ContractInstance
Expand Down

0 comments on commit 29e0e59

Please sign in to comment.