Skip to content

Commit 489babe

Browse files
authored
fix: dataclassy regression (#55)
* chore: upgrade dataclassy to v0.10.1 * refactor: normalize abstract dataclass usage * fix: forgot to implement abstractmethod (now visible with previous) * fix: remove unused Web3 subclassing NOTE: Fixes issue with MRO introduced in dataclassy v0.10.1 * fix: remove unnecessary abstractmethod (visible because of abc fix) * fix: incorrect `__contains__` use for `AccountContainerAPI` * feat: add `remove`/`__delitem__` to `AccountContainerAPI`
1 parent 1131c11 commit 489babe

10 files changed

Lines changed: 63 additions & 30 deletions

File tree

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
install_requires=[
6262
"backports.cached_property ; python_version<'3.8'",
6363
"click>=8.0.0",
64-
"dataclassy==0.10.0", # see https://github.com/biqqles/dataclassy/issues/46
64+
"dataclassy>=0.10.1,<1.0",
6565
"eth-account>=0.5.2,<0.6.0",
6666
"pluggy>=0.13.1,<1.0",
6767
"PyGithub>=1.54,<2.0",

src/ape/api/accounts.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from abc import ABCMeta, abstractmethod
21
from pathlib import Path
32
from typing import TYPE_CHECKING, Iterator, Optional, Type
43

5-
from dataclassy import dataclass
64
from eth_account.datastructures import SignedMessage # type: ignore
75
from eth_account.datastructures import SignedTransaction
86
from eth_account.messages import SignableMessage # type: ignore
97

8+
from .base import abstractdataclass, abstractmethod
9+
1010
if TYPE_CHECKING:
1111
from ape.managers.networks import NetworkManager
1212

1313

14-
@dataclass
15-
class AddressAPI(metaclass=ABCMeta):
14+
@abstractdataclass
15+
class AddressAPI:
1616
network_manager: Optional["NetworkManager"] = None
1717

1818
@property
@@ -78,8 +78,8 @@ def sign_transaction(self, txn: dict) -> Optional[SignedTransaction]:
7878
...
7979

8080

81-
@dataclass
82-
class AccountContainerAPI(metaclass=ABCMeta):
81+
@abstractdataclass
82+
class AccountContainerAPI:
8383
data_folder: Path
8484
account_type: Type[AccountAPI]
8585

@@ -107,17 +107,31 @@ def append(self, account: AccountAPI):
107107
if not isinstance(account, self.account_type):
108108
raise # Not the right type for this container
109109

110-
if account in self:
110+
if account.address in self:
111111
raise # Account already in container
112112

113113
if account.alias and account.alias in self.aliases:
114114
raise # Alias already in use
115115

116116
self.__setitem__(account.address, account)
117117

118-
@abstractmethod
119118
def __setitem__(self, address: str, account: AccountAPI):
120-
raise NotImplementedError("Must define this method to use `container.append(...)`")
119+
raise NotImplementedError("Must define this method to use `container.append(acct)`")
120+
121+
def remove(self, account: AccountAPI):
122+
if not isinstance(account, self.account_type):
123+
raise # Not the right type for this container
124+
125+
if account.address not in self:
126+
raise # Account not in container
127+
128+
if account.alias and account.alias in self.aliases:
129+
raise # Alias already in use
130+
131+
self.__delitem__(account.address)
132+
133+
def __delitem__(self, address: str):
134+
raise NotImplementedError("Must define this method to use `container.remove(acct)`")
121135

122136
def __contains__(self, address: str) -> bool:
123137
try:

src/ape/api/base.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from abc import ABCMeta, abstractmethod
2+
from functools import partial
3+
4+
from dataclassy import dataclass
5+
from dataclassy.dataclass import DataClassMeta
6+
7+
8+
class AbstractDataClassMeta(DataClassMeta, ABCMeta):
9+
pass
10+
11+
12+
abstractdataclass = partial(dataclass, meta=AbstractDataClassMeta)
13+
14+
__all__ = [
15+
"abstractdataclass",
16+
"abstractmethod",
17+
"AbstractDataClassMeta",
18+
"dataclass",
19+
]

src/ape/api/compiler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from abc import ABC, abstractmethod
21
from pathlib import Path
32
from typing import List, Set
43

54
from ape.types import ContractType
65

6+
from .base import abstractdataclass, abstractmethod
77

8-
class CompilerAPI(ABC):
8+
9+
@abstractdataclass
10+
class CompilerAPI:
911
@property
1012
@abstractmethod
1113
def name(self) -> str:

src/ape/api/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from enum import Enum
22
from typing import Dict, Union
33

4-
from dataclassy import dataclass
4+
from .base import dataclass
55

66

77
class ConfigEnum(str, Enum):

src/ape/api/explorers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
from abc import ABCMeta, abstractmethod
2-
3-
from dataclassy import dataclass
4-
51
from . import networks
2+
from .base import abstractdataclass, abstractmethod
63

74

8-
@dataclass
9-
class ExplorerAPI(metaclass=ABCMeta):
5+
@abstractdataclass
6+
class ExplorerAPI:
107
"""
118
An Explorer must work with a particular Network in a particular Ecosystem
129
"""

src/ape/api/networks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from abc import ABCMeta, abstractmethod
21
from functools import partial
32
from pathlib import Path
43
from typing import TYPE_CHECKING, Dict, Iterator, List, Optional, Type
54

6-
from dataclassy import dataclass
75
from pluggy import PluginManager # type: ignore
86

97
from ape.utils import cached_property
108

9+
from .base import abstractdataclass, abstractmethod, dataclass
10+
1111
if TYPE_CHECKING:
1212
from ape.managers.networks import NetworkManager
1313

@@ -127,8 +127,8 @@ def __exit__(self, *args, **kwargs):
127127
self.network_manager.active_provider = self._connected_providers[-1]
128128

129129

130-
@dataclass
131-
class NetworkAPI(metaclass=ABCMeta):
130+
@abstractdataclass
131+
class NetworkAPI:
132132
"""
133133
A Network is a wrapper around a Provider for a specific Ecosystem
134134
"""

src/ape/api/providers.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
from abc import ABCMeta, abstractmethod
21
from pathlib import Path
32

4-
from dataclassy import dataclass
5-
63
from . import networks
4+
from .base import abstractdataclass, abstractmethod
75

86

9-
@dataclass
10-
class ProviderAPI(metaclass=ABCMeta):
7+
@abstractdataclass
8+
class ProviderAPI:
119
"""
1210
A Provider must work with a particular Network in a particular Ecosystem
1311
"""

src/ape_infura/providers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ape.api import ProviderAPI
77

88

9-
class Infura(Web3, ProviderAPI):
9+
class Infura(ProviderAPI):
1010
_web3: Web3 = None # type: ignore
1111

1212
def __post_init__(self):

src/ape_test/providers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ape.api import ProviderAPI
44

55

6-
class LocalNetwork(Web3, ProviderAPI):
6+
class LocalNetwork(ProviderAPI):
77
_web3: Web3 = None # type: ignore
88

99
def connect(self):
@@ -12,6 +12,9 @@ def connect(self):
1212
def disconnect(self):
1313
pass
1414

15+
def update_settings(self, new_settings: dict):
16+
pass
17+
1518
def __post_init__(self):
1619
self._web3 = Web3(EthereumTesterProvider())
1720

0 commit comments

Comments
 (0)