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

Issue:
dpnp.sum(x, axis=0) is very slow on integral 64-bit types
```
In [1]: import dpnp

In [2]: x = dpnp.reshape(dpnp.arange(1, 1 + 245612 * 24, 1, dtype="i8"), (245612, 24))

In [3]: %time y = dpnp.sum(x, axis=0, dtype="i8")
CPU times: user 1.13 s, sys: 1.11 s, total: 2.24 s
Wall time: 2.43 s

In [4]: %time y = dpnp.sum(x, axis=0, dtype="i8")
CPU times: user 1.22 s, sys: 1.03 s, total: 2.25 s
Wall time: 2.44 s

In [5]: import dpctl.tensor as dpt

In [7]: x_dpt = dpt.asarray(x)

In [8]: %time y_dpt = dpt.sum(x_dpt, axis=0, dtype="i8")
CPU times: user 156 ms, sys: 3.52 ms, total: 160 ms
Wall time: 175 ms

In [9]: %time y_dpt = dpt.sum(x_dpt, axis=0, dtype="i8")
CPU times: user 1.82 ms, sys: 1.16 ms, total: 2.97 ms
Wall time: 3.06 ms
```

@AlexanderKalistratov 


Discussion:

Resolving patch:
diff --git a/dpnp/dpnp_iface_mathematical.py b/dpnp/dpnp_iface_mathematical.py
--- a/dpnp/dpnp_iface_mathematical.py
+++ b/dpnp/dpnp_iface_mathematical.py
@@ -2485,9 +2485,23 @@ def sum(
     elif where is not True:
         pass
     else:
-        if len(x.shape) == 2 and (
-            (axis == (0,) and x.flags.c_contiguous)
-            or (axis == (1,) and x.flags.f_contiguous)
+        if (
+            len(x.shape) == 2
+            and x.itemsize == 4
+            and (
+                (
+                    axis == (0,)
+                    and x.flags.c_contiguous
+                    and 32 <= x.shape[1] <= 1024
+                    and x.shape[0] > x.shape[1]
+                )
+                or (
+                    axis == (1,)
+                    and x.flags.f_contiguous
+                    and 32 <= x.shape[0] <= 1024
+                    and x.shape[1] > x.shape[0]
+                )
+            )
         ):
             from dpctl.tensor._reduction import _default_reduction_dtype
 


Test patch:
diff --git a/tests/test_mathematical.py b/tests/test_mathematical.py
--- a/tests/test_mathematical.py
+++ b/tests/test_mathematical.py
@@ -1220,15 +1220,18 @@ def test_sum_empty_out(dtype):
         (0, 6),
         (10, 1),
         (1, 10),
+        (35, 40),
+        (40, 35),
     ],
 )
 @pytest.mark.parametrize("dtype_in", get_all_dtypes())
 @pytest.mark.parametrize("dtype_out", get_all_dtypes())
 @pytest.mark.parametrize("transpose", [True, False])
 @pytest.mark.parametrize("keepdims", [True, False])
-def test_sum(shape, dtype_in, dtype_out, transpose, keepdims):
+@pytest.mark.parametrize("order", ["C", "F"])
+def test_sum(shape, dtype_in, dtype_out, transpose, keepdims, order):
     size = numpy.prod(shape)
-    a_np = numpy.arange(size).astype(dtype_in).reshape(shape)
+    a_np = numpy.arange(size).astype(dtype_in).reshape(shape, order=order)
     a = dpnp.asarray(a_np)
 
     if transpose:
Repository: adamchainz/django-mysql

Issue:
pt_visual_explain need not execute the query
There must be some way of monkey-patching somewhere in MySQLdb/mysqlclient or django to grab the full query from the queryset without executing it. Find it!



Discussion:

Resolving patch:
diff --git a/django_mysql/models/query.py b/django_mysql/models/query.py
--- a/django_mysql/models/query.py
+++ b/django_mysql/models/query.py
@@ -1,5 +1,5 @@
 # -*- coding:utf-8 -*-
-from __future__ import print_function
+from __future__ import print_function, unicode_literals
 
 import sys
 from copy import copy
@@ -309,30 +309,24 @@ def __iter__(self):
 
 
 def pt_visual_explain(queryset, display=True):
-    connection = connections[queryset.db]
-
     if not have_program('pt-visual-explain'):  # pragma: no cover
         raise OSError("pt-visual-explain doesn't appear to be installed")
 
-    # Run one query to ensure we are connected
-    # This allows us to ensure we capture only the queryset's query below,
-    # as Django (and maybe settings) run statements when connecting
-    with connection.cursor() as cursor:
-        cursor.execute("SELECT 1")
-
+    connection = connections[queryset.db]
     capturer = CaptureQueriesContext(connection)
-    with capturer:
-        list(queryset)  # execute the query, discarding results
+    with capturer, connection.cursor() as cursor:
+        sql, params = queryset.query.sql_with_params()
+        cursor.execute('EXPLAIN ' + sql, params)
 
     queries = [q['sql'] for q in capturer.captured_queries]
-    assert len(queries) == 1, \
-        "QuerySet executed > 1 query, don't know which to EXPLAIN"
-    query = queries[0]
+    # Take the last - django may have just opened up a connection in which
+    # case it would have run initialization command[s]
+    explain_query = queries[-1]
 
-    # Now to do the explain and pass through pt-visual-explain
+    # Now do the explain and pass through pt-visual-explain
     mysql_command = (
         settings_to_cmd_args(connection.settings_dict) +
-        ['-e', "EXPLAIN EXTENDED " + query]
+        ['-e', explain_query]
     )
     mysql = Popen(mysql_command, stdout=PIPE)
     visual_explain = Popen(


Test patch:
diff --git a/tests/settings.py b/tests/settings.py
--- a/tests/settings.py
+++ b/tests/settings.py
@@ -51,10 +51,8 @@
     ) + INSTALLED_APPS
 
 MIDDLEWARE_CLASSES = (
-    'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
-    'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
 )
Repository: adamchainz/django-mysql

Issue:
Fix SmartIterator ZeroDivisionError
Seen happening on an iterator in a test case which iterated very quickly:

```
  emacs +170 /opt/yplan/venv/local/lib/python2.7/site-packages/django_mysql/models/query.py  # __iter__
    self.adjust_chunk_size(chunk, timer.total_time)
  emacs +228 /opt/yplan/venv/local/lib/python2.7/site-packages/django_mysql/models/query.py  # adjust_chunk_size
    new_chunk_size = self.rate.update(num_processed, chunk_time)
  emacs +44  /opt/yplan/venv/local/lib/python2.7/site-packages/django_mysql/utils.py  # update
    avg_rate = self.avg_n / self.avg_t
ZeroDivisionError: float division by zero
```



Discussion:

Resolving patch:
diff --git a/django_mysql/utils.py b/django_mysql/utils.py
--- a/django_mysql/utils.py
+++ b/django_mysql/utils.py
@@ -46,7 +46,12 @@ def update(self, n, t):
             self.avg_n = n
             self.avg_t = t
 
-        avg_rate = self.avg_n / self.avg_t
+        try:
+            avg_rate = self.avg_n / self.avg_t
+        except ZeroDivisionError:
+            # Assume a small amount of time, not 0
+            avg_rate = self.avg_n / 0.001
+
         new_n = int(avg_rate * self.target_t)
         return new_n
 


Test patch:
diff --git a/tests/django_mysql_tests/test_utils.py b/tests/django_mysql_tests/test_utils.py
--- a/tests/django_mysql_tests/test_utils.py
+++ b/tests/django_mysql_tests/test_utils.py
@@ -45,6 +45,10 @@ def test_good_guess(self):
         self.assertEqual(rate.update(50, 0.5), 50)
         self.assertEqual(rate.update(50, 0.5), 50)
 
+    def test_zero_division(self):
+        rate = WeightedAverageRate(0.5)
+        self.assertEqual(rate.update(1, 0.0), 500)
+
 
 @skipUnless(have_program('pt-fingerprint'),
             "pt-fingerprint must be installed")
Repository: adamchainz/django-mysql

Issue:
mysql_cache_migration should add dependencies to migration file
Even though it doesn't know which app you'll put your migration in, a dependency is required to make it play nicely so the template should include a blank list and comment that you should hook it up.



Discussion:

Resolving patch:
diff --git a/django_mysql/management/commands/mysql_cache_migration.py b/django_mysql/management/commands/mysql_cache_migration.py
--- a/django_mysql/management/commands/mysql_cache_migration.py
+++ b/django_mysql/management/commands/mysql_cache_migration.py
@@ -64,6 +64,9 @@ def render_migration(self, tables):
 class Migration(migrations.Migration):
 
     dependencies = [
+        # Add a dependency in here on an existing migration in the app you
+        # put this migration in, for example:
+        # ('myapp', '0001_initial'),
     ]
 
     operations = [


Test patch:
Repository: adamchainz/django-mysql

Issue:
Smart iterator output "Finished" message should say how long it took
It would be nice after large data migrations to know how long it took. The `Finished!` message could easily be extended with the time taken.



Discussion:

Resolving patch:
diff --git a/django_mysql/models/query.py b/django_mysql/models/query.py
--- a/django_mysql/models/query.py
+++ b/django_mysql/models/query.py
@@ -3,6 +3,7 @@
 
 import operator
 import sys
+import time
 from copy import copy
 from subprocess import PIPE, Popen
 
@@ -422,6 +423,7 @@ def init_progress(self, direction):
         if not self.report_progress:
             return
 
+        self.start_time = time.time()
         self.old_report = ""
         self.objects_done = 0
         self.chunks_done = 0
@@ -494,7 +496,14 @@ def end_progress(self):
         if not self.report_progress:
             return
 
-        sys.stdout.write("\nFinished!\n")
+        total_time = time.time() - self.start_time
+        sys.stdout.write(
+            "\nFinished! Iterated over {n} object{s} in {duration}.\n".format(
+                n=self.objects_done,
+                s='s' if self.objects_done != 1 else '',
+                duration=format_duration(total_time)
+            )
+        )
 
     @cached_property
     def model_name(self):


Test patch:
diff --git a/tests/testapp/test_models.py b/tests/testapp/test_models.py
--- a/tests/testapp/test_models.py
+++ b/tests/testapp/test_models.py
@@ -629,7 +629,10 @@ def test_reporting(self):
                 report
             )
 
-        assert lines[1] == 'Finished!'
+        assert re.match(
+            r'Finished! Iterated over \d+ objects? in [\dhms]+.',
+            lines[1],
+        )
 
     def test_reporting_reverse(self):
         Author.objects.create(id=10000)
@@ -652,7 +655,10 @@ def test_reporting_reverse(self):
                 report
             )
 
-        assert lines[1] == 'Finished!'
+        assert re.match(
+            r'Finished! Iterated over \d+ objects? in [\dhms]+.',
+            lines[1],
+        )
 
     def test_reporting_with_total(self):
         with captured_stdout() as output:
@@ -671,7 +677,10 @@ def test_reporting_with_total(self):
                 report
             )
 
-        assert lines[1] == 'Finished!'
+        assert re.match(
+            r'Finished! Iterated over \d+ objects? in [\dhms]+.',
+            lines[1],
+        )
 
     def test_reporting_on_uncounted_qs(self):
         Author.objects.create(name="pants")
@@ -693,7 +702,10 @@ def test_reporting_on_uncounted_qs(self):
                 report
             )
 
-        assert lines[1] == 'Finished!'
+        assert re.match(
+            r'Finished! Iterated over \?\?\? objects? in [\dhms.]+.',
+            lines[1],
+        )
 
     def test_filter_and_delete(self):
         VanillaAuthor.objects.create(name="Alpha")
Repository: adamchainz/django-mysql

Issue:
Smart iterator final time report is too precise
It outputs all digits in the seconds field which isn't really useful. Maybe down to the milliseconds is good enough?



Discussion:

Resolving patch:
diff --git a/django_mysql/models/query.py b/django_mysql/models/query.py
--- a/django_mysql/models/query.py
+++ b/django_mysql/models/query.py
@@ -496,7 +496,7 @@ def end_progress(self):
         if not self.report_progress:
             return
 
-        total_time = time.time() - self.start_time
+        total_time = int(time.time() - self.start_time)
         sys.stdout.write(
             "\nFinished! Iterated over {n} object{s} in {duration}.\n".format(
                 n=self.objects_done,


Test patch:
diff --git a/tests/testapp/test_models.py b/tests/testapp/test_models.py
--- a/tests/testapp/test_models.py
+++ b/tests/testapp/test_models.py
@@ -703,7 +703,7 @@ def test_reporting_on_uncounted_qs(self):
             )
 
         assert re.match(
-            r'Finished! Iterated over \?\?\? objects? in [\dhms.]+.',
+            r'Finished! Iterated over \?\?\? objects? in [\dhms]+.',
             lines[1],
         )
Repository: adamchainz/django-mysql

Issue:
JSONField should check for mutable defaults
Summary: If JSONField is given e.g. `default=[]`, it should complain in `check()`



Discussion:

Resolving patch:
diff --git a/django_mysql/models/fields/json.py b/django_mysql/models/fields/json.py
--- a/django_mysql/models/fields/json.py
+++ b/django_mysql/models/fields/json.py
@@ -15,6 +15,7 @@
 from django_mysql.models.lookups import (
     JSONContainedBy, JSONContains, JSONHasAnyKeys, JSONHasKey, JSONHasKeys
 )
+from django_mysql.utils import collapse_spaces
 
 __all__ = ('JSONField',)
 
@@ -28,6 +29,7 @@ def __init__(self, *args, **kwargs):
     def check(self, **kwargs):
         errors = super(JSONField, self).check(**kwargs)
         errors.extend(self._check_django_version())
+        errors.extend(self._check_default())
 
         if django.VERSION[:2] >= (1, 8):
             # This check connects to the DB, which only works on Django 1.8+
@@ -47,6 +49,26 @@ def _check_django_version(self):
             )
         return errors
 
+    def _check_default(self):
+        errors = []
+        if isinstance(self.default, (list, dict)):
+            errors.append(
+                checks.Error(
+                    'Do not use mutable defaults for JSONField',
+                    hint=collapse_spaces('''
+                        Mutable defaults get shared between all instances of
+                        the field, which probably isn't what you want. You
+                        should replace your default with a callable, e.g.
+                        replace default={{}} with default=dict.
+
+                        The default you passed was '{}'.
+                    '''.format(self.default)),
+                    obj=self,
+                    id='django_mysql.E017',
+                )
+            )
+        return errors
+
     def _check_mysql_version(self):
         errors = []
 


Test patch:
diff --git a/tests/testapp/test_jsonfield.py b/tests/testapp/test_jsonfield.py
--- a/tests/testapp/test_jsonfield.py
+++ b/tests/testapp/test_jsonfield.py
@@ -411,6 +411,24 @@ def test_old_django(self):
         assert errors[0].id == 'django_mysql.E015'
         assert 'Django 1.8+ is required' in errors[0].msg
 
+    def test_mutable_default_list(self):
+        class InvalidJSONModel(TemporaryModel):
+            field = JSONField(default=[])
+
+        errors = InvalidJSONModel.check(actually_check=True)
+        assert len(errors) == 1
+        assert errors[0].id == 'django_mysql.E017'
+        assert 'Do not use mutable defaults for JSONField' in errors[0].msg
+
+    def test_mutable_default_dict(self):
+        class InvalidJSONModel(TemporaryModel):
+            field = JSONField(default=[])
+
+        errors = InvalidJSONModel.check(actually_check=True)
+        assert len(errors) == 1
+        assert errors[0].id == 'django_mysql.E017'
+        assert 'Do not use mutable defaults for JSONField' in errors[0].msg
+
     wrapper_path = 'django.db.backends.mysql.base.DatabaseWrapper'
 
     @mock.patch(wrapper_path + '.is_mariadb', new=True)
Repository: adamchainz/django-mysql

Issue:
Smart Iterator - fail when primary key is a non-integer foreign key
Summary: 4506a1cf2c604d11814c935c2624b3a9ed722a3d didn't go far enough, for `ForeignKey` instances we need to check that the target field is an integer, we can't just whitelist `ForeignKey` outright



Discussion:

Resolving patch:
diff --git a/django_mysql/models/query.py b/django_mysql/models/query.py
--- a/django_mysql/models/query.py
+++ b/django_mysql/models/query.py
@@ -340,7 +340,13 @@ def sanitize_queryset(self, queryset):
             )
 
         pk = queryset.model._meta.pk
-        if not isinstance(pk, self.ALLOWED_PK_FIELD_CLASSES):
+        allowed_field = (
+            isinstance(pk, self.ALLOWED_PK_FIELD_CLASSES) or
+            (isinstance(pk, models.ForeignKey) and
+             isinstance(pk.foreign_related_fields[0],
+                        self.ALLOWED_PK_FIELD_CLASSES))
+        )
+        if not allowed_field:
             # If your custom field class should be allowed, just add it to
             # ALLOWED_PK_FIELD_CLASSES
             raise ValueError(
@@ -353,7 +359,6 @@ def sanitize_queryset(self, queryset):
     ALLOWED_PK_FIELD_CLASSES = (
         models.IntegerField,  # Also covers e.g. PositiveIntegerField
         models.AutoField,  # Is an integer field but doesn't subclass it :(
-        models.ForeignKey  # Should always point to an integer
     )
 
     def get_first_and_last(self):


Test patch:
diff --git a/tests/testapp/models.py b/tests/testapp/models.py
--- a/tests/testapp/models.py
+++ b/tests/testapp/models.py
@@ -184,6 +184,10 @@ def __unicode__(self):
         return "{} {}".format(self.id, self.name)
 
 
+class NameAuthorExtra(Model):
+    name = OneToOneField(NameAuthor, primary_key=True)
+
+
 class AuthorMultiIndex(Model):
     class Meta(object):
         index_together = ('name', 'country')
diff --git a/tests/testapp/test_models.py b/tests/testapp/test_models.py
--- a/tests/testapp/test_models.py
+++ b/tests/testapp/test_models.py
@@ -16,7 +16,8 @@
 )
 from django_mysql.utils import have_program, index_name
 from testapp.models import (
-    Author, AuthorExtra, AuthorMultiIndex, Book, NameAuthor, VanillaAuthor
+    Author, AuthorExtra, AuthorMultiIndex, Book, NameAuthor, NameAuthorExtra,
+    VanillaAuthor
 )
 from testapp.utils import CaptureLastQuery, captured_stdout, used_indexes
 
@@ -612,6 +613,11 @@ def test_iter_smart_fk_primary_key(self):
             seen_author_ids.append(extra.author_id)
         assert seen_author_ids == [author.id, author2.id]
 
+    def test_iter_smart_fk_string_primary_key_fails(self):
+        with pytest.raises(ValueError) as excinfo:
+            NameAuthorExtra.objects.iter_smart()
+        assert "non-integer primary key" in str(excinfo.value)
+
     def test_reporting(self):
         with captured_stdout() as output:
             qs = Author.objects.all()
Repository: adamchainz/django-mysql

Issue:
ImportError: No module named termios on Windows
Summary: https://stackoverflow.com/questions/36508126/importerror-no-module-named-termios-with-django-mysql-on-windows

Django Version: 1.8.7
Database and version used: MySQL 5.6
Version: Django-MySQL 1.0.7

Even after installing Cygwin.



Discussion:
Looks like here pty is the culprit too? https://github.com/mwilliamson/spur.py/issues/16


Resolving patch:
diff --git a/django_mysql/utils.py b/django_mysql/utils.py
--- a/django_mysql/utils.py
+++ b/django_mysql/utils.py
@@ -2,7 +2,6 @@
 from __future__ import division
 
 import os
-import pty
 import time
 from collections import defaultdict
 from contextlib import contextmanager
@@ -201,6 +200,9 @@ def __init__(self, in_queue, out_queue, **kwargs):
         super(PTFingerprintThread, self).__init__(**kwargs)
 
     def run(self):
