Skip to content

Fix UnboundLocalError in ClassroomNotificationsViewset.list when filter_queryset raises DatabaseError#14345

Merged
rtibbles merged 5 commits intolearningequality:developfrom
TheGreatPratyush:fix-classroom-notification-unboundlocalerror
Mar 9, 2026
Merged

Fix UnboundLocalError in ClassroomNotificationsViewset.list when filter_queryset raises DatabaseError#14345
rtibbles merged 5 commits intolearningequality:developfrom
TheGreatPratyush:fix-classroom-notification-unboundlocalerror

Conversation

@TheGreatPratyush
Copy link
Copy Markdown
Contributor

Summary

This PR fixes an issue in ClassroomNotificationsViewset.list where queryset can be referenced before it was assigned.
If filter_queryset() raises a DatabaseError or OperationalError,repair_sqlite_db() is triggered, but queryset is not reassigned before it is used later in the method, which results in an error :
UnboundLocalError: local variable 'queryset' referenced before assignment
This change ensures that queryset is always defined by assigning an empty queryset after the database repair:
queryset = self.get_queryset().none()

With this fix, the /coach/api/notifications/ endpoint no longer crashes
with a 500 error and instead returns a valid response.

References

Fixes #14322

Reviewer guidance

  1. Start Kolibri locally from source.
  2. Simulate a DatabaseError inside filter_queryset().
  3. Navigate to the Coach page.
  4. Verify that the /coach/api/notifications/ endpoint no longer raises
    UnboundLocalError.
  5. Confirm that the endpoint returns a valid response instead of a 500 error.

@github-actions github-actions Bot added DEV: backend Python, databases, networking, filesystem... APP: Coach Re: Coach App (lessons, quizzes, groups, reports, etc.) SIZE: very small labels Mar 7, 2026
@learning-equality-bot
Copy link
Copy Markdown

👋 Thanks for contributing!

We will assign a reviewer within the next two weeks. In the meantime, please ensure that:

  • You ran pre-commit locally
  • All issue requirements are satisfied
  • The contribution is aligned with our Contributing guidelines. Pay extra attention to Using generative AI. Pull requests that don't follow the guidelines will be closed.

We'll be in touch! 😊

Copy link
Copy Markdown
Member

@rtibbles rtibbles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked you to include a regression test in this, please include.

@TheGreatPratyush
Copy link
Copy Markdown
Contributor Author

I am Sorry for that thankyou for pointing out .I will add a regression test and update the PR .

@TheGreatPratyush
Copy link
Copy Markdown
Contributor Author

TheGreatPratyush commented Mar 7, 2026

I’ve added a regression test and updated the PR. Please review and suggest changes if necessary , thanks again for pointing out the mistake.



def test_database_error_does_not_crash(self):
from unittest.mock import patch
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why these imports are inlined like this - there's no reason to defer their import.

This is a common pattern for LLM generated Python code, but I don't see any disclosure of your usage of LLM tools.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out that @rtibbles
yes you are 100% right that there is no need to defer that import sorry for that i will move the import to top and update pr accordingly
the inline import was really not intentional i just by mistake overlooked while writing the test . actually i sometimes write quick test this way and then clean them up afterwards but unfortunately missed moving up in this case.
i would like to clear about LLm comment while learning and exploring the codebase i do occasionally use llm tool for guidance but for code i generally write myself else would have mentioned in the specific section . i am trying to improve my coding style .
thanks again for review i will push the fix in few minute

@TheGreatPratyush
Copy link
Copy Markdown
Contributor Author

TheGreatPratyush commented Mar 7, 2026

i have moved import to top and pushed the update . Please let me know if anything else needs any adjustment .

Copy link
Copy Markdown
Member

@rtibbles rtibbles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as all checks pass this is good to go! Thank you.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 7, 2026

@TheGreatPratyush
Copy link
Copy Markdown
Contributor Author

All checks are passing now . Thanks for the review.

@rtibbles rtibbles merged commit ccce75d into learningequality:develop Mar 9, 2026
51 checks passed
@rtibbles
Copy link
Copy Markdown
Member

rtibbles commented Mar 9, 2026

Thank you!

@TheGreatPratyush TheGreatPratyush deleted the fix-classroom-notification-unboundlocalerror branch March 9, 2026 06:13
marcellamaki pushed a commit to marcellamaki/kolibri that referenced this pull request Mar 11, 2026
…er_queryset raises DatabaseError (learningequality#14345)

* Fix UnboundLocalError in ClassroomNotificationsViewset.list when filter_queryset raises DatabaseError

* Add regression test for ClassroomNotificationsViewset DatabaseError handling

* Move unittest.mock import to top-level as suggested in review

* [pre-commit.ci lite] apply automatic fixes

* Fix DatabaseError handling in ClassroomNotificationsViewset.list for postgres CI

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

APP: Coach Re: Coach App (lessons, quizzes, groups, reports, etc.) DEV: backend Python, databases, networking, filesystem... SIZE: small SIZE: very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential UnboundLocalError in ClassroomNotificationViewset.list() when filter_queryset() raises DB exception

2 participants