8000 Address review comments · stripe/stripe-python@49f21ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 49f21ba

Browse files
Address review comments
1 parent a1b479c commit 49f21ba

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
license="MIT",
2929
keywords="stripe api payments",
3030
packages=find_packages(exclude=["tests", "tests.*"]),
31-
package_data={"stripe": ["data/ca-certificates.crt"]},
31+
package_data={"stripe": ["data/ca-certificates.crt", "py.typed"]},
3232
zip_safe=False,
3333
install_requires=[
3434
'typing_extensions <= 4.2.0, > 3.7.2; python_version < "3.7"',

stripe/error.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Optional
1+
from typing import Dict, Optional, Union, cast
22
import stripe
33
from stripe.api_resources.error_object import ErrorObject
44

@@ -15,26 +15,27 @@ class StripeError(Exception):
1515

1616
def __init__(
1717
self,
18-
message=None,
19-
http_body=None,
20-
http_status=None,
21-
json_body=None,
22-
headers=None,
23-
code=None,
18+
message: Optional[str] = None,
19+
http_body: Optional[Union[bytes, str]] = None,
20+
http_status: Optional[int] = None,
21+
json_body: Optional[object] = None,
22+
headers: Optional[Dict[str, str]] = None,
23+
code: Optional[str] = None,
2424
):
2525
super(StripeError, self).__init__(message)
2626

27+
body: Optional[str] = None
2728
if http_body and hasattr(http_body, "decode"):
2829
try:
29-
http_body = http_body.decode("utf-8")
30+
body = cast(bytes, http_body).decode("utf-8")
3031
except BaseException:
31-
http_body = (
32+
body = (
3233
"<Could not decode body as utf-8. "
3334
"Please report to support@stripe.com>"
3435
)
3536

3637
self._message = message
37-
self.http_body = http_body
38+
self.http_body = body
3839
self.http_status = http_status
3940
self.json_body = json_body
4041
self.headers = headers or {}

stripe/http_client.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from stripe import error, util
1212
from stripe.request_metrics import RequestMetrics
1313

14-
from typing import Any, Optional, Tuple, ClassVar
14+
from typing import Any, Dict, Optional, Tuple, ClassVar, Union, cast
1515
from typing_extensions import NoReturn, TypedDict
1616

1717
# - Requests is the preferred HTTP library
@@ -104,14 +104,21 @@ class _Proxy(TypedDict):
104104
MAX_DELAY = 2
105105
INITIAL_DELAY = 0.5
106106
MAX_RETRY_AFTER = 60
107-
proxy: _Proxy
107+
_proxy: Optional[_Proxy]
108+
_verify_ssl_certs: bool
108109

109-
def __init__(self, verify_ssl_certs=True, proxy=None):
110+
def __init__(
111+
self,
112+
verify_ssl_certs: bool = True,
113+
proxy: Optional[Union[str, _Proxy]] = None,
114+
):
110115
self._verify_ssl_certs = verify_ssl_certs
111116
if proxy:
112117
if isinstance(proxy, str):
113118
proxy = {"http": proxy, "https": proxy}
114-
if not isinstance(proxy, dict):
119+
if not isinstance(
120+
proxy, dict
121+
): # pyright: ignore[reportUnnecessaryIsInstance]
115122
raise ValueError(
116123
"Proxy(ies) must be specified as either a string "
117124
"URL or a dict() with string URL under the"
@@ -545,13 +552,11 @@ def __init__(self, verify_ssl_certs=True, proxy=None):
545552
# need to urlparse the proxy, since PyCurl
546553
# consumes the proxy url in small pieces
547554
if self._proxy:
548-
# now that we have the parser, get the proxy url pieces
549-
# Note: self._proxy is actually dict[str, str] because this is the
550-
# type on the superclass. Here, we reassign self._proxy into
551-
# dict[str, ParseResult]
552555
proxy_ = self._proxy
553556
for scheme, value in proxy_.items():
554-
self._parsed_proxy[scheme] = urlparse(value)
557+
# In general, TypedDict.items() gives you (key: str, value: object)
558+
# but we know value to be a string because all the value types on Proxy_ are strings.
559+
self._parsed_proxy[scheme] = urlparse(cast(str, value))
555560

556561
def parse_headers(self, data):
557562
if "\r\n" not in data:
@@ -692,7 +697,9 @@ def __init__(self, verify_ssl_certs=True, proxy=None):
692697
# prepare and cache proxy tied opener here
693698
self._opener = None
694699
if self._proxy:
695-
proxy = urllibrequest.ProxyHandler(self._proxy)
700+
# We have to cast _Proxy to Dict[str, str] because pyright is not smart enough to
701+
# realize that all the value types are str.
702+
proxy = urllibrequest.ProxyHandler(cast(Dict[str, str], self._proxy))
696703
self._opener = urllibrequest.build_opener(proxy)
697704

698705
def request(self, method, url, headers, post_data=None):

stripe/multipart_data_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MultipartDataGenerator(object):
1010
boundary: int
1111
chunk_size: int
1212

13-
def __init__(self, chunk_size=1028):
13+
def __init__(self, chunk_size: int = 1028):
1414
self.data = io.BytesIO()
1515
self.line_break = "\r\n"
1616
self.boundary = self._initialize_boundary()

stripe/stripe_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class StripeResponseBase(object):
88
code: int
99
headers: Dict[str, str]
1010

11-
def __init__(self, code, headers):
11+
def __init__(self, code: int, headers: Dict[str, str]):
1212
self.code = code
1313
self.headers = headers
1414

0 commit comments

Comments
 (0)
0