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

Fix index out of range error when data not found #438

Merged
merged 14 commits into from
May 31, 2024

Conversation

Yao-Wen-Chang
Copy link
Contributor

close #437

@Yao-Wen-Chang Yao-Wen-Chang force-pushed the fix-index-out-of-range branch from b4f1176 to bf43ebc Compare May 30, 2024 06:51
@hugovk hugovk added the changelog: Fixed For any bug fixes label May 30, 2024
@hugovk hugovk changed the title fix index out of range error in __init__.py Fix index out of range error when package not found May 30, 2024
@hugovk
Copy link
Owner

hugovk commented May 30, 2024

Running against a project which doesn't exist on PyPI and getting results in JSON format:

pypistats python_major testbrojct -f json
{"data": [], "package": "testbrojct", "type": "python_major_downloads"}

However, we still get the error when asking for results in other formats:

❯ pypistats python_major testbrojct
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.12/bin/pypistats", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/hugo/github/pypistats/src/pypistats/cli.py", line 372, in main
    args.func(args)
  File "/Users/hugo/github/pypistats/src/pypistats/cli.py", line 258, in python_major
    pypistats.python_major(
  File "/Users/hugo/github/pypistats/src/pypistats/__init__.py", line 497, in python_major
    return pypi_stats_api(endpoint, params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hugo/github/pypistats/src/pypistats/__init__.py", line 169, in pypi_stats_api
    data = _percent(data)
           ^^^^^^^^^^^^^^
  File "/Users/hugo/github/pypistats/src/pypistats/__init__.py", line 322, in _percent
    grand_total = _grand_total_value(data)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hugo/github/pypistats/src/pypistats/__init__.py", line 282, in _grand_total_value
    if data[0]["category"] in ["with_mirrors", "without_mirrors"]:
       ~~~~^^^
IndexError: list index out of range
❯ pypistats python_major testbrojct -f html
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.12/bin/pypistats", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/hugo/github/pypistats/src/pypistats/cli.py", line 372, in main
    args.func(args)
  File "/Users/hugo/github/pypistats/src/pypistats/cli.py", line 258, in python_major
    pypistats.python_major(
  File "/Users/hugo/github/pypistats/src/pypistats/__init__.py", line 497, in python_major
    return pypi_stats_api(endpoint, params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hugo/github/pypistats/src/pypistats/__init__.py", line 169, in pypi_stats_api
    data = _percent(data)
           ^^^^^^^^^^^^^^
  File "/Users/hugo/github/pypistats/src/pypistats/__init__.py", line 322, in _percent
    grand_total = _grand_total_value(data)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hugo/github/pypistats/src/pypistats/__init__.py", line 282, in _grand_total_value
    if data[0]["category"] in ["with_mirrors", "without_mirrors"]:
       ~~~~^^^
IndexError: list index out of range

Let's also handle these too. And please can you add some tests?

For example, see a43357d that added the KeyError handling with tests.

@Yao-Wen-Chang
Copy link
Contributor Author

I will fix those bugs and add the test.

@Yao-Wen-Chang
Copy link
Contributor Author

Yao-Wen-Chang commented May 30, 2024

Instead of handling with those errors, I prefer to check if res["data"] is an empty list at this line here.
If it's empty, the function returns the message: 'The package 'sssssssssssssssssssssssssss' does not exist.'

I will add the test to check the return message.

@Yao-Wen-Chang Yao-Wen-Chang force-pushed the fix-index-out-of-range branch from 329128c to f18dfe2 Compare May 30, 2024 13:28
@Yao-Wen-Chang Yao-Wen-Chang marked this pull request as draft May 31, 2024 06:35
@Yao-Wen-Chang Yao-Wen-Chang force-pushed the fix-index-out-of-range branch 2 times, most recently from 53916cd to 020d5e4 Compare May 31, 2024 07:07
@Yao-Wen-Chang Yao-Wen-Chang force-pushed the fix-index-out-of-range branch from fcffe12 to e478939 Compare May 31, 2024 07:11
@Yao-Wen-Chang Yao-Wen-Chang marked this pull request as ready for review May 31, 2024 07:20
@Yao-Wen-Chang
Copy link
Contributor Author

Do you know why using the
Command: pypistats python_major -j aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
it returns 200, but
Command: pypistats overall -j aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
it returns 404 from the API call, then raises the exception.

Reasonably, both should return 404, because the package does not exist.

@hugovk
Copy link
Owner

hugovk commented May 31, 2024

I don't know.

If we run with --verbose it shows the API endpoints:

As you say, the first returns 200 with empty data, the second is 404.

Turns out I already reported this upstream 3 years ago, but there's been no response so we'll have to live with it:

@Yao-Wen-Chang
Copy link
Contributor Author

For this line in the code:

r.raise_for_status()

Do you think it would be a good idea to delete it, considering it raises an error message?

This reverts commit c8bb92a.
@Yao-Wen-Chang Yao-Wen-Chang force-pushed the fix-index-out-of-range branch from 3bc113b to 46d8bcc Compare May 31, 2024 13:26
@hugovk hugovk changed the title Fix index out of range error when package not found Fix index out of range error when data not found May 31, 2024
@hugovk
Copy link
Owner

hugovk commented May 31, 2024

Thank you!

@hugovk hugovk merged commit 1f333f7 into hugovk:main May 31, 2024
47 checks passed
@Yao-Wen-Chang Yao-Wen-Chang deleted the fix-index-out-of-range branch June 1, 2024 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Fixed For any bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improper IndexError handling
2 participants