Remove Python 2.7 legacy: Update JSONDecodeError exception handling
🙂 Looking for an issue? Welcome! This issue is open for contribution. If this is the first time you’re requesting an issue, please:
- Read Contributing guidelines carefully. Pay extra attention to Using generative AI. Pull requests and comments that don’t follow the guidelines won’t be answered.
- Confirm that you’ve read the guidelines in your comment.
Overview
Python 2 didn't have json.JSONDecodeError - instead, JSON parsing errors raised ValueError. Python 3 introduced the more specific json.JSONDecodeError exception. Code catching ValueError for JSON parsing can now use the more precise exception type.
Difficulty: Beginner-friendly
Risk: Low
Estimated changes: ~1-2 instances
The Change
Python 2 compatible pattern (current):
import json
try:
data = json.loads(json_string)
except ValueError as e: # Use ValueError rather than JSONDecodeError for Py2 compatibility
# handle JSON parsing error
Python 3 pattern (target):
import json
try:
data = json.loads(json_string)
except json.JSONDecodeError as e:
# handle JSON parsing error
Known Instance
File: kolibri/core/device/management/commands/provisiondevice.py (line 39)
Current:
except ValueError as e: # Use ValueError rather than JSONDecodeError for Py2 compatibility
Action:
- Change
ValueErrortojson.JSONDecodeError - Remove the Python 2 compatibility comment
- Ensure
jsonis imported
Benefits of This Change
- More specific exception handling:
json.JSONDecodeErroronly catches JSON parsing errors, not all ValueErrors - Better error messages:
json.JSONDecodeErrorprovides better error details (line, column, position) - Clearer intent: Code explicitly shows it's handling JSON parsing failures
Tasks
1. Find all instances
Search for JSON parsing with ValueError:
grep -r "json.loads" kolibri/ --include="*.py" -A 2 | grep -i valueerror
grep -r "json.load(" kolibri/ --include="*.py" -A 2 | grep -i valueerror
Look for comments mentioning JSONDecodeError:
grep -r "JSONDecodeError" kolibri/ --include="*.py"
2. Verify each instance
For each ValueError exception near JSON parsing:
- Confirm it's for JSON parsing: Check if it's in a try block with
json.loads()orjson.load() - Check for other ValueErrors: Make sure the except block isn't also catching other ValueError instances
- Verify import: Ensure
jsonis imported in the file
3. Update each instance
For JSON parsing error handling:
- Change
except ValueErrortoexcept json.JSONDecodeError - Remove Python 2 compatibility comments
- Keep the same error handling logic
Important: Only change ValueError to JSONDecodeError if it's specifically catching JSON parsing errors. Don't change ValueError exceptions used for other validation purposes!
Acceptance Criteria
- [ ] All JSON parsing error handlers updated from
ValueErrortojson.JSONDecodeError - [ ] Python 2 compatibility comments removed
- [ ] No non-JSON ValueError exceptions changed
- [ ] All tests pass
Testing
Run all tests:
pytest
Run device management tests:
pytest kolibri/core/device/test/
Run management command tests:
pytest kolibri/core/device/management/
Test the provisiondevice command specifically:
pytest kolibri/core/device/test/ -k provision
Find and run other relevant tests:
# Find test files for modules you modify
pytest kolibri/path/to/module/test/test_file.py
Tips
- Be selective: Only change ValueError to JSONDecodeError for JSON parsing
- Check context: Read the surrounding code to understand what the exception is for
- Test with invalid JSON: If possible, test the code path with malformed JSON to verify the exception handling works
- Look at error messages: Make sure error handling logic makes sense with the new exception type
Example
Before:
import json
def load_config(config_str):
try:
config = json.loads(config_str)
except ValueError as e: # Use ValueError rather than JSONDecodeError for Py2 compatibility
logger.error(f"Invalid JSON: {e}")
return None
return config
After:
import json
def load_config(config_str):
try:
config = json.loads(config_str)
except json.JSONDecodeError as e:
logger.error(f"Invalid JSON: {e}")
return None
return config
Questions?
Comment on this issue if you need help or clarification!
Disclosure
🤖 This issue was written by Claude Code, under supervision, review and final edits by @rtibbles 🤖
hey @rtibbles @MisRob can i work to solve this issue?My kolibri setup is complete and would love to explore if its fine. :)
Hi! 👋
Thanks so much for your interest! This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.
We really appreciate your willingness to help—you're welcome to find a more suitable issue, and let us know if you have any questions. 😊
Hi @Prashant-thakur77, this project is not yet generally available. There are some free 'help wanted' so meanwhile I recommend those.
If we open it for more contributors in the future, then I am happy to let you know. Thanks!
Thanks a lot for the clarification @MisRob! No worries , I’ll look into the help wanted issues meanwhile.
@Prashant-thakur77 so we chatted with @rtibbles and we won't yet open these issues generally as 'help wanted', but we can have one more person join the project - and as you have a nice previous experience in our codebases, we're happy to have you on this. If you're still interested, let us know.
@MisRob Thanks for the update! Yes, I’m definitely still interested in contributing further. I’d be happy to join the project and help wherever needed. :)
Thank you @Prashant-thakur77, assigned