Skip to content

Brownie KeyError in Local & Remote CI #557

@trentmc

Description

@trentmc

Background

In both local CI and remote CI, Brownie can get a KeyError:

  File "brownie/project/main.py", line 494, in close
    _remove_contract(contract)
  File "brownie/network/state.py", line 586, in _remove_contract
    del _contract_map[contract.address]
KeyError: '0x02175de5A7F168517688e3E93f55936C9c2C7A19'

Below is details for (a) local (b) remote.

KeyError in Local CI

(Taken from README. Written a few months ago.)

When pytest winds down, Brownie emits a KeyError:

  File "brownie/project/main.py", line 494, in close
    _remove_contract(contract)
  File "brownie/network/state.py", line 586, in _remove_contract
    del _contract_map[contract.address]
KeyError: '0x02175de5A7F168517688e3E93f55936C9c2C7A19'

The problem: it's attempting del _contract_map[contract.address] but that contract has already been deleted. This was reported in brownie#1144 for Contract.at() calls. We don't have at() calls yet get it in df-py (only as of Dec 2022). We don't see the issue in ocean.py.

The workaround: open ./venv/lib/python3.10/site-packages/brownie/network/state.py, and change _remove_contract() to the following:

def _remove_contract(contract: Any) -> None:
    try:
        del _contract_map[contract.address]
    except:
        # workaround for brownie issue
        # https://github.com/eth-brownie/brownie/issues/1144
        pass

KeyError in Remote CI

Where seen: in this PR for issue#539.

Log.

Traceback:


Attached to local RPC client listening at '127.0.0.1:8545'...
============================= test session starts ==============================
platform linux -- Python 3.8.16, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/runner/work/df-py/df-py
plugins: web3-5.31.3, xdist-1.34.0, forked-1.4.0, eth-brownie-1.19.3, hypothesis-6.27.3
collected 267 items

util/test/test_DFRewards.py ........                                     [  2%]
util/test/test_DFRewards_allocate_gas.py ...                             [  4%]
util/test/test_DFRewards_withdraw.py ...                                 [  5%]
util/test/test_Simpletoken.py ...                                        [  6%]
util/test/test_allocations.py .....                                      [  8%]
util/test/test_blockrange.py ..............                              [ 13%]
util/test/test_blocktime_eth_mainnet.py .....                            [ 15%]
util/test/test_blocktime_ganache.py ........                             [ 18%]
util/test/test_calc_did.py .                                             [ 18%]
util/test/test_calcrewards.py .....................................s...  [ 34%]
util/test/test_cleancase.py ..........                                   [ 37%]
util/test/test_contract_persistence.py ..                                [ 38%]
util/test/test_csvs.py ..................                                [ 45%]
util/test/test_dftool.py .....s....                                      [ 49%]
util/test/test_dispense.py ...                                           [ 50%]
util/test/test_getrate.py .....ss                                        [ 52%]
util/test/test_goerli.py ...                                             [ 53%]
util/test/test_judge.py ......                                           [ 56%]
util/test/test_networkutil.py ....                                       [ 57%]
util/test/test_oceanutil.py .....                                        [ 59%]
util/test/test_query.py ...............                                  [ 65%]
util/test/test_retryfunction.py .                                        [ 65%]
util/test/test_thegraph.py .                                             [ 65%]
util/test/test_tok.py ......                                             [ 68%]
util/test/test_tousd.py .........                                        [ 71%]
util/test/test_vesting_schedule.py ..................................... [ 85%]
......................                                                   [ 93%]
util/test/veOcean/test_allocate_dt.py .......                            [ 96%]
util/test/veOcean/test_delegation.py .                                   [ 96%]
util/test/veOcean/test_deploy.py .                                       [ 97%]
util/test/veOcean/test_estimateclaim.py .                                [ 97%]
util/test/veOcean/test_fee_dist.py ...                                   [ 98%]
util/test/veOcean/test_fee_estimate.py .                                 [ 98%]
util/test/veOcean/test_lock.py .                                         [ 99%]
/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/brownie/network/main.py:44: BrownieEnvironmentWarning: Development network has a block height of 25
  warnings.warn(
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pytest/__main__.py", line 5, in <module>
    raise SystemExit(pytest.console_main())
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/_pytest/config/__init__.py", line 185, in console_main
    code = main()
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/_pytest/config/__init__.py", line 162, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/_pytest/main.py", line 316, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/_pytest/main.py", line 311, in wrap_session
    config._ensure_unconfigure()
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/_pytest/config/__init__.py", line 987, in _ensure_unconfigure
    self.hook.pytest_unconfigure(config=self)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/brownie/test/managers/base.py", line 267, in pytest_unconfigure
    project.close(raises=False)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/brownie/project/main.py", line 494, in close
    _remove_contract(contract)
  File "/opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/brownie/network/state.py", line 586, in _remove_contract
    del _contract_map[contract.address]
KeyError: '0x471817de04FAa9B616ED7644117d957439717bF9'
util/test/veOcean/test_whitelist_sc.py ..                                [100%]

=============================== warnings summary ===============================
util/test/test_query.py:38
  /home/runner/work/df-py/df-py/util/test/test_query.py:38: PytestUnknownMarkWarning: Unknown pytest.mark.timeout - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.timeout(300)

util/test/test_oceanutil.py: 13 warnings
util/test/test_query.py: 75 warnings
util/test/veOcean/test_allocate_dt.py: 2 warnings
  /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/eth_abi/codec.py:191: DeprecationWarning: abi.decode_abi() is deprecated and will be removed in version 4.0.0 in favor of abi.decode()
    warnings.warn(

util/test/test_oceanutil.py: 20 warnings
util/test/test_query.py: 125 warnings
util/test/veOcean/test_allocate_dt.py: 4 warnings
  /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/eth_abi/codec.py:164: DeprecationWarning: abi.decode_single() is deprecated and will be removed in version 4.0.0 in favor of abi.decode()
    warnings.warn(

util/test/test_oceanutil.py::test_createDataNFT
  /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/brownie/network/event.py:490: UserWarning: 0x832205068355eEc6Bb8a19117d9c249b689D7d07: Event log does not contain enough topics for the given ABI - this is usually because an event argument is not marked as indexed
    warnings.warn(f"{address}: {exc}")

...

util/test/test_query.py::test_all
  /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/brownie/network/event.py:490: UserWarning: 0xE3ecFF59534cEd6aBBdE77A019f3d79E149A4480: Event log does not contain enough topics for the given ABI - this is usually because an event argument is not marked as indexed
    warnings.warn(f"{address}: {exc}")

-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========== 263 passed, 4 skipped, 247 warnings in 483.93s (0:08:03) ===========
Error: Process completed with exit code 1.

Metadata

Metadata

Assignees

Labels

Type: BugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions