From b9ca82cc0d82c0a019a9d961598bd3aff4b3baa2 Mon Sep 17 00:00:00 2001 From: adehad <26027314+adehad@users.noreply.github.com> Date: Fri, 23 Jul 2021 19:05:55 +0100 Subject: [PATCH] use requests.structures.CaseInsensitiveDict directly (#1084) * use requests.structures.CaseInsensitiveDict directly * add tests for attachment using filename --- docs/conf.py | 1 + jira/client.py | 3 ++- jira/resources.py | 3 ++- jira/utils/__init__.py | 37 ++++++++---------------------- tests/resources/test_attachment.py | 16 ++++++++++++- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 058643cf1..33839d3f8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -67,6 +67,7 @@ ("py:class", "Request"), ("py:class", "requests.models.Response"), ("py:class", "requests.sessions.Session"), + ("py:class", "requests.structures.CaseInsensitiveDict"), ("py:class", "Response"), ("py:mod", "requests-kerberos"), ("py:mod", "requests-oauthlib"), diff --git a/jira/client.py b/jira/client.py index 48fac8663..33038126c 100644 --- a/jira/client.py +++ b/jira/client.py @@ -44,6 +44,7 @@ from pkg_resources import parse_version from requests import Response from requests.auth import AuthBase +from requests.structures import CaseInsensitiveDict from requests.utils import get_netrc_auth from jira import __version__ @@ -86,7 +87,7 @@ Watchers, Worklog, ) -from jira.utils import CaseInsensitiveDict, json_loads, threaded_requests +from jira.utils import json_loads, threaded_requests try: # noinspection PyUnresolvedReferences diff --git a/jira/resources.py b/jira/resources.py index faf43b3f0..3b3f58380 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -11,9 +11,10 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union, cast from requests import Response +from requests.structures import CaseInsensitiveDict from jira.resilientsession import ResilientSession -from jira.utils import CaseInsensitiveDict, json_loads, threaded_requests +from jira.utils import json_loads, threaded_requests if TYPE_CHECKING: from jira.client import JIRA diff --git a/jira/utils/__init__.py b/jira/utils/__init__.py index 86052b949..ac468d552 100644 --- a/jira/utils/__init__.py +++ b/jira/utils/__init__.py @@ -1,16 +1,20 @@ # -*- coding: utf-8 -*- """Jira utils used internally.""" import threading +import warnings from typing import Any, Optional, cast from requests import Response +from requests.structures import CaseInsensitiveDict as _CaseInsensitiveDict from jira.resilientsession import raise_on_error -class CaseInsensitiveDict(dict): +class CaseInsensitiveDict(_CaseInsensitiveDict): """A case-insensitive ``dict``-like object. + DEPRECATED: use requests.structures.CaseInsensitiveDict directly. + Implements all methods and operations of ``collections.MutableMapping`` as well as dict's ``copy``. Also provides ``lower_items``. @@ -36,32 +40,11 @@ class CaseInsensitiveDict(dict): """ - def __init__(self, *args, **kw): - super(CaseInsensitiveDict, self).__init__(*args, **kw) - - self.itemlist = {} - for key, value in super(CaseInsensitiveDict, self).copy().items(): - if key != key.lower(): - self[key.lower()] = value - self.pop(key, None) - - # self.itemlist[key.lower()] = value - - def __setitem__(self, key, value): - """Overwrite [] implementation.""" - super(CaseInsensitiveDict, self).__setitem__(key.lower(), value) - - # def __iter__(self): - # return iter(self.itemlist) - - # def keys(self): - # return self.itemlist - - # def values(self): - # return [self[key] for key in self] - - # def itervalues(self): - # return (self[key] for key in self) + def __init__(self, *args, **kwargs) -> None: + warnings.warn( + "Use requests.structures.CaseInsensitiveDict directly", DeprecationWarning + ) + super().__init__(*args, **kwargs) def threaded_requests(requests): diff --git a/tests/resources/test_attachment.py b/tests/resources/test_attachment.py index 667a39afd..a823f0053 100644 --- a/tests/resources/test_attachment.py +++ b/tests/resources/test_attachment.py @@ -15,7 +15,7 @@ def test_0_attachment_meta(self): # we have no control over server side upload limit self.assertIn("uploadLimit", meta) - def test_1_add_remove_attachment(self): + def test_1_add_remove_attachment_using_filestream(self): issue = self.jira.issue(self.issue_1) with open(TEST_ATTACH_PATH, "rb") as f: attachment = self.jira.add_attachment(issue, f, "new test attachment") @@ -27,3 +27,17 @@ def test_1_add_remove_attachment(self): ) # JIRA returns a HTTP 204 upon successful deletion self.assertEqual(attachment.delete().status_code, 204) + + def test_2_add_remove_attachment_using_filename(self): + issue = self.jira.issue(self.issue_1) + attachment = self.jira.add_attachment( + issue, TEST_ATTACH_PATH, "new test attachment" + ) + new_attachment = self.jira.attachment(attachment.id) + msg = "attachment %s of issue %s" % (new_attachment.__dict__, issue) + self.assertEqual(new_attachment.filename, "new test attachment", msg=msg) + self.assertEqual( + new_attachment.size, os.path.getsize(TEST_ATTACH_PATH), msg=msg + ) + # JIRA returns a HTTP 204 upon successful deletion + self.assertEqual(attachment.delete().status_code, 204)