+        # pty is unix/linux only
+        import pty  # noqa
+
         global fingerprint_thread
         master, slave = pty.openpty()
         proc = Popen(


Test patch:
Repository: adamchainz/django-mysql

Issue:
'dbparams' doesn't need to run system checks
Summary: `manage.py dbparams` can be unnecessarily slow because it runs all the system checks, these can be disabled on the `Command` class quite easily.



Discussion:

Resolving patch:
diff --git a/django_mysql/management/commands/dbparams.py b/django_mysql/management/commands/dbparams.py
--- a/django_mysql/management/commands/dbparams.py
+++ b/django_mysql/management/commands/dbparams.py
@@ -19,6 +19,8 @@ class Command(BaseCommand):
             "connection alias should be a name from DATABASES - defaults to "
             "'{default}'.").format(default=DEFAULT_DB_ALIAS)
 
+    requires_system_checks = False
+
     if django.VERSION[:2] < (1, 8):
 
         option_list = BaseCommand.option_list + (


Test patch:
Repository: adamchainz/django-mysql

Issue:
"manage.py dumpdata" uses str instead of json.dumps on JSONField
Summary: Running ./manage.py dumpdata dumps the the JSONField as a Python dictionary string instead of a JSON string.

e.g.

```json
[
    {
        "model": "myapp.mymodel",
        "pk": 1,
        "fields": {
            "my_json_field": "{'field1': None, 'field2': False}"
        }
    }
]
```

instead of 

```json
[
    {
        "model": "myapp.mymodel",
        "pk": 1,
        "fields": {
            "my_json_field": "{\"field1\": null, \"field2\": false}"
        }
    }
]
```

*If this is a bug, please fill in the below, else delete*

Django Version: Django 1.10.6

Database and version used: MySQL 5.7

Version: Django-MySQL 1.1.1



Discussion:
Aha! We probably need to add some extra methods to the field to support the serializers. Have you had a look at how `django.contrib.postgres` works with `dumpdata`?
I looks like the field just needs a `value_to_string()` method.

[Postgres version](https://github.com/django/django/blob/master/django/contrib/postgres/fields/jsonb.py#L73) and [Django docs](https://docs.djangoproject.com/en/1.11/howto/custom-model-fields/#converting-field-data-for-serialization)

Resolving patch:
diff --git a/django_mysql/models/fields/json.py b/django_mysql/models/fields/json.py
--- a/django_mysql/models/fields/json.py
+++ b/django_mysql/models/fields/json.py
@@ -120,6 +120,9 @@ def get_lookup(self, lookup_name):
             )
         return super(JSONField, self).get_lookup(lookup_name)
 
+    def value_to_string(self, obj):
+        return self.value_from_object(obj)
+
     def formfield(self, **kwargs):
         defaults = {'form_class': forms.JSONField}
         defaults.update(kwargs)


Test patch:
diff --git a/tests/testapp/test_jsonfield.py b/tests/testapp/test_jsonfield.py
--- a/tests/testapp/test_jsonfield.py
+++ b/tests/testapp/test_jsonfield.py
@@ -3,9 +3,11 @@
     absolute_import, division, print_function, unicode_literals
 )
 
+import json
 from unittest import SkipTest, mock
 
 import pytest
+from django.core import serializers
 from django.db import connection, connections
 from django.db.models import F
 from django.test import TestCase
@@ -475,3 +477,25 @@ def test_model_field_formfield(self):
         model_field = JSONField()
         form_field = model_field.formfield()
         assert isinstance(form_field, forms.JSONField)
+
+
+class TestSerialization(JSONFieldTestCase):
+    test_data = '''[
+        {
+            "fields": {
+                "attrs": {"a": "b", "c": null}
+            },
+            "model": "testapp.jsonmodel",
+            "pk": null
+        }
+    ]'''
+
+    def test_dumping(self):
+        instance = JSONModel(attrs={'a': 'b', 'c': None})
+        data = serializers.serialize('json', [instance])
+        assert json.loads(data) == json.loads(self.test_data)
+
+    def test_loading(self):
+        instances = list(serializers.deserialize('json', self.test_data))
+        instance = instances[0].object
+        assert instance.attrs == {'a': 'b', 'c': None}