Skip to content

Wrong and confusing type annotations #318

@XiaoHuiHui233

Description

@XiaoHuiHui233

The wrong type annotation was used in the uniswap.uniswap.Uniswap class. The parameters provider, web3, factory_contract_addr and router_contract_addr of the constructor default to None but are not declared as Optional. This is very unfriendly to static type checkers (such as Pyright with strict type checking enabled). And it will generate a lot of type error prompts, but actually no problem.

class Uniswap:
    """
    Wrapper around Uniswap contracts.
    """
    ...

    def __init__(
        self,
        address: Union[AddressLike, str, None],
        private_key: Optional[str],
        provider: str = None,
        web3: Web3 = None,
        version: int = 1,
        default_slippage: float = 0.01,
        use_estimate_gas: bool = True,
        # use_eip1559: bool = True,
        factory_contract_addr: str = None,
        router_contract_addr: str = None,
        enable_caching: bool = False,
    ) -> None:
        ...

At the same time, the type annotation format of address and private_key is confusing. In Python3.9 and below, the recommended declaration method is Optional[T] to indicate that the parameter can be None. And in 3.10+, its recommended to use T | None(also Union[T, None]). But here we can see the two different formats of type annotations are being used at the same time, which is confusing.

I suggest changing it to the following:

class Uniswap:
    """
    Wrapper around Uniswap contracts.
    """
    ...

    def __init__(
        self,
        address: Optional[Union[AddressLike, str]],
        private_key: Optional[str],
        provider: Optional[str] = None,
        web3: Optional[Web3] = None,
        version: int = 1,
        default_slippage: float = 0.01,
        use_estimate_gas: bool = True,
        # use_eip1559: bool = True,
        factory_contract_addr: Optional[str] = None,
        router_contract_addr: Optional[str] = None,
        enable_caching: bool = False,
    ) -> None:
        ...

Also, I noticed that some function parameters/return values also have such annotations. Returns possible None but no Optional is declared. This will confuse the user for wasting more time debugging the type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy