Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too many positional arguments #139

Open
wants to merge 9 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist
_version.py
.idea/
venv/
.venv/
tmp/
.vscode/
build/
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[MASTER]
disable=fixme,logging-fstring-interpolation,too-many-positional-arguments
disable=fixme,logging-fstring-interpolation
[DESIGN]
max-args=10
36 changes: 20 additions & 16 deletions dune_client/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,44 +89,48 @@ def default_headers(self) -> Dict[str, str]:

def _build_parameters(
self,
params: Optional[Dict[str, Union[str, int]]] = None,
columns: Optional[List[str]] = None,
sample_count: Optional[int] = None,
filters: Optional[str] = None,
sort_by: Optional[List[str]] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
allow_partial_results: str = "true",
params: Optional[Dict[str, Any]] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good way to start here would be, create the proposed data class and put it here as optional.

See how the function needs to be changed because of the replacement...

Actually, I just noticed this is _build_parameters... This will likely be replaces with a method like to_dict on the class itself. The data class can implement both to_dict and from_dict (this might come for free with the @DataClass decorator although I can't remember).

Nit: It looks like you forgot to include allow_partial_results here, but I really think this _build_parameters function will no longer be necessary once this class is in place.

) -> Dict[str, Union[str, int]]:
#TODO: Change the function class for this function
"""
Utility function that builds a dictionary of parameters to be used
when retrieving advanced results (filters, pagination, sorting, etc.).
This is shared between the sync and async client.
"""
# Ensure we don't specify parameters that are incompatible:
if params is None:
params = {}
parameters = params.get("params", None)
columns = params.get("columns", None)
sample_count = params.get("sample_count", None)
filters = params.get("filters", None)
sort_by = params.get("sort_by", None)
limit = params.get("limit", None)
offset = params.get("offset", None)
assert (
# We are not sampling
sample_count is None
# We are sampling and don't use filters or pagination
or (limit is None and offset is None and filters is None)
), "sampling cannot be combined with filters or pagination"

params = params or {}
params["allow_partial_results"] = allow_partial_results
parameters = parameters or {}
parameters["allow_partial_results"] = allow_partial_results
if columns is not None and len(columns) > 0:
params["columns"] = ",".join(columns)
parameters["columns"] = ",".join(columns)
if sample_count is not None:
params["sample_count"] = sample_count
parameters["sample_count"] = sample_count
if filters is not None:
params["filters"] = filters
parameters["filters"] = filters
if sort_by is not None and len(sort_by) > 0:
params["sort_by"] = ",".join(sort_by)
parameters["sort_by"] = ",".join(sort_by)
if limit is not None:
params["limit"] = limit
parameters["limit"] = limit
if offset is not None:
params["offset"] = offset
parameters["offset"] = offset

return params
return parameters


class BaseRouter(BaseDuneClient):
Expand Down
22 changes: 13 additions & 9 deletions dune_client/api/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from __future__ import annotations
from typing import List, Optional
from typing import Dict, Optional, Any

from dune_client.api.base import BaseRouter
from dune_client.models import (
Expand All @@ -25,12 +25,7 @@ def get_custom_endpoint_result(
self,
handle: str,
endpoint: str,
limit: Optional[int] = None,
offset: Optional[int] = None,
columns: Optional[List[str]] = None,
sample_count: Optional[int] = None,
filters: Optional[str] = None,
sort_by: Optional[List[str]] = None,
params: Optional[Dict[str, Any]] = None,
) -> ResultsResponse:
"""
Custom endpoints allow you to fetch and filter data from any
Expand All @@ -48,7 +43,16 @@ def get_custom_endpoint_result(
filters (str, optional): The filters to apply.
sort_by (List[str], optional): The columns to sort by.
"""
params = self._build_parameters(
if params is None:
params = {}
limit = params.get("limit", None)
offset = params.get("offset", None)
columns = params.get("columns", None)
sample_count = params.get("sample_counts", None)
filters = params.get("filters", None)
sort_by = params.get("sort_by", None)

build_params = self._build_parameters(
columns=columns,
sample_count=sample_count,
filters=filters,
Expand All @@ -58,7 +62,7 @@ def get_custom_endpoint_result(
)
response_json = self._get(
route=f"/endpoints/{handle}/{endpoint}/results",
params=params,
params=build_params,
)
try:
return ResultsResponse.from_dict(response_json)
Expand Down
44 changes: 26 additions & 18 deletions dune_client/api/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from io import BytesIO
from typing import Any, Dict, List, Optional
from typing import Any, Dict, Optional

from deprecated import deprecated

Expand Down Expand Up @@ -75,16 +75,20 @@ def get_execution_status(self, job_id: str) -> ExecutionStatusResponse:
def get_execution_results(
self,
job_id: str,
limit: Optional[int] = None,
offset: Optional[int] = None,
columns: Optional[List[str]] = None,
sample_count: Optional[int] = None,
filters: Optional[str] = None,
sort_by: Optional[List[str]] = None,
allow_partial_results: str = "true",
params: Optional[Dict[str, Any]] = None,
) -> ResultsResponse:
"""GET results from Dune API for `job_id` (aka `execution_id`)"""
params = self._build_parameters(
if params is None:
params = {}
limit = params.get("limit", None)
offset = params.get("offset", None)
columns = params.get("columns", None)
sample_count = params.get("sample_counts", None)
filters = params.get("filters", None)
sort_by = params.get("sort_by", None)
allow_partial_results = params.get("allow_partial_results", None)

build_params = self._build_parameters(
columns=columns,
sample_count=sample_count,
filters=filters,
Expand All @@ -96,17 +100,12 @@ def get_execution_results(

route = f"/execution/{job_id}/results"
url = self._route_url(route)
return self._get_execution_results_by_url(url=url, params=params)
return self._get_execution_results_by_url(url=url, params=build_params)

def get_execution_results_csv(
self,
job_id: str,
limit: Optional[int] = None,
offset: Optional[int] = None,
columns: Optional[List[str]] = None,
filters: Optional[str] = None,
sample_count: Optional[int] = None,
sort_by: Optional[List[str]] = None,
params: Optional[Dict[str, Any]] = None,
) -> ExecutionResultCSV:
"""
GET results in CSV format from Dune API for `job_id` (aka `execution_id`)
Expand All @@ -115,7 +114,16 @@ def get_execution_results_csv(
use this method for large results where you want lower CPU and memory overhead
if you need metadata information use get_results() or get_status()
"""
params = self._build_parameters(
if params is None:
params = {}
limit = params.get("limit", None)
offset = params.get("offset", None)
columns = params.get("columns", None)
sample_count = params.get("sample_counts", None)
filters = params.get("filters", None)
sort_by = params.get("sort_by", None)

build_params = self._build_parameters(
columns=columns,
sample_count=sample_count,
filters=filters,
Expand All @@ -126,7 +134,7 @@ def get_execution_results_csv(

route = f"/execution/{job_id}/results/csv"
url = self._route_url(route)
return self._get_execution_results_csv_by_url(url=url, params=params)
return self._get_execution_results_csv_by_url(url=url, params=build_params)

def _get_execution_results_by_url(
self, url: str, params: Optional[Dict[str, Any]] = None
Expand Down
Loading
Loading