internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: pytorch/pytorch

Issue:
[DCP] Some Distributed state_dict related APIs docstring not formatting correctly in pytorch docs page 
### 📚 The doc issue

https://pytorch.org/docs/stable/distributed.checkpoint.html

One example:
the example code and the return type of ```get_state_dict``` are not showing up correctly. Need to fix this by 2.2.1.

![image](https://github.com/pytorch/pytorch/assets/31293777/8925b37e-72bd-4687-8146-39a031ad5d46)

### Suggest a potential alternative/fix

_No response_

cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu @fegin @XilunWu @wanchaol @fduwjj @tianyu-l @wconstab @yf225 @LucasLLC


Discussion:

Resolving patch:
diff --git a/torch/distributed/checkpoint/state_dict.py b/torch/distributed/checkpoint/state_dict.py
--- a/torch/distributed/checkpoint/state_dict.py
+++ b/torch/distributed/checkpoint/state_dict.py
@@ -678,25 +678,25 @@ def get_state_dict(
     optimizer parameter IDs to the canonical FQNs.
 
     Example:
+        >>> # xdoctest: +SKIP
+        >>> import torch
+        >>> from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
+        >>> from torch.nn.parallel import DistributedDataParallel as DDP
+        >>> from torch.distributed.checkpoint.state_dict import get_state_dict
 
-        import torch
-        from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
-        from torch.nn.parallel import DistributedDataParallel as DDP
-        from torch.distributed.checkpoint.state_dict import get_state_dict
-
-        fsdp_model = FSDP(copy.deepcopy(model))
-        fsdp_optim = torch.optim.Adam(model.parameters(), lr=1e-3)
-        ddp_model = DDP(copy.deepcopy(model))
-        ddp_optim = torch.optim.Adam(model.parameters(), lr=1e-3)
+        >>> fsdp_model = FSDP(copy.deepcopy(model))
+        >>> fsdp_optim = torch.optim.Adam(model.parameters(), lr=1e-3)
+        >>> ddp_model = DDP(copy.deepcopy(model))
+        >>> ddp_optim = torch.optim.Adam(model.parameters(), lr=1e-3)
 
 
-        ddp_state_dict, ddp_optim_state_dict = get_state_dict(ddp_model, ddp_optim)
-        fsdp_state_dict, fsdp_optim_state_dict = get_state_dict(fsdp_model, fsdp_optim)
+        >>> ddp_state_dict, ddp_optim_state_dict = get_state_dict(ddp_model, ddp_optim)
+        >>> fsdp_state_dict, fsdp_optim_state_dict = get_state_dict(fsdp_model, fsdp_optim)
 
-        # if we simply call ddp_model.state_dict() and fsdp_model.state_dict(),
-        # the asserts will fail.
-        assert ddp_state_dict == fsdp_state_dict
-        assert ddp_optim_state == fsdp_optim_state_dict
+        >>> # if we simply call ddp_model.state_dict() and fsdp_model.state_dict(),
+        >>> # the asserts will fail.
+        >>> assert ddp_state_dict == fsdp_state_dict
+        >>> assert ddp_optim_state == fsdp_optim_state_dict
 
 
     Args:
@@ -711,6 +711,8 @@ def get_state_dict(
 
     Returns:
         ``Tuple`` that contain model state_dict and optimizer state_dict.
+
+    :rtype: typing.Tuple[typing.Dict[str, ValueType], OptimizerStateType]
     """
 
     with gc_context():


Test patch:
Repository: encode/django-rest-framework

Issue:
ModelSerializers should respect the max_length attribue of a Model Field
The current implementation causes a DatabaseError because the passed value is too large.



Discussion:

Resolving patch:
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -700,6 +700,23 @@ def from_native(self, value):
         return smart_unicode(value)
 
 
+class URLField(CharField):
+    type_name = 'URLField'
+
+    def __init__(self, **kwargs):
+        kwargs['max_length'] = kwargs.get('max_length', 200)
+        kwargs['validators'] = [validators.URLValidator()]
+        super(URLField, self).__init__(**kwargs)
+
+
+class SlugField(CharField):
+    type_name = 'SlugField'
+
+    def __init__(self, *args, **kwargs):
+        kwargs['max_length'] = kwargs.get('max_length', 50)
+        super(SlugField, self).__init__(*args, **kwargs)
+
+
 class ChoiceField(WritableField):
     type_name = 'ChoiceField'
     widget = widgets.Select
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -427,6 +427,10 @@ def get_field(self, model_field):
             kwargs['choices'] = model_field.flatchoices
             return ChoiceField(**kwargs)
 
+        max_length = getattr(model_field, 'max_length', None)
+        if max_length:
+            kwargs['max_length'] = max_length
+
         field_mapping = {
             models.FloatField: FloatField,
             models.IntegerField: IntegerField,
@@ -437,6 +441,8 @@ def get_field(self, model_field):
             models.DateField: DateField,
             models.EmailField: EmailField,
             models.CharField: CharField,
+            models.URLField: URLField,
+            models.SlugField: SlugField,
             models.TextField: CharField,
             models.CommaSeparatedIntegerField: CharField,
             models.BooleanField: BooleanField,


Test patch:
diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py
--- a/rest_framework/tests/serializer.py
+++ b/rest_framework/tests/serializer.py
@@ -239,6 +239,14 @@ def test_null_is_true_fields(self):
         self.assertEquals(serializer.is_valid(), True)
         self.assertEquals(serializer.errors, {})
 
+    def test_modelserializer_max_length_exceeded(self):
+        data = {
+            'title': 'x' * 201,
+        }
+        serializer = ActionItemSerializer(data=data)
+        self.assertEquals(serializer.is_valid(), False)
+        self.assertEquals(serializer.errors, {'title': [u'Ensure this value has at most 200 characters (it has 201).']})
+
 
 class MetadataTests(TestCase):
     def test_empty(self):
Repository: encode/django-rest-framework

Issue:
ModelSerializer validation and blank=True
When None values are passed to the ModelSerializer validation, for a field that has `blank=True` set, but not `null=True`, the serializer will return "This field is required."

For example, if the field is:

```
title = models.CharField(max_length=100, blank=True, null=False)
```

If no value is passed to the API view - validation will fail.

Django suggests setting non empty string fields as `blank=True, null=False`:

> Note that empty string values will always get stored as empty strings, not as NULL. Only use null=True for non-string fields such as integers, booleans and dates...

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.null

This issue is similar to #336 

Will submit a pull-request with a failing test case soon.



Discussion:

Resolving patch:
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -456,7 +456,7 @@ def get_field(self, model_field):
 
         kwargs['blank'] = model_field.blank
 
-        if model_field.null:
+        if model_field.null or model_field.blank:
             kwargs['required'] = False
 
         if model_field.has_default():


Test patch:
diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py
--- a/rest_framework/tests/models.py
+++ b/rest_framework/tests/models.py
@@ -160,7 +160,7 @@ class Photo(RESTFrameworkModel):
 
 # Model for issue #324
 class BlankFieldModel(RESTFrameworkModel):
-    title = models.CharField(max_length=100, blank=True, null=True)
+    title = models.CharField(max_length=100, blank=True, null=False)
 
 
 # Model for issue #380
diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py
--- a/rest_framework/tests/serializer.py
+++ b/rest_framework/tests/serializer.py
@@ -704,6 +704,10 @@ def test_create_model_not_blank_field(self):
         serializer = self.not_blank_model_serializer_class(data=self.data)
         self.assertEquals(serializer.is_valid(), False)
 
+    def test_create_model_null_field(self):
+        serializer = self.model_serializer_class(data={})
+        self.assertEquals(serializer.is_valid(), True)
+
 
 #test for issue #460
 class SerializerPickleTests(TestCase):
Repository: encode/django-rest-framework

Issue:
Partial serializer and "default" values
If a model has a field with a default value set, and it's updated by a partial serializer with the field excluded from the data - the previously saved value will be overridden by the default.



Discussion:

Resolving patch:
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -189,8 +189,10 @@ def field_from_native(self, data, files, field_name, into):
             else:
                 native = data[field_name]
         except KeyError:
-            if self.default is not None:
+            if self.default is not None and not self.root.partial:
                 native = self.default
+                # partial serializers shouldn't set the default field to avoid
+                # overriding the previously set value
             else:
                 if self.required:
                     raise ValidationError(self.error_messages['required'])


Test patch:
diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py
--- a/rest_framework/tests/models.py
+++ b/rest_framework/tests/models.py
@@ -71,6 +71,7 @@ class SlugBasedModel(RESTFrameworkModel):
 
 class DefaultValueModel(RESTFrameworkModel):
     text = models.CharField(default='foobar', max_length=100)
+    extra = models.CharField(blank=True, null=True, max_length=100)
 
 
 class CallableDefaultValueModel(RESTFrameworkModel):
diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py
--- a/rest_framework/tests/serializer.py
+++ b/rest_framework/tests/serializer.py
@@ -340,7 +340,6 @@ class Meta:
         self.assertTrue(photo_serializer.save())
 
 
-
 class RegexValidationTest(TestCase):
     def test_create_failed(self):
         serializer = BookSerializer(data={'isbn': '1234567890'})
@@ -551,6 +550,21 @@ def test_create_overriding_default(self):
         self.assertEquals(instance.pk, 1)
         self.assertEquals(instance.text, 'overridden')
 
+    def test_partial_update_default(self):
+        """ Regression test for issue #532 """
+        data = {'text': 'overridden'}
+        serializer = self.serializer_class(data=data, partial=True)
+        self.assertEquals(serializer.is_valid(), True)
+        instance = serializer.save()
+
+        data = {'extra': 'extra_value'}
+        serializer = self.serializer_class(instance=instance, data=data, partial=True)
+        self.assertEquals(serializer.is_valid(), True)
+        instance = serializer.save()
+
+        self.assertEquals(instance.extra, 'extra_value')
+        self.assertEquals(instance.text, 'overridden')
+
 
 class CallableDefaultValueTests(TestCase):
     def setUp(self):
Repository: encode/django-rest-framework

Issue:
PrimaryKeyRelatedField with OneToOneField serializes wrong object's id
```
class A(Model):
    pass
class B(Model):
    a = OneToOneField('A')

class ASerializer(Serializer):
    b_id = PrimaryKeyRelatedField(source='b', null=True)
```

Now when an `A` is serialized, it will not have `B`'s `id`, but its own. I believe this is due to [this erroneous line in PrimaryKeyRelatedField](https://github.com/tomchristie/django-rest-framework/blob/018298deb89628b39e1caeceecb414c1e27310da/rest_framework/relations.py#L238). Once I remove that line, the correct value for `b_id` will be serialized.



Discussion:
Yup this looks incorrect to me too.

I'll try submitting a PR for this


Resolving patch:
diff --git a/rest_framework/relations.py b/rest_framework/relations.py
--- a/rest_framework/relations.py
+++ b/rest_framework/relations.py
@@ -235,7 +235,6 @@ def field_to_native(self, obj, field_name):
                 pk = getattr(obj, self.source or field_name).pk
             except ObjectDoesNotExist:
                 return None
-            return self.to_native(obj.pk)
 
         # Forward relationship
         return self.to_native(pk)


Test patch:
diff --git a/rest_framework/tests/relations_pk.py b/rest_framework/tests/relations_pk.py
--- a/rest_framework/tests/relations_pk.py
+++ b/rest_framework/tests/relations_pk.py
@@ -407,14 +407,14 @@ def setUp(self):
         target.save()
         new_target = OneToOneTarget(name='target-2')
         new_target.save()
-        source = NullableOneToOneSource(name='source-1', target=target)
+        source = NullableOneToOneSource(name='source-1', target=new_target)
         source.save()
 
     def test_reverse_foreign_key_retrieve_with_null(self):
         queryset = OneToOneTarget.objects.all()
         serializer = NullableOneToOneTargetSerializer(queryset, many=True)
         expected = [
-            {'id': 1, 'name': 'target-1', 'nullable_source': 1},
-            {'id': 2, 'name': 'target-2', 'nullable_source': None},
+            {'id': 1, 'name': 'target-1', 'nullable_source': None},
+            {'id': 2, 'name': 'target-2', 'nullable_source': 1},
         ]
         self.assertEqual(serializer.data, expected)
Repository: encode/django-rest-framework

Issue:
Mini.correction in assert in serializers
Hi, I've just noticed a small problem in the get_default_fields methods of the ModelSerializer : line 694 :

```
        assert field_name in ret, \
            "Noexistant field '%s' specified in `read_only_fields` " \
            "on serializer '%s'." % \
            (self.__class__.__name__, field_name)
```

As you may see directly, the arguments are inverted, which leads to rather funny error messages :-)



Discussion:

Resolving patch:
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -690,7 +690,7 @@ def get_default_fields(self):
             assert field_name in ret, \
                 "Noexistant field '%s' specified in `read_only_fields` " \
                 "on serializer '%s'." % \
-                (self.__class__.__name__, field_name)
+                (field_name, self.__class__.__name__)
             ret[field_name].read_only = True
 
         return ret


Test patch:
Repository: encode/django-rest-framework

Issue:
Views handling Throttled exception with wait == None should not attempt to set Throttle wait header
Defining a non-time-based throttle, in which wait() returns None, causes APIView.handle_exception(exc) to fail when setting X-Throttle-Wait-Seconds header since it's using '%d' % exc.wait



Discussion:

Resolving patch:
diff --git a/rest_framework/views.py b/rest_framework/views.py
--- a/rest_framework/views.py
+++ b/rest_framework/views.py
@@ -269,7 +269,7 @@ def handle_exception(self, exc):
         Handle any exception that occurs, by returning an appropriate response,
         or re-raising the error.
         """
-        if isinstance(exc, exceptions.Throttled):
+        if isinstance(exc, exceptions.Throttled) and exc.wait is not None:
             # Throttle wait header
             self.headers['X-Throttle-Wait-Seconds'] = '%d' % exc.wait
 


Test patch:
diff --git a/rest_framework/tests/test_throttling.py b/rest_framework/tests/test_throttling.py
--- a/rest_framework/tests/test_throttling.py
+++ b/rest_framework/tests/test_throttling.py
@@ -7,7 +7,7 @@
 from django.core.cache import cache
 from rest_framework.test import APIRequestFactory
 from rest_framework.views import APIView
-from rest_framework.throttling import UserRateThrottle, ScopedRateThrottle
+from rest_framework.throttling import BaseThrottle, UserRateThrottle, ScopedRateThrottle
 from rest_framework.response import Response
 
 
@@ -21,6 +21,14 @@ class User3MinRateThrottle(UserRateThrottle):
     scope = 'minutes'
 
 
+class NonTimeThrottle(BaseThrottle):
+    def allow_request(self, request, view):
+        if not hasattr(self.__class__, 'called'):
+            self.__class__.called = True
+            return True
+        return False 
+
+
 class MockView(APIView):
     throttle_classes = (User3SecRateThrottle,)
 
@@ -35,6 +43,13 @@ def get(self, request):
         return Response('foo')
 
 
+class MockView_NonTimeThrottling(APIView):
+    throttle_classes = (NonTimeThrottle,)
+
+    def get(self, request):
+        return Response('foo')
+
+
 class ThrottlingTests(TestCase):
     def setUp(self):
         """
@@ -140,6 +155,22 @@ def test_next_rate_remains_constant_if_followed(self):
           (80, None)
          ))
 
