Skip to content

Commit a6f6aa8

Browse files
author
childish-sambino
authored
chore: fix pyright errors in http folder (#683)
1 parent daecc55 commit a6f6aa8

File tree

7 files changed

+54
-41
lines changed

7 files changed

+54
-41
lines changed

tests/unit/http/test_async_http_client.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def setUp(self):
2424
self.session_mock = AsyncMock(wraps=ClientSession)
2525
self.session_mock.request.return_value = MockResponse("test", 200)
2626

27-
self.session_patcher = patch(
28-
"twilio.http.async_http_client.aiohttp.ClientSession"
29-
)
27+
self.session_patcher = patch("twilio.http.async_http_client.ClientSession")
3028
session_constructor_mock = self.session_patcher.start()
3129
session_constructor_mock.return_value = self.session_mock
3230

@@ -69,9 +67,7 @@ def setUp(self):
6967
MockResponse("Success", 200),
7068
]
7169

72-
self.session_patcher = patch(
73-
"twilio.http.async_http_client.aiohttp.ClientSession"
74-
)
70+
self.session_patcher = patch("twilio.http.async_http_client.ClientSession")
7571
session_constructor_mock = self.session_patcher.start()
7672
session_constructor_mock.return_value = self.session_mock
7773

@@ -97,9 +93,7 @@ async def test_request_retries_until_max(self):
9793

9894
class TestAsyncHttpClientSession(aiounittest.AsyncTestCase):
9995
def setUp(self):
100-
self.session_patcher = patch(
101-
"twilio.http.async_http_client.aiohttp.ClientSession"
102-
)
96+
self.session_patcher = patch("twilio.http.async_http_client.ClientSession")
10397
self.session_constructor_mock = self.session_patcher.start()
10498

10599
def tearDown(self):

tests/unit/http/test_http_client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_request_with_timeout(self):
4646
self.request_mock.headers = {"Host": "other.twilio.com"}
4747

4848
response = self.client.request(
49-
"doesnt matter", "doesnt matter", None, None, None, None, 30, None
49+
"doesnt matter", "doesnt matter", None, None, None, None, 30
5050
)
5151

5252
self.assertEqual("other.twilio.com", self.request_mock.headers["Host"])
@@ -59,7 +59,7 @@ def test_request_where_method_timeout_equals_zero(self):
5959

6060
try:
6161
self.client.request(
62-
"doesnt matter", "doesnt matter", None, None, None, None, 0, None
62+
"doesnt matter", "doesnt matter", None, None, None, None, 0
6363
)
6464
except Exception as e:
6565
self.assertEqual(ValueError, type(e))
@@ -90,7 +90,7 @@ def test_request_where_class_timeout_and_method_timeout_set(self):
9090
self.client.timeout = 30
9191

9292
response = self.client.request(
93-
"doesnt matter", "doesnt matter", None, None, None, None, 15, None
93+
"doesnt matter", "doesnt matter", None, None, None, None, 15
9494
)
9595

9696
self.assertEqual("other.twilio.com", self.request_mock.headers["Host"])
@@ -138,10 +138,12 @@ def test_last_request_last_response_exist(self):
138138
self.assertEqual(["a", "b"], self.client._test_only_last_request.auth)
139139

140140
self.assertIsNotNone(self.client._test_only_last_response)
141-
self.assertEqual(200, self.client._test_only_last_response.status_code)
142-
self.assertEqual(
143-
"testing-unicode: Ω≈ç√, 💩", self.client._test_only_last_response.text
144-
)
141+
142+
if self.client._test_only_last_response is not None:
143+
self.assertEqual(200, self.client._test_only_last_response.status_code)
144+
self.assertEqual(
145+
"testing-unicode: Ω≈ç√, 💩", self.client._test_only_last_response.text
146+
)
145147

146148
def test_last_response_empty_on_error(self):
147149
self.session_mock.send.side_effect = Exception("voltron")

tests/unit/http/test_validation_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_build_validation_payload_basic(self):
3030
self.assertEqual("", validation_payload.body)
3131

3232
def test_build_validation_payload_query_string_parsed(self):
33-
self.request.url = self.request.url + "?QueryParam=1&Other=true"
33+
self.request.url = (self.request.url or "") + "?QueryParam=1&Other=true"
3434

3535
validation_payload = self.client._build_validation_payload(self.request)
3636

@@ -41,7 +41,7 @@ def test_build_validation_payload_query_string_parsed(self):
4141
self.assertEqual("", validation_payload.body)
4242

4343
def test_build_validation_payload_body_parsed(self):
44-
self.request.body = "foobar"
44+
setattr(self.request, "body", "foobar")
4545

4646
validation_payload = self.client._build_validation_payload(self.request)
4747

@@ -52,10 +52,10 @@ def test_build_validation_payload_body_parsed(self):
5252
self.assertEqual("foobar", validation_payload.body)
5353

5454
def test_build_validation_payload_complex(self):
55-
self.request.body = "foobar"
55+
setattr(self.request, "body", "foobar")
5656
self.request.url = (
57-
self.request.url + "?QueryParam=Value&OtherQueryParam=OtherValue"
58-
)
57+
self.request.url or ""
58+
) + "?QueryParam=Value&OtherQueryParam=OtherValue"
5959

