Skip to content

Replace bare except clauses with specific exception handling#4986

Open
jaideepj2004 wants to merge 1 commit into
Cloud-CV:masterfrom
jaideepj2004:fix-bare-except-clauses-4890
Open

Replace bare except clauses with specific exception handling#4986
jaideepj2004 wants to merge 1 commit into
Cloud-CV:masterfrom
jaideepj2004:fix-bare-except-clauses-4890

Conversation

@jaideepj2004

Copy link
Copy Markdown
Contributor

Description

This PR addresses issue #4890 by replacing bare except clauses with specific exception handling in apps/analytics/views.py.

Changes Made

  • ✅ Added logging import at the top of the file
  • ✅ Replaced bare except: clauses with except (TypeError, ValueError, AttributeError) as e:
  • ✅ Added error logging for better debuggability when serialization errors occur
  • ✅ Removed # noqa: E722 suppression comments

Why These Changes?

Bare except clauses are considered a bad practice in Python because they:

  • Hide unexpected errors and bugs
  • Make debugging more difficult
  • Catch exceptions that shouldn't be silently handled (e.g., KeyboardInterrupt, SystemExit)

Specific Exceptions Caught

The exceptions chosen (TypeError, ValueError, AttributeError) are appropriate for serialization errors:

  • TypeError: Wrong type passed to serializer
  • ValueError: Invalid value for a field
  • AttributeError: Missing attribute on an object

Testing Performed

Syntax validation: python -m py_compile apps/analytics/views.py - passed
Code quality: flake8 apps/analytics/views.py - no errors
Exception handling: Verified all three exception types are correctly caught
No remaining bare excepts: Confirmed no other bare except clauses remain in file

Benefits

  • Follows Python best practices (PEP 8)
  • Improves debuggability by logging specific exceptions
  • Makes error handling more explicit and maintainable
  • Removes linter suppressions

Files Affected

  • apps/analytics/views.py (2 locations updated)

Related Issue

Fixes #4890


Note: This is my third contribution to EvalAI. My previous PRs (#4977, #4978) were documentation improvements that were successfully merged. Looking forward to contributing more! 🚀

- Replace bare except clauses in apps/analytics/views.py with specific exception handling (TypeError, ValueError, AttributeError)
- Add logging for better debuggability when serialization errors occur
- Remove noqa: E722 suppression comments
- Follows Python best practices (PEP 8)

Fixes Cloud-CV#4890
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.

Replace Bare except Clauses with Specific Exception Handling in Analytics Views

1 participant