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

Issue:
Add alias argument in query_counter
The context_managers.query_counter constructor currently does not accept any arguments.

In my case, I define custom aliases and do not use the default one. When instantiating a query_counter, it calls get_db with no arguments which means that it tries to get the default alias, and throws the following exception:
`mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection`

This problem is easily solved by adding an "alias" parameter in the constructor for query_counter. I can push a PR if you are interested.

Thanks !


Discussion:

Resolving patch:
diff --git a/mongoengine/context_managers.py b/mongoengine/context_managers.py
--- a/mongoengine/context_managers.py
+++ b/mongoengine/context_managers.py
@@ -182,10 +182,10 @@ class query_counter(object):
     - Some queries are ignored by default by the counter (killcursors, db.system.indexes)
     """
 
-    def __init__(self):
+    def __init__(self, alias=DEFAULT_CONNECTION_NAME):
         """Construct the query_counter
         """
-        self.db = get_db()
+        self.db = get_db(alias=alias)
         self.initial_profiling_level = None
         self._ctx_query_counter = 0  # number of queries issued by the context
 


Test patch:
diff --git a/tests/document/test_instance.py b/tests/document/test_instance.py
--- a/tests/document/test_instance.py
+++ b/tests/document/test_instance.py
@@ -2825,6 +2825,44 @@ class B(A):
 
         assert "testdb-1" == B._meta.get("db_alias")
 
+    def test_query_counter_alias(self):
+        """query_counter works properly with db aliases?"""
+        # Register a connection with db_alias testdb-1
+        register_connection("testdb-1", "mongoenginetest2")
+
+        class A(Document):
+            """Uses default db_alias
+            """
+
+            name = StringField()
+
+        class B(Document):
+            """Uses testdb-1 db_alias
+            """
+
+            name = StringField()
+            meta = {"db_alias": "testdb-1"}
+
+        with query_counter() as q:
+            assert q == 0
+            a = A.objects.create(name="A")
+            assert q == 1
+            a = A.objects.first()
+            assert q == 2
+            a.name = "Test A"
+            a.save()
+            assert q == 3
+
+        with query_counter(alias="testdb-1") as q:
+            assert q == 0
+            b = B.objects.create(name="B")
+            assert q == 1
+            b = B.objects.first()
+            assert q == 2
+            b.name = "Test B"
+            b.save()
+            assert q == 3
+
     def test_db_ref_usage(self):
         """DB Ref usage in dict_fields."""
 
@@ -3644,7 +3682,7 @@ class User(Document):
             User.objects().select_related()
 
     def test_embedded_document_failed_while_loading_instance_when_it_is_not_a_dict(
-        self
+        self,
     ):
         class LightSaber(EmbeddedDocument):
             color = StringField()
Repository: MongoEngine/mongoengine

Issue:
New release
Hi,

When is coming new release, because I can't update to mongodb 4.2 because of this: https://github.com/MongoEngine/mongoengine/pull/2160/commits/47f8a126ca167cb8fe020e3cc5604b155dfcdebc.

Thanks


Discussion:
Hi @ZEROF, I believe we'll release a new version shortly. I'd just like to include some of the latest PR before doing this
Hi @bagerard, that is good news, because I want to end this year with :) 
Hi @bagerard, I don't want to sound "heavy ", but this start to be more than annoying, it's kind big issue now.

Thanks


Resolving patch:
diff --git a/mongoengine/__init__.py b/mongoengine/__init__.py
--- a/mongoengine/__init__.py
+++ b/mongoengine/__init__.py
@@ -28,7 +28,7 @@
 )
 
 
-VERSION = (0, 18, 2)
+VERSION = (0, 19, 0)
 
 
 def get_version():


Test patch:
Repository: MongoEngine/mongoengine

Issue:
AttributeError on delete
Hi, I was trying to delete something when I got this error:
`AttributeError: 'DuplicateKeyError' object has no attribute 'message'`

Which is caused by (line 642 in document.py)
```
except pymongo.errors.OperationFailure as err:
    message = "Could not delete document (%s)" % err.message
```
Python no longer supports the message attribute for Exceptions. It should be changed to args or _message


Discussion:

Resolving patch:
diff --git a/mongoengine/document.py b/mongoengine/document.py
--- a/mongoengine/document.py
+++ b/mongoengine/document.py
@@ -639,7 +639,7 @@ def delete(self, signal_kwargs=None, **write_concern):
                 write_concern=write_concern, _from_doc_delete=True
             )
         except pymongo.errors.OperationFailure as err:
-            message = "Could not delete document (%s)" % err.message
+            message = "Could not delete document (%s)" % err.args
             raise OperationError(message)
         signals.post_delete.send(self.__class__, document=self, **signal_kwargs)
 


Test patch:
Repository: MongoEngine/mongoengine

Issue:
Migrate from travis to github-actions for the CI
This is an ongoing project but I started looking into migrating to github-actions as TravisCI became very disappointing and Github-actions is looking very promising.

Pros of migrating:
- integrated to github
- jobs are spending less time in queues and are also running faster overall
- github-actions market place is very nice
- the workflows are very extensible

Work is ongoing but unfortunately, this is blocked as I need a new pypi token to be generated by the core team


Discussion:

Resolving patch:
diff --git a/docs/conf.py b/docs/conf.py
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -26,7 +26,7 @@
 
 # Add any Sphinx extension module names here, as strings. They can be extensions
 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
-extensions = ["sphinx.ext.autodoc", "sphinx.ext.todo"]
+extensions = ["sphinx.ext.autodoc", "sphinx.ext.todo", "readthedocs_ext.readthedocs"]
 
 # Add any paths that contain templates here, relative to this directory.
 templates_path = ["_templates"]
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -112,7 +112,7 @@ def run_tests(self):
     "tests_require": [
         "pytest<5.0",
         "pytest-cov",
-        "coverage<5.0",  # recent coverage switched to sqlite format for the .coverage file which isn't handled properly by coveralls
+        "coverage",
         "blinker",
         "Pillow>=7.0.0",
     ],


Test patch:
Repository: MongoEngine/mongoengine

Issue:
When will be new release?
When are you planning a new release? Some features has been added since last one in May. Thanks!


Discussion:
Hi @mas15 , I don't have any particular date in mind but I'm currently working on migrating from travisCI to github-actions and so I'll take the opportunity to do this release from github-actions.
I guess I should get there in a few days. Is that acceptable for you?
Yrs, sure, good luck with github actions ;) 

Resolving patch:
diff --git a/mongoengine/__init__.py b/mongoengine/__init__.py
--- a/mongoengine/__init__.py
+++ b/mongoengine/__init__.py
@@ -28,7 +28,7 @@
 )
 
 
-VERSION = (0, 20, 0)
+VERSION = (0, 21, 0)
 
 
 def get_version():


Test patch:
Repository: MongoEngine/mongoengine

Issue:
Version restriction on pillow
Do we still need to restrict pillow to less then 7.0.0? This looks to have been implemented because of python2 support, which mongoengine dropped with version 0.20.0

https://github.com/MongoEngine/mongoengine/blob/277b827d4dab4630145bc747fdab0df48a045273/setup.py#L118


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -115,7 +115,7 @@ def run_tests(self):
         "pytest-cov",
         "coverage<5.0",  # recent coverage switched to sqlite format for the .coverage file which isn't handled properly by coveralls
         "blinker",
-        "Pillow>=2.0.0, <7.0.0",  # 7.0.0 dropped Python2 support
+        "Pillow>=7.0.0",
     ],
 }
 


Test patch:
diff --git a/tests/fields/test_file_field.py b/tests/fields/test_file_field.py
--- a/tests/fields/test_file_field.py
+++ b/tests/fields/test_file_field.py
@@ -429,7 +429,7 @@ class TestFile(Document):
     @require_pil
     def test_image_field_resize(self):
         class TestImage(Document):
-            image = ImageField(size=(185, 37))
+            image = ImageField(size=(185, 37, True))
 
         TestImage.drop_collection()
 
@@ -471,7 +471,7 @@ class TestImage(Document):
     @require_pil
     def test_image_field_thumbnail(self):
         class TestImage(Document):
-            image = ImageField(thumbnail_size=(92, 18))
+            image = ImageField(thumbnail_size=(92, 18, True))
 
         TestImage.drop_collection()
Repository: MongoEngine/mongoengine

Issue:
Manually setting Sequence Field in DynamicDocument increments the counter
Dynamic Document __setattr__ checks existance of the field with hasattr that calls __get__ of sequencefield that runs generate that increments the counter. 

This works as expected with Document.

For example this increments the counter:

```
class Test(DynamicDocument):
   id = SequenceField()

test = Test()
test.id = 123
```






Discussion:

Resolving patch:
diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py
--- a/mongoengine/base/document.py
+++ b/mongoengine/base/document.py
@@ -156,7 +156,7 @@ def __setattr__(self, name, value):
         # Handle dynamic data only if an initialised dynamic document
         if self._dynamic and not self._dynamic_lock:
 
-            if not hasattr(self, name) and not name.startswith("_"):
+            if name not in self._fields_ordered and not name.startswith("_"):
                 DynamicField = _import_class("DynamicField")
                 field = DynamicField(db_field=name, null=True)
                 field.name = name


Test patch:
diff --git a/tests/fields/test_sequence_field.py b/tests/fields/test_sequence_field.py
--- a/tests/fields/test_sequence_field.py
+++ b/tests/fields/test_sequence_field.py
@@ -274,3 +274,25 @@ class Bar(Base):
         assert foo.counter == bar.counter
         assert foo._fields["counter"].owner_document == Foo
         assert bar._fields["counter"].owner_document == Bar
+
+    def test_sequence_setattr_not_incrementing_counter(self):
+        class Person(DynamicDocument):
+            id = SequenceField(primary_key=True)
+            name = StringField()
+
+        self.db["mongoengine.counters"].drop()
+        Person.drop_collection()
+
+        for x in range(10):
+            Person(name="Person %s" % x).save()
+
+        c = self.db["mongoengine.counters"].find_one({"_id": "person.id"})
+        assert c["next"] == 10
+
+        # Setting SequenceField field value should not increment counter:
+        new_person = Person()
+        new_person.id = 1100
+
+        # Counter should still be at 10
+        c = self.db["mongoengine.counters"].find_one({"_id": "person.id"})
+        assert c["next"] == 10
Repository: MongoEngine/mongoengine

Issue:
Improve doc one-to-many-with-listfields
In the [documentation](https://docs.mongoengine.org/guide/defining-documents.html#one-to-many-with-listfields), why is the relation defined as a **one-to-many**?

As the example suggests, _John_ has two pages and _Test Page_ has two authors.

```
class User(Document):
    name = StringField()

class Page(Document):
    content = StringField()
    authors = ListField(ReferenceField(User))

bob = User(name="Bob Jones").save()
john = User(name="John Smith").save()

Page(content="Test Page", authors=[bob, john]).save()
Page(content="Another Page", authors=[john]).save()

# Find all pages Bob authored
Page.objects(authors__in=[bob])

# Find all pages that both Bob and John have authored
Page.objects(authors__all=[bob, john])

# Remove Bob from the authors for a page.
Page.objects(id='...').update_one(pull__authors=bob)

# Add John to the authors for a page.
Page.objects(id='...').update_one(push__authors=john)
```
Shouldn't it be qualified as a **many-to-many** relation?
It confused me, but maybe I am missing something...


Discussion:

Resolving patch:
diff --git a/mongoengine/fields.py b/mongoengine/fields.py
--- a/mongoengine/fields.py
+++ b/mongoengine/fields.py
@@ -915,7 +915,7 @@ class ListField(ComplexBaseField):
     """A list field that wraps a standard field, allowing multiple instances
     of the field to be used as a list in the database.
 
-    If using with ReferenceFields see: :ref:`one-to-many-with-listfields`
+    If using with ReferenceFields see: :ref:`many-to-many-with-listfields`
 
     .. note::
         Required means it cannot be empty - as the default for ListFields is []


Test patch:
Repository: MongoEngine/mongoengine

Issue:
the second argument type is wrong when invoking isinstance()
code file: /mongoengine/queryset/base.py
line num: 328
argument: self._document

the self ._document is an object NOT a TYPE, i guess, you want to pass the variable  Document.


Discussion:
I havn't checked but this is not a great bug description, would you mind elaborating? Perhaps explain the bug you've observed in the first place and provide a reproducible code snippet to demonstrate the problem? Thanks
My code like this:


class MyDocument(Document):
...


object = MyDocument()
object._qs().insert(doc_or_docs)


the error message is :
    if not isinstance(doc, self._document):
TypeError: isinstance() arg 2 must be a type or tuple of types

should i call the insert function by an object ??
![2021412-205927](https://user-images.githubusercontent.com/3401642/114398102-fd980280-9bd1-11eb-9b6b-9336137bffa7.png)

file: mongoengine/document.py
line: 565

self.__objects = QuerySet(self, self._get_collection())

this first parameter should not be self, because self is an object not class.

Resolving patch:
diff --git a/mongoengine/document.py b/mongoengine/document.py
--- a/mongoengine/document.py
+++ b/mongoengine/document.py
@@ -574,7 +574,7 @@ def cascade_save(self, **kwargs):
     def _qs(self):
         """Return the default queryset corresponding to this document."""
         if not hasattr(self, "__objects"):
-            self.__objects = QuerySet(self, self._get_collection())
+            self.__objects = QuerySet(self.__class__, self._get_collection())
         return self.__objects
 
     @property


Test patch:
diff --git a/tests/document/test_instance.py b/tests/document/test_instance.py
--- a/tests/document/test_instance.py
+++ b/tests/document/test_instance.py
@@ -407,6 +407,16 @@ def test_creation(self):
         assert person.name == "Test User"
         assert person.age == 30
 
+    def test__qs_property_does_not_raise(self):
+        # ensures no regression of #2500
+        class MyDocument(Document):
+            pass
+
+        MyDocument.drop_collection()
+        object = MyDocument()
+        object._qs().insert([MyDocument()])
+        assert MyDocument.objects.count() == 1
+
     def test_to_dbref(self):
         """Ensure that you can get a dbref of a document."""
         person = self.Person(name="Test User", age=30)
Repository: MongoEngine/mongoengine

Issue:
cls.compare_index() and cls.ensure_indexes() not picking up the right ttl syntax and hence it end up returning the compound index rather than ttl index
The issue seems to be with the "list_index()" is building up the list of required index. It only acquires fields and than the comparison only includes list of fields thus if we looped on in and created index than it would end up creating the compound index rather than ttl index.

This is also true for ensure_indexes. It has the identical behavior and will miss the TTL index creation.


Discussion:
I had a look into this and here are my thoughts
- TTL index is a special kind of single-field index, it can't be used with compound index and will be silently ignored (https://docs.mongodb.com/manual/core/index-ttl/#restrictions)
- You can't build a TTL index and another single-field index on the same field (thus 2 index on same keys but that differs only in the options), pymongo/MongoDB won't let you do that
- ensure_indexes is not missing the TTL index, `expireAfterSeconds` is being taken into account in the index options (`opts` in ensure_indexes method)

If you need to create the indexes, use ensure_indexes, it will indeed not work if you try to loop over list_indexes.

I agree that list_indexes is confusing, I'll improve its docstring to make that clearer but apart from that I don't see how it could lead to additional problems or inconsistencies in the indexes.

Resolving patch:
diff --git a/mongoengine/document.py b/mongoengine/document.py
--- a/mongoengine/document.py
+++ b/mongoengine/document.py
@@ -875,6 +875,10 @@ def ensure_indexes(cls):
 
         Global defaults can be set in the meta - see :doc:`guide/defining-documents`
 
+        By default, this will get called automatically upon first interaction with the
+        Document collection (query, save, etc) so unless you disabled `auto_create_index`, you
+        shouldn't have to call this manually.
+
         .. note:: You can disable automatic index creation by setting
                   `auto_create_index` to False in the documents meta data
         """
@@ -924,8 +928,10 @@ def ensure_indexes(cls):
 
     @classmethod
     def list_indexes(cls):
-        """Lists all of the indexes that should be created for given
-        collection. It includes all the indexes from super- and sub-classes.
+        """Lists all indexes that should be created for the Document collection.
+        It includes all the indexes from super- and sub-classes.
+
+        Note that it will only return the indexes' fields, not the indexes' options
         """
         if cls._meta.get("abstract"):
             return []


Test patch:
Repository: MongoEngine/mongoengine

Issue:
is it possible to search null objects ?
I'm trying to search `null object` with python `NoneType` but got the below error : 

`raise ValidationError(message, errors=errors, field_name=field_name)
mongoengine.errors.ValidationError: 'null' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string`


Discussion:
#### Code for reproducing this issue:

<pre>class User(mongoengine.Document):
meta = {
    "db_alias": 'core',
    "collection": 'so_users_details'
}
user_name = mongoengine.StringField(required=True)
tag_details = mongoengine.ObjectIdField(null=True)

User(user_name='Sam').save() 
User.objects().filter(tag_details=None)</pre>
does it make sense to add a null check [here ](https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/base/fields.py) in method `to_mongo` in class ` ObjectIdField` ?

Resolving patch:
diff --git a/mongoengine/base/fields.py b/mongoengine/base/fields.py
--- a/mongoengine/base/fields.py
+++ b/mongoengine/base/fields.py
@@ -511,7 +511,8 @@ def to_python(self, value):
     def to_mongo(self, value):
         if not isinstance(value, ObjectId):
             try:
-                return ObjectId(str(value))
+                if value:
+                    return ObjectId(str(value))
             except Exception as e:
                 self.error(str(e))
         return value


Test patch:
Repository: MongoEngine/mongoengine

Issue:
ImportError: cannot import name '_CaseInsensitiveDictionary' from 'pymongo.common'
mongoengine               0.28.1
pymongo                   3.7.2


File "C:\Users\usead\anaconda3\envs\qlib\lib\site-packages\mongoengine\__init__.py", line 2, in <module>
    from mongoengine import (
  File "C:\Users\usead\anaconda3\envs\qlib\lib\site-packages\mongoengine\connection.py", line 4, in <module>
    from pymongo.common import (
ImportError: cannot import name '_CaseInsensitiveDictionary' from 'pymongo.common' (C:\Users\usead\anaconda3\envs\qlib\lib\site-packages\pymongo\common.py)


Discussion:

Resolving patch:
diff --git a/mongoengine/__init__.py b/mongoengine/__init__.py
--- a/mongoengine/__init__.py
+++ b/mongoengine/__init__.py
@@ -29,7 +29,7 @@
 )
 
 
-VERSION = (0, 28, 1)
+VERSION = (0, 28, 2)
 
 
 def get_version():
diff --git a/mongoengine/connection.py b/mongoengine/connection.py
--- a/mongoengine/connection.py
+++ b/mongoengine/connection.py
@@ -1,10 +1,7 @@
 import warnings
 
 from pymongo import MongoClient, ReadPreference, uri_parser
-from pymongo.common import (
-    _UUID_REPRESENTATIONS,
-    _CaseInsensitiveDictionary,
-)
+from pymongo.common import _UUID_REPRESENTATIONS
 from pymongo.database import _check_name
 
 # DriverInfo was added in PyMongo 3.7.
@@ -135,7 +132,9 @@ def _get_connection_settings(
                 if uri_dict.get(param):
                     conn_settings[param] = uri_dict[param]
 
-            uri_options: _CaseInsensitiveDictionary = uri_dict["options"]
+            uri_options = uri_dict[
+                "options"
+            ]  # uri_options is a _CaseInsensitiveDictionary
             if "replicaset" in uri_options:
                 conn_settings["replicaSet"] = uri_options["replicaset"]
             if "authsource" in uri_options:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -98,8 +98,6 @@ def run_tests(self):
     "Operating System :: OS Independent",
     "Programming Language :: Python",
     "Programming Language :: Python :: 3",
-    "Programming Language :: Python :: 3.6",
-    "Programming Language :: Python :: 3.7",
     "Programming Language :: Python :: 3.8",
     "Programming Language :: Python :: 3.9",
     "Programming Language :: Python :: 3.10",


Test patch:
Repository: engnadeau/pybotics

Issue:
Update examples
Examples are now out of sync with current codebase. Potential use for iPython?


Discussion:

Resolving patch:
diff --git a/examples/example_robot.py b/examples/example_robot.py
deleted file mode 100644
--- a/examples/example_robot.py
+++ /dev/null
@@ -1,24 +0,0 @@
-import copy
-
-import pybotics as py
-import numpy as np
-
-# set numpy print options
-np.set_printoptions(precision=3)
-np.set_printoptions(suppress=True)
-
-# create robot
-model = np.loadtxt('ur10-mdh.csv', delimiter=',')
-robot = py.Robot(model)
-
-print('Robot Model:\n{}\n'.format(robot.robot_model))
-
-# demonstrate forward kinematics
-joints = [0] * robot.num_dof()
-pose = robot.fk(joints)
-
-print('Pose:\n{}\n'.format(pose))
-
-# demonstrate inverse kinematics
-new_joints = robot.ik(pose)
-print('Solved Joints:\n{}\n'.format(new_joints))


Test patch: