Skip to content

Commit

Permalink
fix: use getattr for EthereumProvider network configs instead of dict…
Browse files Browse the repository at this point in the history
…ionary key lookup (#128)

I am inheriting from NetworkConfig for Hardhat, but the behavior of key
lookup vs. attribute lookup is different for the base class vs.
inherited class.

Base NetworkConfig class:

    (Pdb) self.config
    NetworkConfig(ethereum={'development': {'uri': 'http://localhost:8555'}})
    (Pdb) self.config['ethereum']
    {'development': {'uri': 'http://localhost:8555'}}
    (Pdb) getattr(self.config, 'ethereum')
    {'development': {'uri': 'http://localhost:8555'}}

Vs. my inherited class where the key lookup fails:

    (Pdb) self.config
    HardhatNetworkConfig(ethereum={'development': {'uri': 'http://localhost:8555'}}, fork_url=None, fork_block_number=None, port=8555)
    (Pdb) self.config['ethereum']
    *** KeyError: "''ethereum'"
    (Pdb) self.config.ethereum
    {'development': {'uri': 'http://localhost:8555'}}

Since attribute lookup works on both classes, let's just use that.

Also fixed a typo in the KeyError raised by the ConfigItem class.
  • Loading branch information
lost-theory authored Aug 24, 2021
1 parent aec5e50 commit 1ec197c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ape/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __getitem__(self, attrname: str) -> Any:
if attrname in self.__slots__:
return getattr(self, attrname)

raise KeyError(f"''{attrname}'")
raise KeyError(f"{attrname!r}")


class ConfigDict(ConfigItem):
Expand Down
2 changes: 1 addition & 1 deletion src/ape_http/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EthereumProvider(ProviderAPI):

@property
def uri(self) -> str:
return self.config[self.network.ecosystem.name][self.network.name]["uri"]
return getattr(self.config, self.network.ecosystem.name)[self.network.name]["uri"]

def connect(self):
self._web3 = Web3(HTTPProvider(self.uri))
Expand Down

0 comments on commit 1ec197c

Please sign in to comment.