Skip to content

Fix mutable default arguments in OrderingFilter methods #9742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

killerdevildog
Copy link

  • 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

#9741

Note: Before submitting a code change, please review our contributing guidelines.

Description

Please describe your pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue. When linking to an issue, please use refs #... in the description of the pull request.

- 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 encode#9741
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.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy