Skip to content

Instantly share code, notes, and snippets.

@anand2312
Last active September 18, 2023 17:22
Show Gist options
  • Save anand2312/93d3abf401335fd3310d9e30112303bf to your computer and use it in GitHub Desktop.
Save anand2312/93d3abf401335fd3310d9e30112303bf to your computer and use it in GitHub Desktop.
Why RPCFilterRequestBuilder?

Problem:

  • APIResponse and SingleAPIResponse are declared as
class APIResponse(BaseModel, Generic[T]):
  data: list[T]
  ...
class SingleAPIResponse(BaseModel, Generic[T]):
  data: T
  ...
  • Any query method -> FilterBuilder[dict[str, Any]] -> ... -> APIResponse[dict[str, Any]] i.e data would be of type list[dict[str, Any]].
  • BUT rpc is declared as
def rpc(...) -> FilterBuilder[Any]: ...

which eventually returns an APIResponse[Any] - i.e data would be of type list[Any] - but it should be just Any. Pydantic sees that the response isn't a list[Any] and raises a validation error.

Solution:

  • An RPCFilterRequestBuilder[T] that is exactly the same as the usual filter builder, but it eventually returns a SingleAPIResponse[T] instead

That is: rpc() -> RPCFilterRequestBuilder[Any] -> ... -> SingleAPIResponse[Any] i.e data is of type Any

Hope that explains the rationale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment