Skip to content

mypy/build: Use _load_json_file in load_tree #11575

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

Merged

Conversation

woodruffw
Copy link
Contributor

Description

This is a variant of #6156, albeit not the same exact failure site.

In one of my professional projects, we use mypy to lint a relatively complex Python codebase. For reasons that haven't been fully root-caused, we occasionally see JSON corruption errors that look like this:

Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/usr/local/lib/python3.8/dist-packages/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 87, in main
  File "mypy/main.py", line 165, in run_build
  File "mypy/build.py", line 179, in build
  File "mypy/build.py", line 254, in _build
  File "mypy/build.py", line 2697, in dispatch
  File "mypy/build.py", line 3014, in process_graph
  File "mypy/build.py", line 3089, in process_fresh_modules
  File "mypy/build.py", line 1975, in load_tree
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 5069 (char 5068)

That's with the latest MyPy (0.910), Python 3.8, Ubuntu 20.04. Note that the error here happens in load_tree and not _load_json_file.

We're not 100% sure what causes this error, but we do know how to make the error more comprehensible: by reusing the error handling functionality that's already been added to _load_json_file with #6156. So, that's all this PR does: it reuses that utility function to turn the uncontrolled exception into something more recognizable as a MyPy cache error.

Test Plan

I haven't made any test changes, but I've confirmed that the test suite passes locally with the functional changes that I've applied:

source venv/bin/activate
python3 runtests.py

@woodruffw
Copy link
Contributor Author

Anything else I can do to help this fix move forwards? 🙂

@JelleZijlstra JelleZijlstra merged commit e557ec2 into python:master Nov 28, 2021
@JelleZijlstra
Copy link
Member

Thanks, sorry for dropping this!

@woodruffw woodruffw deleted the ww/reasonable-json-decode-error branch November 28, 2021 03:48
@woodruffw
Copy link
Contributor Author

No problem at all! Thanks a ton for the merge 🙂

tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants