From a36247e1486bad737cebaa067024bcc6c55295c8 Mon Sep 17 00:00:00 2001 From: Allan Crooks Date: Tue, 12 Sep 2017 00:08:26 +0100 Subject: [PATCH] Rename HTTPHeaderDict methods to getlist and setlist to be more consistent with other similar implementations. --- docs/user/quickstart.rst | 2 +- requests/structures.py | 14 ++++++-------- tests/test_requests.py | 8 ++++---- tests/test_structures.py | 38 +++++++++++++++++++------------------- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 3f31aa8d..84287cc2 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -419,7 +419,7 @@ represented in the dictionary within a single mapping, as per field value in order, separated by a comma. If you do need to access each individual value sent with the same header, then -you can use the ``multiget`` method to get a sequence of all the values returned +you can use the ``getlist`` method to get a sequence of all the values returned for a particular header. Cookies diff --git a/requests/structures.py b/requests/structures.py index df03538d..5cbd8f6d 100644 --- a/requests/structures.py +++ b/requests/structures.py @@ -118,12 +118,12 @@ class HTTPHeaderDict(CaseInsensitiveDict): def copy(self): return type(self)(self) - def multiget(self, key): - """Returns a tuple of all the values for the named field. Returns an - empty tuple if the key isn't present in the dictionary.""" - return self._store.get(key.lower(), (None, ()))[1] + def getlist(self, key): + """Returns a list of all the values for the named field. Returns an + empty list if the key isn't present in the dictionary.""" + return list(self._store.get(key.lower(), (None, []))[1]) - def multiset(self, key, values): + def setlist(self, key, values): """Set a sequence of strings to the associated key - this will overwrite any previously stored value.""" if not isinstance(values, (list, tuple)): @@ -168,9 +168,7 @@ class HTTPHeaderDict(CaseInsensitiveDict): # See if looks like a HTTPHeaderDict (either urllib3's # implementation or ours). If so, then we have to add values # in one go for each key. - multiget = getattr(other, 'multiget', None) - if not multiget: - multiget = getattr(other, 'getlist', None) + multiget = getattr(other, 'getlist', None) if multiget: for key in other: self._extend(key, tuple(multiget(key))) diff --git a/tests/test_requests.py b/tests/test_requests.py index 8f5fc796..ffe8fe25 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -2188,9 +2188,9 @@ class TestRequests: # As we are using HTTPHeaderDict, we should be able to extract the # individual header values too. - assert resp.headers.multiget('fruit') == ( + assert resp.headers.getlist('fruit') == [ 'Apple', 'Blood Orange', 'Banana', 'Berry, Blue' - ) + ] def test_multiple_response_headers_with_same_name_diff_case(self, httpbin): # urllib3 seems to have trouble guaranteeing the order of the items when @@ -2202,13 +2202,13 @@ class TestRequests: # These are all possible acceptable combinations for the header. fruit_choices = ['Apple', 'Blood Orange', 'Banana', 'Berry, Blue'] fruit_permutations = itertools.permutations(fruit_choices) - fruit_multiheaders = set(tuple(fp) for fp in fruit_permutations) + fruit_multiheaders = [list(fp) for fp in fruit_permutations] fruit_headers = set(', '.join(fp) for fp in fruit_multiheaders) assert resp.headers['fruit'] in fruit_headers # As we are using HTTPHeaderDict, we should be able to extract the # individual header values too. - assert resp.headers.multiget('fruit') in fruit_multiheaders + assert resp.headers.getlist('fruit') in fruit_multiheaders class TestCaseInsensitiveDict: diff --git a/tests/test_structures.py b/tests/test_structures.py index c7076314..65f6ecce 100644 --- a/tests/test_structures.py +++ b/tests/test_structures.py @@ -86,7 +86,7 @@ class TestHTTPHeaderDict: # equivalent to each other. self.extra_hd = hd2 = HTTPHeaderDict(ANIMAL=['Dog', 'elephant']) hd2['cake'] = 'Babka' - hd2.multiset('sound', ('quiet', 'LOUD')) + hd2.setlist('sound', ['quiet', 'LOUD']) hd2['CUTLERY'] = 'fork' self.extra_tuple_pairs = tuple_pairs = [ @@ -153,30 +153,30 @@ class TestHTTPHeaderDict: assert hd is not hd2 assert hd == hd2 - def test_multi_get_and_set(self): + def test_get_and_set_list(self): hd = HTTPHeaderDict(self.kvs) - assert hd.multiget('SAUCE') == ('Bread', 'Cherry, or Plum Tomato') - assert hd.multiget('CAKE') == ('Cheese!',) - assert hd.multiget('DRINK') == () + assert hd.getlist('SAUCE') == ['Bread', 'Cherry, or Plum Tomato'] + assert hd.getlist('CAKE') == ['Cheese!'] + assert hd.getlist('DRINK') == [] # Needs to be a regular sequence type containing just strings. - pytest.raises(ValueError, hd.multiset, 'Drink', 'Water') - pytest.raises(ValueError, hd.multiset, 'Drink', ['H', 2, 'O']) + pytest.raises(ValueError, hd.setlist, 'Drink', 'Water') + pytest.raises(ValueError, hd.setlist, 'Drink', ['H', 2, 'O']) # Test multi-setting. - hd.multiset('Drink', ['Water', 'Juice']) - assert hd.multiget('DRINK') == ('Water', 'Juice') + hd.setlist('Drink', ['Water', 'Juice']) + assert hd.getlist('DRINK') == ['Water', 'Juice'] # Setting to an empty sequence should remove the entry. - hd.multiset('DRInk', []) + hd.setlist('DRInk', []) pytest.raises(KeyError, hd.__getitem__, 'DrinK') - assert hd.multiget('DRiNK') == () + assert hd.getlist('DRiNK') == [] def test_add(self): hd = HTTPHeaderDict() hd.add('sound', 'quiet') hd.add('SOUND', 'LOUD') - assert hd.multiget('Sound') == ('quiet', 'LOUD') + assert hd.getlist('Sound') == ['quiet', 'LOUD'] # Enforce type-checking in the add method. pytest.raises(ValueError, hd.add, 'Sound', 5) @@ -206,29 +206,29 @@ class TestHTTPHeaderDict: hd.extend(extras, **item) # Test all the stored values are what we expect. - mget = hd.multiget + mget = hd.getlist # Depending on the item we merged in, we might be able to make # assumptions what the overall order of the structure is. animal_seq = mget('animal') if animal_arg_is_ordered: - assert animal_seq == ('chicken', 'Cow', 'Dog', 'elephant') + assert animal_seq == ['chicken', 'Cow', 'Dog', 'elephant'] else: # The existing order in HTTPHeadersDict of the first two values # should be preserved - no guarantees in which order the other # two values are added. assert animal_seq in [ - ('chicken', 'Cow', 'Dog', 'elephant'), - ('chicken', 'Cow', 'elephant', 'Dog') + ['chicken', 'Cow', 'Dog', 'elephant'], + ['chicken', 'Cow', 'elephant', 'Dog'] ] - assert mget('cake') == ('Cheese!', 'Babka') - assert mget('sound') == ('quiet', 'LOUD') + assert mget('cake') == ['Cheese!', 'Babka'] + assert mget('sound') == ['quiet', 'LOUD'] # We don't mandate the order in which these dictionaries are # processed, so it's fine whichever order it is. assert mget('cutlery') in [ - ('fork', 'knife'), ('knife', 'fork') + ['fork', 'knife'], ['knife', 'fork'] ] def test_extend_type_checking(self):