+    def test_non_time_throttle(self):
+        """
+        Ensure for second based throttles.
+        """
+        request = self.factory.get('/')
+
+        self.assertFalse(hasattr(MockView_NonTimeThrottling.throttle_classes[0], 'called'))
+
+        response = MockView_NonTimeThrottling.as_view()(request)
+        self.assertFalse('X-Throttle-Wait-Seconds' in response)
+
+        self.assertTrue(MockView_NonTimeThrottling.throttle_classes[0].called)
+
+        response = MockView_NonTimeThrottling.as_view()(request)
+        self.assertFalse('X-Throttle-Wait-Seconds' in response) 
+
 
 class ScopedRateThrottleTests(TestCase):
     """
Repository: encode/django-rest-framework

Issue:
Browsable API bug (in master)
The browsable API breaks on listview when the queryset is empty.

Git bisect says it was introduced in commit 10d386e.

Traceback:

```
'NoneType' object has no attribute 'company'

File "[...]/django/core/handlers/base.py" in get_response
  140.                     response = response.render()
File "[...]/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "[...]/rest_framework/response.py" in rendered_content
  59.         ret = renderer.render(self.data, media_type, context)
File "[...]/rest_framework/renderers.py" in render
  540.         post_form = self._get_rendered_html_form(view, 'POST', request)
File "[...]/rest_framework/renderers.py" in _get_rendered_html_form
  437.             return self.get_rendered_html_form(view, method, request)
File "[...]/rest_framework/renderers.py" in get_rendered_html_form
  459.         data = serializer.data
File "[...]/rest_framework/serializers.py" in data
  506.                 self._data = self.to_native(obj)
File "[...]/rest_framework/serializers.py" in to_native
  309.             value = field.field_to_native(obj, field_name)
File "[...]/rest_framework/relations.py" in field_to_native
  266.                 pk = getattr(obj, self.source or field_name).pk
```



Discussion:
A quick and dirty fix would be adding:

``` python
if not obj:
    return None
```

in PrimaryKeyRelatedField.field_to_native(), but I'm not sure it covers all cases...

@yprez - Can't replicate this, can you give me a failing example?

Yeah, I'll try replicating this with a simpler case.


Resolving patch:
diff --git a/rest_framework/relations.py b/rest_framework/relations.py
--- a/rest_framework/relations.py
+++ b/rest_framework/relations.py
@@ -264,7 +264,7 @@ def field_to_native(self, obj, field_name):
             # RelatedObject (reverse relationship)
             try:
                 pk = getattr(obj, self.source or field_name).pk
-            except ObjectDoesNotExist:
+            except (ObjectDoesNotExist, AttributeError):
                 return None
 
         # Forward relationship


Test patch:
diff --git a/rest_framework/tests/test_relations_pk.py b/rest_framework/tests/test_relations_pk.py
--- a/rest_framework/tests/test_relations_pk.py
+++ b/rest_framework/tests/test_relations_pk.py
@@ -283,6 +283,15 @@ def test_foreign_key_update_with_invalid_null(self):
         self.assertFalse(serializer.is_valid())
         self.assertEqual(serializer.errors, {'target': ['This field is required.']})
 
+    def test_foreign_key_with_empty(self):
+        """
+        Regression test for #1072
+
+        https://github.com/tomchristie/django-rest-framework/issues/1072
+        """
+        serializer = NullableForeignKeySourceSerializer()
+        self.assertEqual(serializer.data['target'], None)
+
 
 class PKNullableForeignKeyTests(TestCase):
     def setUp(self):
Repository: encode/django-rest-framework

Issue:
RepresenterError with DecimalField and YAMLRenderer
A RepresenterError occurs when trying to render a DecimalField with YAMLRenderer

```
RepresenterError: cannot represent an object: 55.378051
```

Adding

```
SafeDumper.add_representer(decimal.Decimal, SafeDumper.represent_decimal)
```

just after the declaration of SafeDumper (utils/encoders.py) seems to fix the problem.



Discussion:
Gotcha.  Pull request with that fix would be very welcome :)


Resolving patch:
diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py
--- a/rest_framework/utils/encoders.py
+++ b/rest_framework/utils/encoders.py
@@ -89,6 +89,9 @@ def represent_mapping(self, tag, mapping, flow_style=None):
                     node.flow_style = best_style
             return node
 
+    SafeDumper.add_representer(decimal.Decimal,
+            SafeDumper.represent_decimal)
+
     SafeDumper.add_representer(SortedDict,
             yaml.representer.SafeRepresenter.represent_dict)
     SafeDumper.add_representer(DictWithMetadata,


Test patch:
diff --git a/rest_framework/tests/test_renderers.py b/rest_framework/tests/test_renderers.py
--- a/rest_framework/tests/test_renderers.py
+++ b/rest_framework/tests/test_renderers.py
@@ -328,7 +328,7 @@ def test_with_callback(self):
 
     class YAMLRendererTests(TestCase):
         """
-        Tests specific to the JSON Renderer
+        Tests specific to the YAML Renderer
         """
 
         def test_render(self):
@@ -354,6 +354,17 @@ def test_render_and_parse(self):
             data = parser.parse(StringIO(content))
             self.assertEqual(obj, data)
 
+        def test_render_decimal(self):
+            """
+            Test YAML decimal rendering.
+            """
+            renderer = YAMLRenderer()
+            content = renderer.render({'field': Decimal('111.2')}, 'application/yaml')
+            self.assertYAMLContains(content, "field: '111.2'")
+
+        def assertYAMLContains(self, content, string):
+            self.assertTrue(string in content, '%r not in %r' % (string, content))
+
 
 class XMLRendererTestCase(TestCase):
     """
Repository: encode/django-rest-framework

Issue:
Add pre_delete and post_delete hooks to DestroyModelMixin
Like the `pre_save` and `post_save` hooks in `UpdateModelMixin` and `CreateModelMixin`.



Discussion:
Yeah I guess I'd accept a PR for this, so long as it included them in the documentation.


Resolving patch:
diff --git a/rest_framework/generics.py b/rest_framework/generics.py
--- a/rest_framework/generics.py
+++ b/rest_framework/generics.py
@@ -344,6 +344,18 @@ def post_save(self, obj, created=False):
         """
         pass
 
+    def pre_delete(self, obj):
+        """
+        Placeholder method for calling before deleting an object.
+        """
+        pass
+
+    def post_delete(self, obj):
+        """
+        Placeholder method for calling after saving an object.
+        """
+        pass
+
     def metadata(self, request):
         """
         Return a dictionary of metadata about the view.
diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py
--- a/rest_framework/mixins.py
+++ b/rest_framework/mixins.py
@@ -192,5 +192,7 @@ class DestroyModelMixin(object):
     """
     def destroy(self, request, *args, **kwargs):
         obj = self.get_object()
+        self.pre_delete(obj)
         obj.delete()
+        self.post_delete(obj)
         return Response(status=status.HTTP_204_NO_CONTENT)


Test patch: