Skip to content

Commit

Permalink
Merge master into beta (#1025)
Browse files Browse the repository at this point in the history
* Update generated code (#1016)

* Update generated code for v466

* Update generated code for v468

* Update generated code for v482

---------

Co-authored-by: Stripe OpenAPI <105521251+stripe-openapi[bot]@users.noreply.github.com>
Co-authored-by: anniel-stripe <97691964+anniel-stripe@users.noreply.github.com>

* Bump version to 6.1.0

* Pyright: fix more type suppressions (#1023)

---------

Co-authored-by: stripe-openapi[bot] <105521251+stripe-openapi[bot]@users.noreply.github.com>
Co-authored-by: Richard Marmorstein <52928443+richardm-stripe@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 24, 2023
1 parent 82209e4 commit 5abe8eb
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 225 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ coverage.xml
.pytest_cache/

# pyenv
.python_version
.python-version

# Environments
.env
Expand Down
388 changes: 196 additions & 192 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions flake8_stripe/flake8_stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TypingImportsChecker:
"TYPE_CHECKING",
"Type",
"TypedDict",
"Self",
]

allowed_typing_imports = [
Expand All @@ -39,6 +40,7 @@ class TypingImportsChecker:
"cast",
"overload",
"Dict",
"List",
]

def __init__(self, tree: ast.AST):
Expand Down
3 changes: 3 additions & 0 deletions stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@

from stripe.preview import preview # noqa

from . import stripe_response # noqa
from . import stripe_object # noqa


# Sets some basic information about the running application that's sent along
# with API requests. Useful for plugin authors to identify their plugin when
Expand Down
1 change: 1 addition & 0 deletions stripe/api_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import absolute_import, division, print_function

# flake8: noqa
from . import abstract

from stripe.api_resources.error_object import ErrorObject, OAuthErrorObject
from stripe.api_resources.list_object import ListObject
Expand Down
37 changes: 26 additions & 11 deletions stripe/api_resources/list_object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import absolute_import, division, print_function
from typing_extensions import Self
from typing import List

from stripe.stripe_object import StripeObject

Expand All @@ -7,6 +9,9 @@

class ListObject(StripeObject):
OBJECT_NAME = "list"
data: List[StripeObject]
has_more: bool
url: str

def list(
self, api_key=None, stripe_version=None, stripe_account=None, **params
Expand Down Expand Up @@ -86,13 +91,13 @@ def auto_paging_iter(self):
):
for item in reversed(page): # type: ignore
yield item
page = page.previous_page() # type: ignore
page = page.previous_page()
else:
for item in page: # type: ignore
yield item
page = page.next_page() # type: ignore
page = page.next_page()

if page.is_empty: # type: ignore
if page.is_empty:
break

@classmethod
Expand All @@ -109,19 +114,23 @@ def empty_list(

@property
def is_empty(self):
return not self.data # type: ignore
return not self.data

def next_page(
self, api_key=None, stripe_version=None, stripe_account=None, **params
):
if not self.has_more: # type: ignore
) -> Self:
if not self.has_more:
return self.empty_list(
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
)

last_id = self.data[-1].id # type: ignore
last_id = getattr(self.data[-1], "id")
if not last_id:
raise ValueError(
"Unexpected: element in .data of list object had no id"
)

params_with_filters = self._retrieve_params.copy()
params_with_filters.update({"starting_after": last_id})
Expand All @@ -138,23 +147,29 @@ def next_page(

def previous_page(
self, api_key=None, stripe_version=None, stripe_account=None, **params
):
if not self.has_more: # type: ignore
) -> Self:
if not self.has_more:
return self.empty_list(
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
)

first_id = self.data[0].id # type: ignore
first_id = getattr(self.data[0], "id")
if not first_id:
raise ValueError(
"Unexpected: element in .data of list object had no id"
)

params_with_filters = self._retrieve_params.copy()
params_with_filters.update({"ending_before": first_id})
params_with_filters.update(params)

return self.list(
result = self.list(
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
**params_with_filters
)
assert isinstance(result, ListObject)
return result
17 changes: 11 additions & 6 deletions stripe/api_resources/search_result_object.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from __future__ import absolute_import, division, print_function
from typing_extensions import Self
from typing import List

from stripe.stripe_object import StripeObject


class SearchResultObject(StripeObject):
OBJECT_NAME = "search_result"
data: List[StripeObject]
has_more: bool
next_page: str

def search(
self, api_key=None, stripe_version=None, stripe_account=None, **params
Expand Down Expand Up @@ -41,9 +46,9 @@ def auto_paging_iter(self):
while True:
for item in page:
yield item
page = page.next_search_result_page() # type: ignore
page = page.next_search_result_page()

if page.is_empty: # type: ignore
if page.is_empty:
break

@classmethod
Expand All @@ -60,20 +65,20 @@ def empty_search_result(

@property
def is_empty(self):
return not self.data # type: ignore
return not self.data

def next_search_result_page(
self, api_key=None, stripe_version=None, stripe_account=None, **params
):
if not self.has_more: # type: ignore
) -> Self:
if not self.has_more:
return self.empty_search_result(
api_key=api_key,
stripe_version=stripe_version,
stripe_account=stripe_account,
)

params_with_filters = self._retrieve_params.copy()
params_with_filters.update({"page": self.next_page}) # type: ignore
params_with_filters.update({"page": self.next_page})
params_with_filters.update(params)

result = self.search(
Expand Down
11 changes: 6 additions & 5 deletions stripe/stripe_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _request(

if idempotency_key is not None:
headers = {} if headers is None else headers.copy()
headers.update(util.populate_headers(idempotency_key)) # type: ignore
headers.update(util.populate_headers(idempotency_key))

response, api_key = requestor.request(method_, url_, params, headers)

Expand All @@ -304,8 +304,9 @@ def request_stream(self, method, url, params=None, headers=None):
def __repr__(self):
ident_parts = [type(self).__name__]

if isinstance(self.get("object"), str):
ident_parts.append(self.get("object")) # type: ignore
obj_str = self.get("object")
if isinstance(obj_str, str):
ident_parts.append(obj_str)

if isinstance(self.get("id"), str):
ident_parts.append("id=%s" % (self.get("id"),))
Expand Down Expand Up @@ -347,7 +348,7 @@ def maybe_to_dict_recursive(value):

@property
def stripe_id(self):
return self.id # type: ignore
return getattr(self, "id")

def serialize(self, previous):
params = {}
Expand All @@ -357,7 +358,7 @@ def serialize(self, previous):
for k, v in self.items():
if k == "id" or (isinstance(k, str) and k.startswith("_")):
continue
elif isinstance(v, stripe.api_resources.abstract.APIResource): # type: ignore
elif isinstance(v, stripe.abstract.APIResource):
continue
elif hasattr(v, "serialize"):
child = v.serialize(previous.get(k, None))
Expand Down
27 changes: 17 additions & 10 deletions stripe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import stripe
from urllib.parse import parse_qsl, quote_plus

from typing import Union, overload, Dict
from typing_extensions import Literal
from typing_extensions import Type, Literal
from typing import Union, overload, Dict, cast

STRIPE_LOG = os.environ.get("STRIPE_LOG")

Expand Down Expand Up @@ -117,7 +117,7 @@ def secure_compare(val1, val2):
result |= x ^ y
else:
for x, y in zip(val1, val2):
result |= ord(x) ^ ord(y) # type: ignore
result |= ord(cast(str, x)) ^ ord(cast(str, y))
return result == 0


Expand All @@ -136,7 +136,7 @@ def convert_to_stripe_object(
# the raw API response information
stripe_response = None

if isinstance(resp, stripe.stripe_response.StripeResponse): # type: ignore
if isinstance(resp, stripe.stripe_response.StripeResponse):
stripe_response = resp
resp = stripe_response.data

Expand All @@ -148,16 +148,16 @@ def convert_to_stripe_object(
for i in resp
]
elif isinstance(resp, dict) and not isinstance(
resp, stripe.stripe_object.StripeObject # type: ignore
resp, stripe.stripe_object.StripeObject
):
resp = resp.copy()
klass_name = resp.get("object")
if isinstance(klass_name, str):
klass = get_object_classes().get(
klass_name, stripe.stripe_object.StripeObject # type: ignore
klass_name, stripe.stripe_object.StripeObject
)
else:
klass = stripe.stripe_object.StripeObject # type: ignore
klass = stripe.stripe_object.StripeObject

obj = klass.construct_from(
resp,
Expand All @@ -173,7 +173,10 @@ def convert_to_stripe_object(
if (
params is not None
and hasattr(obj, "object")
and ((obj.object == "list") or (obj.object == "search_result"))
and (
(getattr(obj, "object") == "list")
or (getattr(obj, "object") == "search_result")
)
):
obj._retrieve_params = params

Expand Down Expand Up @@ -253,14 +256,18 @@ def __call__(self, method):
self.method = method
return self

def __get__(self, obj, objtype=None):
def __get__(self, obj, objtype: Type = None):
@functools.wraps(self.method)
def _wrapper(*args, **kwargs):
if obj is not None:
# Method was called as an instance method, e.g.
# instance.method(...)
return self.method(obj, *args, **kwargs)
elif len(args) > 0 and isinstance(args[0], objtype): # type: ignore
elif (
len(args) > 0
and objtype is not None
and isinstance(args[0], objtype)
):
# Method was called as a class method with the instance as the
# first argument, e.g. Class.method(instance, ...) which in
# Python is the same thing as calling an instance method
Expand Down

0 comments on commit 5abe8eb

Please sign in to comment.