Skip to content

Commit 648ded9

Browse files
Fix mutable default arguments in OrderingFilter methods (#9742)
* 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>
1 parent ad0fea0 commit 648ded9

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ repos:
1818
- id: flake8
1919
additional_dependencies:
2020
- flake8-tidy-imports
21+
- flake8-bugbear
2122
- repo: https://github.com/adamchainz/blacken-docs
2223
rev: 1.20.0
2324
hooks:

rest_framework/filters.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ def get_default_ordering(self, view):
249249
return (ordering,)
250250
return ordering
251251

252-
def get_default_valid_fields(self, queryset, view, context={}):
252+
def get_default_valid_fields(self, queryset, view, context=None):
253+
if context is None:
254+
context = {}
253255
# If `ordering_fields` is not specified, then we determine a default
254256
# based on the serializer class, if one exists on the view.
255257
if hasattr(view, 'get_serializer_class'):
@@ -286,7 +288,9 @@ def get_default_valid_fields(self, queryset, view, context={}):
286288
)
287289
]
288290

289-
def get_valid_fields(self, queryset, view, context={}):
291+
def get_valid_fields(self, queryset, view, context=None):
292+
if context is None:
293+
context = {}
290294
valid_fields = getattr(view, 'ordering_fields', self.ordering_fields)
291295

292296
if valid_fields is None:

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[flake8]
2-
ignore = E501,W503,W504
2+
extend-ignore = E501,W503,W504,B
3+
extend-select = B006
34
banned-modules = json = use from rest_framework.utils import json!

0 commit comments

Comments
 (0)