bybit-exchange / pybit

Official Python3 API connector for Bybit's HTTP and WebSockets APIs.
Other
339 stars 113 forks source link

Non-standard type-hinting thoughout the library. #208

Open thisismygitrepo opened 2 months ago

thisismygitrepo commented 2 months ago

My linters and type checkers are failing to provide any help due to non-standard way of type hinting used in the library, For instance, in the file: unified_trading:

    def kline_stream(self, interval: int, symbol: (str, list), callback):
        """Subscribe to the klines stream."""

        ...

The proper way of enshrining the logic that symbol can be either str OR list[str] is:

from typing import Union, Callable
def kline_stream(self, interval: int, symbol: Union[str, list[str]], callaback: Callable[[str], None]):
    ...

Once fixed, type checker will be able to statically check all the code (including the library itself) for any inconsistency. Can you guys please use the standards?

Thank you,

dextertd commented 2 months ago

Are you referring to GUI hints in an IDE? I tried making this change but it doesn't make any difference in pycharm. Can you give me a way to test this?

thisismygitrepo commented 2 months ago

Yes, I am referring to IDE.

It is weird that Pycharm makes no difference.

image

I use VS-Code with official pylance, mypy linter and type checker and I set them on hashest mode, so they yell at me for all tiny mistakes.

As can be seen in the image, becaues of non-standard type hinting, they fail to parse it and they think the type is Unknown, therefore they are unable to yell at me no matter what I pass as argument, because they don't know what should be passed to begin with.

You mentioned that it makes no diffrence, but you should get squiggly underline hiting from the editor saying you violated the types. At the moment, the callable argument is not even type-hintend, so, no matter what function you pass, the IDE is happy, until you run the code and you get error, but we want to get all the errors while we type, not at runtime (too late, too slow). In my example, the type-hint declares what signature the callable should have, so if you passed anything else, the IDE will underline it and declares it as error (in a static way before you run the code).

dextertd commented 2 months ago

Ah, I can see a similar prompt when hovering over the function name and it is more detailed when using typing. Will investigate implementing this