From e16fc666af1e5cd2d96bc7ff3192a56e9c23a336 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 15 Mar 2018 14:20:53 -0400 Subject: [PATCH] white / improvements Signed-off-by: Kenneth Reitz --- requests/__init__.py | 6 ++-- requests/models.py | 4 +-- tests/test_requests.py | 62 ++++++++++++++-------------------------- tests/test_structures.py | 41 ++++---------------------- 4 files changed, 32 insertions(+), 81 deletions(-) diff --git a/requests/__init__.py b/requests/__init__.py index 2435d503..b21f3908 100644 --- a/requests/__init__.py +++ b/requests/__init__.py @@ -63,9 +63,9 @@ def check_compatibility(urllib3_version: str, chardet_version: str) -> None: major, minor, patch = chardet_version.split('.')[:3] major, minor, patch = int(major), int(minor), int(patch) # chardet >= 3.0.2, < 3.1.0 - assert major == 3 - assert minor < 1 - assert patch >= 2 + assert major == 3 # type: ignore + assert minor < 1 # type: ignore + assert patch >= 2 # type: ignore def _check_cryptography(cryptography_version: str) -> None: diff --git a/requests/models.py b/requests/models.py index 09049b7a..4497271e 100644 --- a/requests/models.py +++ b/requests/models.py @@ -244,7 +244,6 @@ class Request(RequestHooksMixin): >>> req.prepare() """ - __slots__ = ( 'method', 'url', @@ -255,7 +254,7 @@ class Request(RequestHooksMixin): 'auth', 'cookies', 'hooks', - 'json' + 'json', ) def __init__( @@ -640,7 +639,6 @@ class Response(object): 'elapsed', 'request', ] - __slots__ = __attrs__ + ['_content_consumed', 'raw', '_next', 'connection'] def __init__(self): diff --git a/tests/test_requests.py b/tests/test_requests.py index b06b1ce8..1456cad6 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -2229,7 +2229,6 @@ class TestRequests: resp = requests.get(httpbin('response-headers?' + qs)) fruits = resp.headers['fruit'] assert fruits == 'Apple, Blood Orange, Banana, Berry, Blue' - # As we are using HTTPHeaderDict, we should be able to extract the # individual header values too. assert resp.headers.getlist('fruit') == [ @@ -2242,14 +2241,12 @@ class TestRequests: # are there, rather than asserting a particular order. qs = 'Fruit=Apple&Fruit=Blood+Orange&Fruit=Banana&Fruit=Berry,+Blue' resp = requests.get(httpbin('response-headers?' + qs)) - # 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 = [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.getlist('fruit') in fruit_multiheaders @@ -2752,20 +2749,16 @@ class TestPreparingURLs(object): @pytest.mark.parametrize( 'input, expected', - ( - # TODO: Bugs in rfc3986, apparently. - # ( - # b"http+unix://%2Fvar%2Frun%2Fsocket/path%7E", - # u"http+unix://%2Fvar%2Frun%2Fsocket/path~", - # ), - # ( - # u"http+unix://%2Fvar%2Frun%2Fsocket/path%7E", - # u"http+unix://%2Fvar%2Frun%2Fsocket/path~", - # ), - (b"mailto:user@example.org", u"mailto:user@example.org"), - (u"mailto:user@example.org", u"mailto:user@example.org"), - (b"data:SSDimaUgUHl0aG9uIQ==", u"data:SSDimaUgUHl0aG9uIQ=="), - ), + ((b"mailto:user@example.org", u"mailto:user@example.org"), (u"mailto:user@example.org", u"mailto:user@example.org"), (b"data:SSDimaUgUHl0aG9uIQ==", u"data:SSDimaUgUHl0aG9uIQ==")), + # TODO: Bugs in rfc3986, apparently. + # ( + # b"http+unix://%2Fvar%2Frun%2Fsocket/path%7E", + # u"http+unix://%2Fvar%2Frun%2Fsocket/path~", + # ), + # ( + # u"http+unix://%2Fvar%2Frun%2Fsocket/path%7E", + # u"http+unix://%2Fvar%2Frun%2Fsocket/path~", + # ), ) def test_url_mutation(self, input, expected): """ @@ -2780,29 +2773,18 @@ class TestPreparingURLs(object): @pytest.mark.parametrize( 'input, params, expected', - ( - # TODO: - # ( - # b"http+unix://%2Fvar%2Frun%2Fsocket/path", - # {"key": "value"}, - # u"http+unix://%2Fvar%2Frun%2Fsocket/path?key=value", - # ), - # ( - # u"http+unix://%2Fvar%2Frun%2Fsocket/path", - # {"key": "value"}, - # u"http+unix://%2Fvar%2Frun%2Fsocket/path?key=value", - # ), - ( - b"mailto:user@example.org", - {"key": "value"}, - u"mailto:user@example.org", - ), - ( - u"mailto:user@example.org", - {"key": "value"}, - u"mailto:user@example.org", - ), - ), + ((b"mailto:user@example.org", {"key": "value"}, u"mailto:user@example.org"), (u"mailto:user@example.org", {"key": "value"}, u"mailto:user@example.org")), + # TODO: + # ( + # b"http+unix://%2Fvar%2Frun%2Fsocket/path", + # {"key": "value"}, + # u"http+unix://%2Fvar%2Frun%2Fsocket/path?key=value", + # ), + # ( + # u"http+unix://%2Fvar%2Frun%2Fsocket/path", + # {"key": "value"}, + # u"http+unix://%2Fvar%2Frun%2Fsocket/path?key=value", + # ), ) def test_parameters_for_nonstandard_schemes(self, input, params, expected): """ diff --git a/tests/test_structures.py b/tests/test_structures.py index 5803079f..694b9619 100644 --- a/tests/test_structures.py +++ b/tests/test_structures.py @@ -53,7 +53,6 @@ class TestCaseInsensitiveDict: class TestHTTPHeaderDictCompatibility(TestCaseInsensitiveDict): - """HTTPHeaderDict should be completely compatible with CaseInsensitiveDict when used for headers, so ensure that all the tests for the base class also pass for this one.""" @@ -75,22 +74,18 @@ class TestHTTPHeaderDict: ('Sauce', 'Bread'), ('Sauce', 'Cherry, or Plum Tomato'), ] - # HTTPHeaderDict from urllib3. self.u3dict = ud = U3HeaderDict() [ud.add(*tpl) for tpl in self.kvs] - # Regular dictionary. self.ddict = dict(self.kvs) self.ddict['Sauce'] = ['Bread!', 'Cherry, or Plum Tomato'] - # Used by test_extend. All of these "extra" values are mostly # equivalent to each other. self.extra_hd = hd2 = HTTPHeaderDict(ANIMAL=['Dog', 'elephant']) hd2['cake'] = 'Babka' hd2.setlist('sound', ['quiet', 'LOUD']) hd2['CUTLERY'] = 'fork' - self.extra_tuple_pairs = tuple_pairs = [ ('ANIMAL', 'Dog'), ('Animal', 'elephant'), @@ -99,10 +94,8 @@ class TestHTTPHeaderDict: ('sound', 'LOUD'), ('CUTLERY', 'fork'), ] - self.extra_simple_dict = dict(tuple_pairs) self.extra_simple_dict['sound'] = ('quiet', 'LOUD') - self.extra_u3 = U3HeaderDict() for k, v in tuple_pairs: if isinstance(v, (tuple, list)): @@ -113,19 +106,15 @@ class TestHTTPHeaderDict: def test_item_access(self): hd = HTTPHeaderDict(self.kvs) - # Test that values are combined. assert hd['Sauce'] == 'Bread, Cherry, or Plum Tomato' assert hd['ANIMAL'] == 'chicken, Cow' - # Test we can overwrite values. hd['animal'] = 'Goat!' assert hd['anIMal'] == 'Goat!' - # Test deletion works. del hd['sauce'] pytest.raises(KeyError, hd.__getitem__, 'sauce') - # Only string types allowed. pytest.raises(ValueError, hd.__setitem__, 'cake', ['Cheese', 'sponge']) @@ -133,7 +122,6 @@ class TestHTTPHeaderDict: hd = HTTPHeaderDict(self.u3dict) assert hd == self.u3dict assert hd == HTTPHeaderDict(hd) - # Test that we still work even if we are comparing to a # CaseInsensitiveDict instance. cid = CaseInsensitiveDict(hd) @@ -160,15 +148,12 @@ class TestHTTPHeaderDict: 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.setlist, 'Drink', 'Water') pytest.raises(ValueError, hd.setlist, 'Drink', ['H', 2, 'O']) - # Test multi-setting. hd.setlist('Drink', ['Water', 'Juice']) assert hd.getlist('DRINK') == ['Water', 'Juice'] - # Setting to an empty sequence should remove the entry. hd.setlist('DRInk', []) pytest.raises(KeyError, hd.__getitem__, 'DrinK') @@ -179,37 +164,28 @@ class TestHTTPHeaderDict: hd.add('sound', 'quiet') hd.add('SOUND', 'LOUD') assert hd.getlist('Sound') == ['quiet', 'LOUD'] - # Enforce type-checking in the add method. pytest.raises(ValueError, hd.add, 'Sound', 5) - @pytest.mark.parametrize('attr,as_arg,animal_arg_is_ordered', [ + @pytest.mark.parametrize( + 'attr,as_arg,animal_arg_is_ordered', + [('extra_hd', True, True), ('extra_tuple_pairs', True, True), ('extra_simple_dict', True, False), ('extra_u3', True, False), ('extra_simple_dict', False, False)], # These types will have the "animal" arguments in our preferred order. - ('extra_hd', True, True), - ('extra_tuple_pairs', True, True), - # And these types will lose the ordering, so we can't make assertions # about the final order of those values. - ('extra_simple_dict', True, False), - ('extra_u3', True, False), - ('extra_simple_dict', False, False), - ]) + ) def test_extend(self, attr, as_arg, animal_arg_is_ordered): item = getattr(self, attr) - # Call extend with the associated values - we should see all of the # merged data in the HTTPHeaderDict instance. extras = {'cutlery': 'knife'} hd = HTTPHeaderDict(self.kvs) - if as_arg: hd.extend(item, **extras) else: hd.extend(extras, **item) - # Test all the stored values are what we expect. 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') @@ -221,17 +197,13 @@ class TestHTTPHeaderDict: # two values are added. assert animal_seq in [ ['chicken', 'Cow', 'Dog', 'elephant'], - ['chicken', 'Cow', 'elephant', 'Dog'] + ['chicken', 'Cow', 'elephant', 'Dog'], ] - 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'] - ] + assert mget('cutlery') in [['fork', 'knife'], ['knife', 'fork']] def test_extend_type_checking(self): hd = HTTPHeaderDict() @@ -244,7 +216,6 @@ class TestHTTPHeaderDict: assert repr(hd) == "{'type': 'xml'}" hd.add('type', 'html') assert repr(hd) == "{'type': ('xml', 'html')}" - # We can't guarantee order once we have more than one key. hd.add('Accept', 'text/html') assert repr(hd) in [