6060
validation_payload = self.client._build_validation_payload(self.request)
6161

twilio/http/async_http_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import aiohttp
21
import logging
32

4-
from aiohttp import BasicAuth
3+
from aiohttp import BasicAuth, ClientSession
54
from aiohttp_retry import ExponentialRetry, RetryClient
65
from twilio.http import AsyncHttpClient
76
from twilio.http.request import Request as TwilioRequest
@@ -39,7 +38,7 @@ def __init__(
3938
self.proxy_url = proxy_url
4039
self.trace_configs = trace_configs
4140
self.session = (
42-
aiohttp.ClientSession(trace_configs=self.trace_configs)
41+
ClientSession(trace_configs=self.trace_configs)
4342
if pool_connections
4443
else None
4544
)
@@ -102,7 +101,7 @@ async def request(
102101
if self.session:
103102
session = self.session
104103
else:
105-
session = aiohttp.ClientSession()
104+
session = ClientSession()
106105
temp = True
107106
self._test_only_last_request = TwilioRequest(**kwargs)
108107
response = await session.request(**kwargs)
@@ -131,4 +130,5 @@ async def __aexit__(self, *excinfo):
131130
"""
132131
Async context manager exit
133132
"""
134-
await self.session.close()
133+
if self.session:
134+
await self.session.close()

twilio/http/http_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def request(
6969
:return: An http response
7070
:rtype: A :class:`Response <twilio.rest.http.response.Response>` object
7171
"""
72-
if timeout is not None and timeout <= 0:
72+
if timeout is None:
73+
timeout = self.timeout
74+
elif timeout <= 0:
7375
raise ValueError(timeout)
7476

7577
kwargs = {
@@ -95,10 +97,12 @@ def request(
9597
prepped_request.url, self.proxy, None, None, None
9698
)
9799

98-
settings["allow_redirects"] = allow_redirects
99-
settings["timeout"] = timeout if timeout is not None else self.timeout
100-
101-
response = session.send(prepped_request, **settings)
100+
response = session.send(
101+
prepped_request,
102+
allow_redirects=allow_redirects,
103+
timeout=timeout,
104+
**settings
105+
)
102106

103107
self.log_response(response.status_code, response)
104108

twilio/http/request.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
1+
from enum import Enum
2+
from typing import Dict, Union
13
from urllib.parse import urlencode
24

35

6+
class Match(Enum):
7+
ANY = "*"
8+
9+
410
class Request(object):
511
"""
612
An HTTP request.
713
"""
814

9-
ANY = "*"
10-
1115
def __init__(
12-
self, method=ANY, url=ANY, auth=ANY, params=ANY, data=ANY, headers=ANY, **kwargs
16+
self,
17+
method: Union[str, Match] = Match.ANY,
18+
url: Union[str, Match] = Match.ANY,
19+
auth: Union[tuple, Match] = Match.ANY,
20+
params: Union[Dict, Match] = Match.ANY,
21+
data: Union[Dict, Match] = Match.ANY,
22+
headers: Union[Dict, Match] = Match.ANY,
23+
**kwargs
1324
):
14-
self.method = method.upper()
25+
self.method = method
26+
if method and method != Match.ANY:
27+
self.method = method.upper()
1528
self.url = url
1629
self.auth = auth
1730
self.params = params
@@ -20,7 +33,7 @@ def __init__(
2033

2134
@classmethod
2235
def attribute_equal(cls, lhs, rhs):
23-
if lhs == cls.ANY or rhs == cls.ANY:
36+
if lhs == Match.ANY or rhs == Match.ANY:
2437
# ANY matches everything
2538
return True
2639

@@ -44,23 +57,23 @@ def __eq__(self, other):
4457

4558
def __str__(self):
4659
auth = ""
47-
if self.auth and self.auth != self.ANY:
60+
if self.auth and self.auth != Match.ANY:
4861
auth = "{} ".format(self.auth)
4962

5063
params = ""
51-
if self.params and self.params != self.ANY:
64+
if self.params and self.params != Match.ANY:
5265
params = "?{}".format(urlencode(self.params, doseq=True))
5366

5467
data = ""
55-
if self.data and self.data != self.ANY:
68+
if self.data and self.data != Match.ANY:
5669
if self.method == "GET":
5770
data = "\n -G"
5871
data += "\n{}".format(
5972
"\n".join(' -d "{}={}"'.format(k, v) for k, v in self.data.items())
6073
)
6174

6275
headers = ""
63-
if self.headers and self.headers != self.ANY:
76+
if self.headers and self.headers != Match.ANY:
6477
headers = "\n{}".format(
6578
"\n".join(' -H "{}: {}"'.format(k, v) for k, v in self.headers.items())
6679
)

twilio/http/validation_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def _build_validation_payload(self, request):
121121
def _get_host(self, request):
122122
"""Pull the Host out of the request"""
123123
parsed = urlparse(request.url)
124-
return parsed.netloc
124+
return str(parsed.netloc)
125125

126126
def validate_ssl_certificate(self, client):
127127
"""

0 commit comments

Comments
 (0)