From 89ab030cdb83a728a30e172bc65d27ba214d2eda Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Sun, 4 Jun 2017 14:12:08 -0300 Subject: [PATCH] Use comprehensions whenever possible --- requests/adapters.py | 3 +-- requests/cookies.py | 29 +++++++++++++++-------------- requests/hooks.py | 4 ++-- requests/models.py | 5 +---- requests/sessions.py | 2 +- tests/test_requests.py | 2 +- tests/test_utils.py | 2 +- 7 files changed, 22 insertions(+), 25 deletions(-) diff --git a/requests/adapters.py b/requests/adapters.py index f26391502e..09d5eb8036 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -129,8 +129,7 @@ def __init__(self, pool_connections=DEFAULT_POOLSIZE, self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block) def __getstate__(self): - return dict((attr, getattr(self, attr, None)) for attr in - self.__attrs__) + return {attr: getattr(self, attr, None) for attr in self.__attrs__} def __setstate__(self, state): # Can't handle by adding 'proxy_manager' to self.__attrs__ because diff --git a/requests/cookies.py b/requests/cookies.py index 72e94be8d6..56fccd9c25 100644 --- a/requests/cookies.py +++ b/requests/cookies.py @@ -444,20 +444,21 @@ def create_cookie(name, value, **kwargs): By default, the pair of `name` and `value` will be set for the domain '' and sent on every request (this is sometimes called a "supercookie"). """ - result = dict( - version=0, - name=name, - value=value, - port=None, - domain='', - path='/', - secure=False, - expires=None, - discard=True, - comment=None, - comment_url=None, - rest={'HttpOnly': None}, - rfc2109=False,) + result = { + 'version': 0, + 'name': name, + 'value': value, + 'port': None, + 'domain': '', + 'path': '/', + 'secure': False, + 'expires': None, + 'discard': True, + 'comment': None, + 'comment_url': None, + 'rest': {'HttpOnly': None}, + 'rfc2109': False, + } badargs = set(kwargs) - set(result) if badargs: diff --git a/requests/hooks.py b/requests/hooks.py index 32b32de750..7a51f212c8 100644 --- a/requests/hooks.py +++ b/requests/hooks.py @@ -15,14 +15,14 @@ def default_hooks(): - return dict((event, []) for event in HOOKS) + return {event: [] for event in HOOKS} # TODO: response is the only one def dispatch_hook(key, hooks, hook_data, **kwargs): """Dispatches a hook dictionary on a given piece of data.""" - hooks = hooks or dict() + hooks = hooks or {} hooks = hooks.get(key) if hooks: if hasattr(hooks, '__call__'): diff --git a/requests/models.py b/requests/models.py index 4c8d631c7f..3dded57eff 100644 --- a/requests/models.py +++ b/requests/models.py @@ -652,10 +652,7 @@ def __getstate__(self): if not self._content_consumed: self.content - return dict( - (attr, getattr(self, attr, None)) - for attr in self.__attrs__ - ) + return {attr: getattr(self, attr, None) for attr in self.__attrs__} def __setstate__(self, state): for name, value in state.items(): diff --git a/requests/sessions.py b/requests/sessions.py index b124d793d1..a448bd83f2 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -738,7 +738,7 @@ def mount(self, prefix, adapter): self.adapters[key] = self.adapters.pop(key) def __getstate__(self): - state = dict((attr, getattr(self, attr, None)) for attr in self.__attrs__) + state = {attr: getattr(self, attr, None) for attr in self.__attrs__} return state def __setstate__(self, state): diff --git a/tests/test_requests.py b/tests/test_requests.py index b4ee9f94d4..d972f00263 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -864,7 +864,7 @@ def test_certificate_failure(self, httpbin_secure): def test_urlencoded_get_query_multivalued_param(self, httpbin): - r = requests.get(httpbin('get'), params=dict(test=['foo', 'baz'])) + r = requests.get(httpbin('get'), params={'test': ['foo', 'baz']}) assert r.status_code == 200 assert r.url == httpbin('get?test=foo&test=baz') diff --git a/tests/test_utils.py b/tests/test_utils.py index f34c630f07..59b0b0efaf 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -665,7 +665,7 @@ def test_add_dict_to_cookiejar(cookiejar): cookiedict = {'test': 'cookies', 'good': 'cookies'} cj = add_dict_to_cookiejar(cookiejar, cookiedict) - cookies = dict((cookie.name, cookie.value) for cookie in cj) + cookies = {cookie.name: cookie.value for cookie in cj} assert cookiedict == cookies