0xProject / 0x-monorepo

0x protocol monorepo - includes our smart contracts and many developer tools
Other
1.41k stars 464 forks source link

0x-contract-wrappers & 0x/abi-gen: TypeError: Cannot instantiate typing.Union #2740

Closed damjankuznar closed 3 years ago

damjankuznar commented 3 years ago

Expected Behavior

No TypeError should be raised when calling EncodeErc1155AssetDataMethod.call() or DevUtils.encode_erc1155_asset_data.call().

Current Behavior

Currently the following error occurs when calling the above mentioned method:

  File "/home/.../.../.../.../venv/lib/python3.7/site-packages/zero_ex/contract_wrappers/dev_utils/__init__.py", line 1530, in call
    return Union[bytes, str](returned)
  File "/usr/lib/python3.7/typing.py", line 678, in __call__
    result = self.__origin__(*args, **kwargs)
  File "/usr/lib/python3.7/typing.py", line 332, in __call__
    raise TypeError(f"Cannot instantiate {self!r}")
TypeError: Cannot instantiate typing.Union

The problems seems to be with @0x/abi-gen which does not generate a valid Python code, basically instantiating type hint class:

from typing import Union
# ...
class EncodeErc1155AssetDataMethod(ContractMethod):
    # ...
    def call(...):
        # ...
        return Union[bytes, str](returned)

Possible Solution

Steps to Reproduce (for bugs)

from web3 import Web3
from zero_ex.contract_addresses import chain_to_addresses, ChainId
from zero_ex.contract_wrappers.dev_utils import DevUtils
w3 = Web3.HTTPProvider(endpoint_uri="http://127.0.0.1:8545")
contract_addresses = chain_to_addresses(ChainId.MAINNET)
dev_utils = DevUtils(web3_or_provider=w3, contract_address=contract_addresses.dev_utils)
result = dev_utils.encode_erc1155_asset_data.call(
    "0xc1f4b0eea2bd6690930e6c66efd3e197d620b9c2", [2056], [1], "0x"
)
# this should result in the following error:
Traceback (most recent call last):
  File "/home/.../.../.../.../venv/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3319, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-e419739043d5>", line 8, in <module>
    "0xc1f4b0eea2bd6690930e6c66efd3e197d620b9c2", [2056], [1], "0x"
  File "/home/.../.../.../.../venv/lib/python3.7/site-packages/zero_ex/contract_wrappers/dev_utils/__init__.py", line 1530, in call
    return Union[bytes, str](returned)
  File "/usr/lib/python3.7/typing.py", line 678, in __call__
    result = self.__origin__(*args, **kwargs)
  File "/usr/lib/python3.7/typing.py", line 332, in __call__
    raise TypeError(f"Cannot instantiate {self!r}")
TypeError: Cannot instantiate typing.Union

Context

Construct the order data for listing

Your Environment

$ pip show 0x-contract-wrappers 
Name: 0x-contract-wrappers
Version: 2.0.0
Summary: Python wrappers for 0x smart contracts
Home-page: https://github.com/0xproject/0x-monorepo/tree/development/python-packages/contract_wrappers
Author: F. Eugene Aumson
Author-email: feuGeneA@users.noreply.github.com
License: Apache 2.0
Location: /home/.../.../.../.../venv/lib/python3.7/site-packages
Requires: 0x-order-utils, web3, attrs, 0x-contract-addresses, 0x-json-schemas, eth-utils, mypy-extensions, 0x-contract-artifacts
Required-by: 
stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 3 years ago

This issue has been automatically closed because no activity occured in 7 days after being marked as stale. If it's still relevant - feel free to reopen. Thank you for your contributions.