Skip to content

Commit

Permalink
Use format=None to return raw Python data structure (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jan 22, 2025
2 parents 9bee5f8 + c354641 commit a23b0c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/pypistats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _clear_cache() -> None:
def pypi_stats_api(
endpoint: str,
params: str | None = None,
format: str = "pretty",
format: str | None = "pretty",
start_date: str | None = None,
end_date: str | None = None,
sort: bool = True,
Expand Down Expand Up @@ -173,6 +173,9 @@ def pypi_stats_api(
data = _percent(data)
data = _grand_total(data)

if format is None:
return data

if color != "no" and format in ("markdown", "pretty", "rst", "tsv"):
data = _colourify(data)

Expand Down
19 changes: 19 additions & 0 deletions tests/test_pypistats.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,25 @@ def test_format_pandas(self) -> None:
assert isinstance(output, pandas.DataFrame)
assert str(output).strip() == expected_output.strip()

@respx.mock
def test_format_none(self) -> None:
# Arrange
package = "pip"
mocked_url = "https://pypistats.org/api/packages/pip/overall"
mocked_response = SAMPLE_RESPONSE_OVERALL
expected_output = {
"category": "with_mirrors",
"downloads": 3587357,
"percent": "100.00%",
}

# Act
respx.get(mocked_url).respond(content=mocked_response)
output = pypistats.overall(package, format=None)

# Assert
assert output[0] == expected_output

@respx.mock
def test_package_not_exist(self) -> None:
# Arrange
Expand Down

0 comments on commit a23b0c6

Please sign in to comment.