Fix SonarQube warning: signal handler defined as instance method without self
- Dominant language
- Python
- Stars
- 451
- Forks
- 707
- Avg merge
- 22h 59m
- Merged PRs (30d)
- 91
Description
**Context**
SonarQube reports a **Critical Code Smell** (`python:S5720`) in
`backend/apps/mentorship/signals/program.py`.
A Django `post_save` signal handler is defined inside a class but does not take
`self` and is not marked as `@staticmethod`. This makes the method appear to be
an instance method even though it is not, which is confusing and non-idiomatic
Python.
---
**Current Code**
```python
class ProgramPostSaveHandler:
"""Handles post_save signal for Program model to clear Algolia cache."""
@receiver(post_save, sender=Program)
def program_post_save_clear_algolia_cache(sender, instance, **kwargs):
logger.info(
"Signal received for program '%s'. Clearing 'programs' index.",
instance.name,
)
clear_index_cache("programs")
```
**Why this should be fixed**
- The method is not an instance method and does not use self
- Defining it this way is confusing and against Python best practices
- SonarQube flags this as a Critical maintainability issue
- The intent of the code would be clearer if the method were explicitly static
**Proposed Fix**
Mark the signal handler as a @staticmethod:
```
class ProgramPostSaveHandler:
"""Handles post_save signal for Program model to clear Algolia cache."""
@staticmethod
@receiver(post_save, sender=Program)
def program_post_save_clear_algolia_cache(sender, instance, **kwargs):
logger.info(
"Signal received for program '%s'. Clearing 'programs' index.",
instance.name,
)
clear_index_cache("programs")
```
**Scope**
- Small, isolated change
- No behavior or API changes
- Improves clarity and maintainability
- Resolves the SonarQube warning
Contributor guide
Assessment
This issue has not been assessed yet.