Skip to content

Python SDK type hints should support new TypedDict classes #91

@ndmeiri

Description

@ndmeiri

A couple TypedDict classes were added to the Python SDK recently in c656de5. Namely:

  • MarketFetchParams
  • EventFetchParams

We should update the signatures of related methods to support these new classes. (Which will also prevent static analyzers from complaining when the appropriate TypedDict is passed to these methods.)

Consider the current signature of Exchange.fetch_market:

def fetch_market(self, params: Optional[dict] = None, **kwargs) -> UnifiedMarket: ...

There are several potential solutions; I outline a few below.

  1. We could change the signature to

    def fetch_market(self, params: Mapping | None = None, **kwargs) -> UnifiedMarket: ...

    which would allow arguments of type dict, Mapping, or TypedDict (and, of course, NoneType).

  2. Although Mapping is probably too permissive. I.e., we might as well force clients to use MarketFetchParams.

    def fetch_market(self, params: MarketFetchParams | None = None, **kwargs) -> UnifiedMarket: ...

    But removing arbitrary dict support would break backward compatibility.

  3. Or if we want to keep the legacy Optional[] syntax for now:

    def fetch_market(self, params: Optional[Mapping] = None, **kwargs) -> UnifiedMarket: ...
  4. If we need to keep dict in the signature for forward compatibility, then we could update the signature to

    def fetch_market(self, params: Optional[Union[MarketFetchParams, dict] = None, **kwargs) -> UnifiedMarket: ...
  5. For the above options, handling **kwargs will require a bit of finesse in the implementation. We could alternatively consolidate params and **kwargs into a single parameter.

    def fetch_market(self, **kwargs: Unpack[MarketFetchParams]) -> UnifiedMarket: ...

    but this would require bumping the minimum Python version from 3.8 to 3.11, which I acknowledge is quite a large jump. But noting this solution anyway to keep in mind for the future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions