ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
5k stars 1.7k forks source link

Not working with Phyton 3.11 #3110

Closed marcelkb closed 10 months ago

marcelkb commented 1 year ago

What was wrong?

Code which works on Phyton 3.10 on Polygon and Etherum dont work with Phyton 3.11. get_transaction_count fails.

from web3 import Web3
from web3.gas_strategies.time_based import slow_gas_price_strategy

# Configure Web3 to connect to the Polygon
rpc_url = "https://polygon-mainnet.infura.io/v3/{Key}"
account = "0x..."

web3 = Web3(Web3.HTTPProvider(rpc_url))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)

if __name__ == "__main__":
    latest_block = web3.eth.get_block('latest') #works

    print(f"chainId: {web3.eth.chain_id}") #works

    print(f"The latest block number is:{web3.eth.block_number}") #works

    res = web3.is_connected()
    print(f"connected: {res}") #works

    is_address_valid = web3.is_address(account)
    print(f"valid address {account}: {res}") #works

    nonce = web3.eth.get_transaction_count(web3.to_checksum_address(account)) #fails
    print(f"nonce {account}")`
File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\web3\eth\eth.py", line 451, in get_transaction_count
    return self._get_transaction_count(account, block_identifier)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\web3\module.py", line 64, in caller
    (method_str, params), response_formatters = method.process_params(
                                                ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\web3\method.py", line 233, in process_params
    _apply_request_formatters(params, self.request_formatters(method)),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\eth_utils\functional.py", line 45, in inner
    return callback(fn(*args, **kwargs))
                    ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\web3\method.py", line 55, in _apply_request_formatters
    formatted_params = pipe(params, request_formatters)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "cytoolz\\functoolz.pyx", line 680, in cytoolz.functoolz.pipe
  File "cytoolz\\functoolz.pyx", line 655, in cytoolz.functoolz.c_pipe
  File "cytoolz\\functoolz.pyx", line 518, in cytoolz.functoolz.Compose.__call__
  File "cytoolz\\functoolz.pyx", line 263, in cytoolz.functoolz.curry.__call__
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\web3\_utils\abi.py", line 746, in map_abi_data
    return pipe(data, *pipeline)
           ^^^^^^^^^^^^^^^^^^^^^
  File "cytoolz\\functoolz.pyx", line 680, in cytoolz.functoolz.pipe
  File "cytoolz\\functoolz.pyx", line 655, in cytoolz.functoolz.c_pipe
  File "cytoolz\\functoolz.pyx", line 263, in cytoolz.functoolz.curry.__call__
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\web3\_utils\abi.py", line 760, in abi_data_tree
    return [
           ^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\web3\_utils\abi.py", line 761, in <listcomp>
    abi_sub_tree(data_type, data_value)
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\web3\_utils\abi.py", line 814, in abi_sub_tree
    abi_type = parse(type_str_or_abi_type)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\eth_abi\grammar.py", line 123, in parse
    return super().parse(type_str, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\parsimonious\nodes.py", line 252, in parse
    return self._parse_or_match(text, pos, 'parse')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\parsimonious\nodes.py", line 287, in _parse_or_match
    return self.visit(getattr(self.grammar, method_name)(text, pos=pos))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\parsimonious\nodes.py", line 213, in visit
    return method(node, [self.visit(n) for n in node])
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\parsimonious\nodes.py", line 213, in <listcomp>
    return method(node, [self.visit(n) for n in node])
                         ^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\parsimonious\nodes.py", line 213, in visit
    return method(node, [self.visit(n) for n in node])
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\parsimonious\nodes.py", line 213, in <listcomp>
    return method(node, [self.visit(n) for n in node])
                         ^^^^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\Project\venv_311\Lib\site-packages\parsimonious\nodes.py", line 225, in visit
    raise VisitationError(exc, exc_class, node) from exc
parsimonious.exceptions.VisitationError: TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
fselmo commented 1 year ago

@marcelkb what's your eth-abi version? Can you run pip install --upgrade eth-abi and try again?

marcelkb commented 1 year ago

@fselmo eth-abi is 4.1.0 testet it with upgrade to 4.2.1 still same error

fselmo commented 1 year ago

Interesting. eth-abi requires a pin for parsimonious below version 0.10.0. I believe this is the version that includes some breaking changes. Perhaps one of the other libraries in your current python packages requires this higher parsimonious version but it won't work with the web3 library suite.

I'd suggest first checking that parsimonious is below version 0.10.0, meaning the highest it can be is 0.9.0. I was able to reproduce this error with the latest parsimonious version, for example.

Secondly, I'd suggest using a fresh virtual environment for your python web3 project and pip install web3 cleanly there.

pip install parsimonious==0.9.0 should resolve this with your current setup though.

fselmo commented 10 months ago

Closing this as stale. web3.py has been working with python 3.11 for quite some time now. If you are still having issues and believe that it is an issue with the library, please define them more clearly and open another github issue for it. Best of luck.