Skip to content

Commit b9ca82c

Browse files
adehadsvermeulen
authored andcommitted
use requests.structures.CaseInsensitiveDict directly (pycontribs#1084)
* use requests.structures.CaseInsensitiveDict directly * add tests for attachment using filename
1 parent 1cfe8e1 commit b9ca82c

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

docs/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
("py:class", "Request"),
6868
("py:class", "requests.models.Response"),
6969
("py:class", "requests.sessions.Session"),
70+
("py:class", "requests.structures.CaseInsensitiveDict"),
7071
("py:class", "Response"),
7172
("py:mod", "requests-kerberos"),
7273
("py:mod", "requests-oauthlib"),

jira/client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from pkg_resources import parse_version
4545
from requests import Response
4646
from requests.auth import AuthBase
47+
from requests.structures import CaseInsensitiveDict
4748
from requests.utils import get_netrc_auth
4849

4950
from jira import __version__
@@ -86,7 +87,7 @@
8687
Watchers,
8788
Worklog,
8889
)
89-
from jira.utils import CaseInsensitiveDict, json_loads, threaded_requests
90+
from jira.utils import json_loads, threaded_requests
9091

9192
try:
9293
# noinspection PyUnresolvedReferences

jira/resources.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union, cast
1212

1313
from requests import Response
14+
from requests.structures import CaseInsensitiveDict
1415

1516
from jira.resilientsession import ResilientSession
16-
from jira.utils import CaseInsensitiveDict, json_loads, threaded_requests
17+
from jira.utils import json_loads, threaded_requests
1718

1819
if TYPE_CHECKING:
1920
from jira.client import JIRA

jira/utils/__init__.py

+10-27
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# -*- coding: utf-8 -*-
22
"""Jira utils used internally."""
33
import threading
4+
import warnings
45
from typing import Any, Optional, cast
56

67
from requests import Response
8+
from requests.structures import CaseInsensitiveDict as _CaseInsensitiveDict
79

810
from jira.resilientsession import raise_on_error
911

1012

11-
class CaseInsensitiveDict(dict):
13+
class CaseInsensitiveDict(_CaseInsensitiveDict):
1214
"""A case-insensitive ``dict``-like object.
1315
16+
DEPRECATED: use requests.structures.CaseInsensitiveDict directly.
17+
1418
Implements all methods and operations of
1519
``collections.MutableMapping`` as well as dict's ``copy``. Also
1620
provides ``lower_items``.
@@ -36,32 +40,11 @@ class CaseInsensitiveDict(dict):
3640
3741
"""
3842

39-
def __init__(self, *args, **kw):
40-
super(CaseInsensitiveDict, self).__init__(*args, **kw)
41-
42-
self.itemlist = {}
43-
for key, value in super(CaseInsensitiveDict, self).copy().items():
44-
if key != key.lower():
45-
self[key.lower()] = value
46-
self.pop(key, None)
47-
48-
# self.itemlist[key.lower()] = value
49-
50-
def __setitem__(self, key, value):
51-
"""Overwrite [] implementation."""
52-
super(CaseInsensitiveDict, self).__setitem__(key.lower(), value)
53-
54-
# def __iter__(self):
55-
# return iter(self.itemlist)
56-
57-
# def keys(self):
58-
# return self.itemlist
59-
60-
# def values(self):
61-
# return [self[key] for key in self]
62-
63-
# def itervalues(self):
64-
# return (self[key] for key in self)
43+
def __init__(self, *args, **kwargs) -> None:
44+
warnings.warn(
45+
"Use requests.structures.CaseInsensitiveDict directly", DeprecationWarning
46+
)
47+
super().__init__(*args, **kwargs)
6548

6649

6750
def threaded_requests(requests):

tests/resources/test_attachment.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_0_attachment_meta(self):
1515
# we have no control over server side upload limit
1616
self.assertIn("uploadLimit", meta)
1717

18-
def test_1_add_remove_attachment(self):
18+
def test_1_add_remove_attachment_using_filestream(self):
1919
issue = self.jira.issue(self.issue_1)
2020
with open(TEST_ATTACH_PATH, "rb") as f:
2121
attachment = self.jira.add_attachment(issue, f, "new test attachment")
@@ -27,3 +27,17 @@ def test_1_add_remove_attachment(self):
2727
)
2828
# JIRA returns a HTTP 204 upon successful deletion
2929
self.assertEqual(attachment.delete().status_code, 204)
30+
31+
def test_2_add_remove_attachment_using_filename(self):
32+
issue = self.jira.issue(self.issue_1)
33+
attachment = self.jira.add_attachment(
34+
issue, TEST_ATTACH_PATH, "new test attachment"
35+
)
36+
new_attachment = self.jira.attachment(attachment.id)
37+
msg = "attachment %s of issue %s" % (new_attachment.__dict__, issue)
38+
self.assertEqual(new_attachment.filename, "new test attachment", msg=msg)
39+
self.assertEqual(
40+
new_attachment.size, os.path.getsize(TEST_ATTACH_PATH), msg=msg
41+
)
42+
# JIRA returns a HTTP 204 upon successful deletion
43+
self.assertEqual(attachment.delete().status_code, 204)

0 commit comments

Comments
 (0)