mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-11-06 10:57:36 +03:00
* Install and configure flake8-bugbear to spot mutable default arguments
* Fix mutable default arguments in OrderingFilter methods
- Fixed get_default_valid_fields() and get_valid_fields() methods in filters.py
- Changed context={} default parameter to context=None to prevent mutable default anti-pattern
- Added proper None checking with context = {} assignment inside methods
Why this fix is important:
- Mutable default arguments (context={}) create shared state across function calls
- Same dict object gets reused, potentially causing unexpected side effects
- This is a well-known Python anti-pattern that can lead to bugs
What was changed:
- Line 249: get_default_valid_fields(self, queryset, view, context=None)
- Line 285: get_valid_fields(self, queryset, view, context=None)
- Added 'if context is None: context = {}' in both methods
Testing results:
- All existing filter tests pass (pytest tests/test_filters.py)
- Custom verification script confirms fix works correctly
- Maintains backward compatibility
- No breaking changes to API
Addresses GitHub issue #9741
---------
Co-authored-by: Bruno Alla <alla.brunoo@gmail.com>
5 lines
130 B
INI
5 lines
130 B
INI
[flake8]
|
|
extend-ignore = E501,W503,W504,B
|
|
extend-select = B006
|
|
banned-modules = json = use from rest_framework.utils import json!
|