question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexError raised when passing an element of brownie.accounts as the exclude argument to brownie.test.strategy

See original GitHub issue

Environment information

  • brownie Version: 1.12.3, 1.12.2, 1.12.1
  • ganache-cli Version: 6.12.1
  • solc Version: 0.8.0+commit.c7dfd78e.Linux.g++
  • Python Version: 3.8.3, 3.9.1
  • OS: linux

What was wrong?

Command: brownie test Code that caused failure:

from brownie import accounts
from brownie.test import given, strategy

@given(
    amount=strategy("uint256", max_value="1000 ether"),
    to=strategy("address", exclude=accounts[0]),
)
def test_transfer_success_returns_true(token, accounts, amount, to):
    assert token.transfer(to, amount, {"from": accounts[0]}) == True

Error output:

$ brownie test tests/test_transfer.py
Brownie v1.12.3 - Python development framework for Ethereum

=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.8.3, pytest-6.0.1, py-1.10.0, pluggy-0.13.1
rootdir: /home/skelletor/projects/CloudToken
plugins: eth-brownie-1.12.3, hypothesis-5.41.3, forked-1.3.0, web3-5.11.1, xdist-1.34.0
collected 0 items / 1 error                                                                                                                                                                                       

===================================================================================================== ERRORS ======================================================================================================
_____________________________________________________________________________________ ERROR collecting tests/test_transfer.py _____________________________________________________________________________________
tests/test_transfer.py:8: in <module>
    to=strategy("address", exclude=accounts[0]),
E   IndexError: list index out of range
============================================================================================= short test summary info =============================================================================================
FAILED tests/test_transfer.py - IndexError: list index out of range
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================================ 1 error in 0.15s =================================================================================================

Extra Information

  • I was following @iamdefinitelyahuman’s In-Depth Guide to Testing Ethereum Smart Contracts Part Six, which was published on July 23, 2020. Since the test in the guide and mine are essentially the same, and assuming that the code in the guide worked when the content was published, then the bug was introduced between July 23, 2020 and now.
  • I’ve successfully recreated the error above with the v1.12.x releases.
  • Using slice notation (accounts[:1]), the IndexError doesn’t get raised, however, the element accounts[0] is included in the sampling. This makes sense, because a slice of an empty list, will return an empty list, which is essentially excluding nothing.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
iamdefinitelyahumancommented, Mar 16, 2021

Took a look at this… As you said, the problem is that test collection happens prior to connecting to the network, so accounts is empty.

Connecting to the network is handled in pytest_collection_finish, in brownie/test/runner.py, line 258. This is a deliberate design choice for 2 reasons:

  1. If no tests were collected, or there is an error during collection, we don’t have to wait for ganache-cli to launch and be killed.
  2. It allows for individual test suites to implement their own hook point and connect to a different network (example)

There is no easy solution I can see here unfortunately, as the call to strategy happens immediatly upon importing the test module. A hacky solution might be to allow Accounts to return a sort-of proxy “promise” object that represents the eventual Account that will exist at index 0 once the network has connect. The first attempt to inspect this object would then mutate it into the actual account object, or raise if the index is out of bounds.

0reactions
sabotagebeatscommented, Mar 16, 2021

I was hoping to find a solution which would wrap this for the user so that they don’t have to worry about it, however it seems that it’s functioning as designed and so this won’t be feasible? If this is the case, alternatively could we just modify the documentation for the address strategy in order to notate that it requires loading the accounts object first?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property-Based Testing — Brownie 1.19.2 documentation
In the following example, we add a to argument using an address strategy. from brownie import accounts from brownie.test import given ...
Read more >
eth-brownie/community - Gitter
what I ment to ask, how do I define accounts at the start for all fixtures? deployer = accounts[0] E IndexError: list index...
Read more >
Brownie test IndexError: list index out of range
When connecting to a remote network via a hosted node such as Infura, the Accounts container will be empty. Before you can perform...
Read more >
Brownie Documentation
Brownie is a Python framework for Ethereum smart contract testing, ... an argument with the same name to the inputs of your test...
Read more >
Programming Style - GitHub Pages
PEP8: a style guide for Python that discusses topics such as how you should name variables, how you should use indentation in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found