Skip to content

Commit 8b2cccc

Browse files
authored
Stop calling set_context, planned for 3.13 drop (#8589)
Per the deprecation warnings (which have been raised since DRF 3.11), `set_context()` was planned not to be supported in DRF 3.13. I think we can safely delete it, in favor of `requires_context`. From the 3.11 announcement: > Previous our approach to this was that implementations could include a > `set_context` method, which would be called prior to validation. However > this approach had issues with potential race conditions. We have now > move this approach into a pending deprecation state. It will continue to > function, but will be escalated to a deprecated state in 3.12, and > removed entirely in 3.13. Why keep `RemovedInDRF313Warning` around? ========================================= It's a bit odd that version 3.13 includes an exception class describing things which are to be deleted in 3.13, but I've opted to keep the (now unreferenced) class around, for fear of breaking others' setup. (For example, if projects have a `filterwarnings` setup meant to intercept `rest_framework.RemovedInDRF313Warning`, an error will be thrown due to an unresolvable reference).
1 parent fd8adb3 commit 8b2cccc

File tree

2 files changed

+2
-34
lines changed

2 files changed

+2
-34
lines changed

rest_framework/fields.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
from django.utils.translation import gettext_lazy as _
3131
from pytz.exceptions import InvalidTimeError
3232

33-
from rest_framework import (
34-
ISO_8601, RemovedInDRF313Warning, RemovedInDRF314Warning
35-
)
33+
from rest_framework import ISO_8601, RemovedInDRF314Warning
3634
from rest_framework.exceptions import ErrorDetail, ValidationError
3735
from rest_framework.settings import api_settings
3836
from rest_framework.utils import html, humanize_datetime, json, representation
@@ -265,16 +263,6 @@ def __call__(self, serializer_field):
265263
if is_update:
266264
raise SkipField()
267265
if callable(self.default):
268-
if hasattr(self.default, 'set_context'):
269-
warnings.warn(
270-
"Method `set_context` on defaults is deprecated and will "
271-
"no longer be called starting with 3.13. Instead set "
272-
"`requires_context = True` on the class, and accept the "
273-
"context as an additional argument.",
274-
RemovedInDRF313Warning, stacklevel=2
275-
)
276-
self.default.set_context(self)
277-
278266
if getattr(self.default, 'requires_context', False):
279267
return self.default(serializer_field)
280268
else:
@@ -504,16 +492,6 @@ def get_default(self):
504492
# No default, or this is a partial update.
505493
raise SkipField()
506494
if callable(self.default):
507-
if hasattr(self.default, 'set_context'):
508-
warnings.warn(
509-
"Method `set_context` on defaults is deprecated and will "
510-
"no longer be called starting with 3.13. Instead set "
511-
"`requires_context = True` on the class, and accept the "
512-
"context as an additional argument.",
513-
RemovedInDRF313Warning, stacklevel=2
514-
)
515-
self.default.set_context(self)
516-
517495
if getattr(self.default, 'requires_context', False):
518496
return self.default(self)
519497
else:
@@ -578,16 +556,6 @@ def run_validators(self, value):
578556
"""
579557
errors = []
580558
for validator in self.validators:
581-
if hasattr(validator, 'set_context'):
582-
warnings.warn(
583-
"Method `set_context` on validators is deprecated and will "
584-
"no longer be called starting with 3.13. Instead set "
585-
"`requires_context = True` on the class, and accept the "
586-
"context as an additional argument.",
587-
RemovedInDRF313Warning, stacklevel=2
588-
)
589-
validator.set_context(self)
590-
591559
try:
592560
if getattr(validator, 'requires_context', False):
593561
validator(value, self)

tests/test_fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def test_create_only_default_is_not_provided_on_update(self):
566566

567567
def test_create_only_default_callable_sets_context(self):
568568
"""
569-
CreateOnlyDefault instances with a callable default should set_context
569+
CreateOnlyDefault instances with a callable default should set context
570570
on the callable if possible
571571
"""
572572
class TestCallableDefault:

0 commit comments

Comments
 (0)