nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: digitalocean/pynetbox
Pull request #129 (2018-12-08 03:39:51)

Fixes IndexError when field is empty list.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 612bfea..78116ff 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,4 +1,10 @@
 
+v4.0.4 (2018-12-07)
+
+## Bug Fixes
+* Fixes IndexError introduced by fix for #127 when field is empty list.
+
+---
 v4.0.3 (2018-12-07)
 
 ## Bug Fixes
diff --git a/pynetbox/core/response.py b/pynetbox/core/response.py
index 4c70a5c..b1a517c 100644
--- a/pynetbox/core/response.py
+++ b/pynetbox/core/response.py
@@ -183,8 +183,8 @@ class Record(object):
             cur_attr = getattr(self, i)
             if isinstance(cur_attr, Record):
                 yield i, dict(cur_attr)
-            elif isinstance(cur_attr, list) and isinstance(
-                cur_attr[0], Record
+            elif isinstance(cur_attr, list) and all(
+                isinstance(i, Record) for i in cur_attr
             ):
                 yield i, [dict(x) for x in cur_attr]
             else:


Test patch:
diff --git a/tests/unit/test_response.py b/tests/unit/test_response.py
index 833c407..a996d06 100644
--- a/tests/unit/test_response.py
+++ b/tests/unit/test_response.py
@@ -99,6 +99,7 @@ class RecordTestCase(unittest.TestCase):
                 321,
                 231,
             ],
+            'empty_list': [],
             'record_list': [
                 {
                     'id': 123,
Repository: digitalocean/pynetbox
Pull request #201 (2019-11-18 20:49:31)

Add method to return custom choices
This implements a method for `/_custom_field_choices` for `extras` app (#200). Example:

```
>>> nb.extras.custom_choices()
{'Testfield': {'Test2': 2, 'Test1': 1}, 'Testfield2': {'TF2': 4, 'TF1': 3}}
```

Some thoughts:

If called with other app than `extras`, the standard `RequestError` will be raised (because of 404 error):

```
>>> nb.dcim.custom_choices()
Traceback (most recent call last):
...
pynetbox.core.query.RequestError: The requested url: http://netbox.example.local/api/dcim/_custom_field_choices/ could not be found.
```
If desired, specific trigger for `NotImplementedError` can be implemented (`if self.name != "extras" ...`), I just felt keeping this simple after all.

No caching is done (`self._custom_field_choices` etc) because then editing the custom field choices in NetBox would require restarting the application using `pynetbox` (or handling the cache flush some other way).

Some unit test is still needed.


Patch:
diff --git a/pynetbox/api.py b/pynetbox/api.py
index a9fce2c..b658444 100644
--- a/pynetbox/api.py
+++ b/pynetbox/api.py
@@ -78,6 +78,29 @@ class App(object):
 
         return self._choices
 
+    def custom_choices(self):
+        """ Returns _custom_field_choices response from app
+
+        :Returns: Raw response from NetBox's _custom_field_choices endpoint.
+        :Raises: :py:class:`.RequestError` if called for an invalid endpoint.
+        :Example:
+
+        >>> nb.extras.custom_choices()
+        {'Testfield1': {'Testvalue2': 2, 'Testvalue1': 1},
+         'Testfield2': {'Othervalue2': 4, 'Othervalue1': 3}}
+        """
+        custom_field_choices = Request(
+            base="{}/{}/_custom_field_choices/".format(
+                self.api.base_url,
+                self.name,
+            ),
+            token=self.api.token,
+            private_key=self.api.private_key,
+            ssl_verify=self.api.ssl_verify,
+            http_session=self.api.http_session,
+        ).get()
+        return custom_field_choices
+
 
 class Api(object):
     """ The API object is the point of entry to pynetbox.


Test patch:
diff --git a/tests/test_app.py b/tests/test_app.py
new file mode 100644
index 0000000..237ad38
--- /dev/null
+++ b/tests/test_app.py
@@ -0,0 +1,35 @@
+import unittest
+
+import six
+
+import pynetbox
+
+if six.PY3:
+    from unittest.mock import patch
+else:
+    from mock import patch
+
+host = "http://localhost:8000"
+
+def_kwargs = {
+    'token': 'abc123',
+}
+
+
+class AppCustomChoicesTestCase(unittest.TestCase):
+
+    @patch(
+        'pynetbox.core.query.Request.get',
+        return_value={
+            "Testfield1": {"TF1_1": 1, "TF1_2": 2},
+            "Testfield2": {"TF2_1": 3, "TF2_2": 4},
+        }
+    )
+    def test_custom_choices(self, *_):
+        api = pynetbox.api(
+            host,
+            **def_kwargs
+        )
+        choices = api.extras.custom_choices()
+        self.assertEqual(len(choices), 2)
+        self.assertEqual(sorted(choices.keys()), ["Testfield1", "Testfield2"])
Repository: digitalocean/pynetbox
Pull request #271 (2020-07-15 19:39:32)

Fix tag ordering
Fixes issue with tag order sometimes being modified in serialize() when we cast to set(). Which caused save() to make unnecessary calls to NetBox.


Patch:
diff --git a/pynetbox/core/response.py b/pynetbox/core/response.py
index 29e124d..49d9dc5 100644
--- a/pynetbox/core/response.py
+++ b/pynetbox/core/response.py
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
 limitations under the License.
 """
 import copy
+from collections import OrderedDict
 
 import pynetbox.core.app
 from six.moves.urllib.parse import urlsplit
@@ -341,7 +342,7 @@ class Record(object):
                         v.id if isinstance(v, Record) else v for v in current_val
                     ]
                     if i in LIST_AS_SET:
-                        current_val = list(set(current_val))
+                        current_val = list(OrderedDict.fromkeys(current_val))
                 ret[i] = current_val
         return ret
 


Test patch:
diff --git a/tests/unit/test_response.py b/tests/unit/test_response.py
index f37d430..dddd32e 100644
--- a/tests/unit/test_response.py
+++ b/tests/unit/test_response.py
@@ -209,3 +209,25 @@ class RecordTestCase(unittest.TestCase):
         )
         ret = test._endpoint_from_url(test.url)
         self.assertEqual(ret.name, "test-endpoint")
+
+    def test_serialize_tag_list_order(self):
+        """Add tests to ensure we're preserving tag order
+
+        This test could still give false-negatives, but making the tag list
+        longer helps mitigate that.
+        """
+
+        test_tags = [
+            "one",
+            "two",
+            "three",
+            "four",
+            "five",
+            "six",
+            "seven",
+            "eight",
+            "nine",
+            "ten",
+        ]
+        test = Record({"id": 123, "tags": test_tags}, None, None).serialize()
+        self.assertEqual(test["tags"], test_tags)