From c43b1b73d020f0b6651f971ab0551c3633a4ab09 Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Tue, 22 Aug 2023 13:50:42 -0700 Subject: [PATCH] Centralize types --- setup.py | 3 +- stripe/api_resources/abstract/api_resource.py | 2 +- .../abstract/searchable_api_resource.py | 2 +- stripe/api_resources/abstract/test_helpers.py | 4 ++- stripe/api_resources/abstract/verify_mixin.py | 2 +- stripe/api_resources/customer.py | 2 +- stripe/api_resources/issuing/card.py | 2 +- stripe/api_resources/refund.py | 2 +- stripe/api_resources/terminal/reader.py | 2 +- .../treasury/inbound_transfer.py | 2 +- .../treasury/outbound_payment.py | 2 +- .../treasury/outbound_transfer.py | 2 +- .../api_resources/treasury/received_credit.py | 2 +- .../api_resources/treasury/received_debit.py | 2 +- stripe/http_client.py | 2 +- stripe/stripe_object.py | 2 +- stripe/typing.py | 29 +++++++++++++++++++ stripe/util.py | 2 +- 18 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 stripe/typing.py diff --git a/setup.py b/setup.py index 3e014207f..b82754afe 100644 --- a/setup.py +++ b/setup.py @@ -31,8 +31,7 @@ package_data={"stripe": ["data/ca-certificates.crt"]}, zip_safe=False, install_requires=[ - 'typing_extensions <= 4.2.0; python_version < "3.7"', - 'typing_extensions; python_version >= "3.7"', + 'typing_extensions >= 4.7.1; python_version >= "3.7"', 'requests >= 2.20; python_version >= "3.0"', ], python_requires=">=3.6", diff --git a/stripe/api_resources/abstract/api_resource.py b/stripe/api_resources/abstract/api_resource.py index 512bd47b2..2fac06719 100644 --- a/stripe/api_resources/abstract/api_resource.py +++ b/stripe/api_resources/abstract/api_resource.py @@ -3,7 +3,7 @@ from stripe import api_requestor, error, util from stripe.stripe_object import StripeObject from urllib.parse import quote_plus -from typing_extensions import ClassVar +from typing import ClassVar class APIResource(StripeObject): diff --git a/stripe/api_resources/abstract/searchable_api_resource.py b/stripe/api_resources/abstract/searchable_api_resource.py index 154e9cd13..da5bad8da 100644 --- a/stripe/api_resources/abstract/searchable_api_resource.py +++ b/stripe/api_resources/abstract/searchable_api_resource.py @@ -3,7 +3,7 @@ from stripe.api_resources.abstract.api_resource import APIResource from stripe.api_resources.search_result_object import SearchResultObject -from typing_extensions import cast +from stripe.typing import cast class SearchableAPIResource(APIResource): diff --git a/stripe/api_resources/abstract/test_helpers.py b/stripe/api_resources/abstract/test_helpers.py index 8e7334e25..2e0705662 100644 --- a/stripe/api_resources/abstract/test_helpers.py +++ b/stripe/api_resources/abstract/test_helpers.py @@ -3,7 +3,9 @@ from stripe import error from urllib.parse import quote_plus -from typing_extensions import Any, ClassVar, TypeVar, Protocol +from typing import ClassVar + +from stripe.typing import Any, TypeVar, Protocol from stripe.api_resources.abstract.api_resource import APIResource diff --git a/stripe/api_resources/abstract/verify_mixin.py b/stripe/api_resources/abstract/verify_mixin.py index 1e3e3de5a..0b7432b86 100644 --- a/stripe/api_resources/abstract/verify_mixin.py +++ b/stripe/api_resources/abstract/verify_mixin.py @@ -1,5 +1,5 @@ from __future__ import absolute_import, division, print_function -from typing_extensions import Optional, Protocol +from stripe.typing import Optional, Protocol from stripe.stripe_object import StripeObject diff --git a/stripe/api_resources/customer.py b/stripe/api_resources/customer.py index 32c15e9ca..3cafb8aab 100644 --- a/stripe/api_resources/customer.py +++ b/stripe/api_resources/customer.py @@ -10,7 +10,7 @@ from stripe.api_resources.abstract import SearchableAPIResource from stripe.api_resources.abstract import UpdateableAPIResource from stripe.api_resources.abstract import nested_resource_class_methods -from typing_extensions import Type +from stripe.typing import Type @nested_resource_class_methods( diff --git a/stripe/api_resources/issuing/card.py b/stripe/api_resources/issuing/card.py index 37e1be7f2..1c0a3c8ae 100644 --- a/stripe/api_resources/issuing/card.py +++ b/stripe/api_resources/issuing/card.py @@ -7,7 +7,7 @@ from stripe.api_resources.abstract import CreateableAPIResource from stripe.api_resources.abstract import ListableAPIResource from stripe.api_resources.abstract import UpdateableAPIResource -from typing_extensions import Type +from stripe.typing import Type class Card(CreateableAPIResource, ListableAPIResource, UpdateableAPIResource): diff --git a/stripe/api_resources/refund.py b/stripe/api_resources/refund.py index 219ad8ca7..78d4bf9c6 100644 --- a/stripe/api_resources/refund.py +++ b/stripe/api_resources/refund.py @@ -7,7 +7,7 @@ from stripe.api_resources.abstract import CreateableAPIResource from stripe.api_resources.abstract import ListableAPIResource from stripe.api_resources.abstract import UpdateableAPIResource -from typing_extensions import Type +from stripe.typing import Type class Refund( diff --git a/stripe/api_resources/terminal/reader.py b/stripe/api_resources/terminal/reader.py index 9f367ade0..085469b1e 100644 --- a/stripe/api_resources/terminal/reader.py +++ b/stripe/api_resources/terminal/reader.py @@ -8,7 +8,7 @@ from stripe.api_resources.abstract import DeletableAPIResource from stripe.api_resources.abstract import ListableAPIResource from stripe.api_resources.abstract import UpdateableAPIResource -from typing_extensions import Type +from stripe.typing import Type class Reader( diff --git a/stripe/api_resources/treasury/inbound_transfer.py b/stripe/api_resources/treasury/inbound_transfer.py index e2b5e2e8d..bffecf593 100644 --- a/stripe/api_resources/treasury/inbound_transfer.py +++ b/stripe/api_resources/treasury/inbound_transfer.py @@ -6,7 +6,7 @@ from stripe.api_resources.abstract import APIResourceTestHelpers from stripe.api_resources.abstract import CreateableAPIResource from stripe.api_resources.abstract import ListableAPIResource -from typing_extensions import Type +from stripe.typing import Type class InboundTransfer(CreateableAPIResource, ListableAPIResource): diff --git a/stripe/api_resources/treasury/outbound_payment.py b/stripe/api_resources/treasury/outbound_payment.py index 659ad4185..e5dc5ab59 100644 --- a/stripe/api_resources/treasury/outbound_payment.py +++ b/stripe/api_resources/treasury/outbound_payment.py @@ -6,7 +6,7 @@ from stripe.api_resources.abstract import APIResourceTestHelpers from stripe.api_resources.abstract import CreateableAPIResource from stripe.api_resources.abstract import ListableAPIResource -from typing_extensions import Type +from stripe.typing import Type class OutboundPayment(CreateableAPIResource, ListableAPIResource): diff --git a/stripe/api_resources/treasury/outbound_transfer.py b/stripe/api_resources/treasury/outbound_transfer.py index 7fcb81d10..7846082e7 100644 --- a/stripe/api_resources/treasury/outbound_transfer.py +++ b/stripe/api_resources/treasury/outbound_transfer.py @@ -6,7 +6,7 @@ from stripe.api_resources.abstract import APIResourceTestHelpers from stripe.api_resources.abstract import CreateableAPIResource from stripe.api_resources.abstract import ListableAPIResource -from typing_extensions import Type +from stripe.typing import Type class OutboundTransfer(CreateableAPIResource, ListableAPIResource): diff --git a/stripe/api_resources/treasury/received_credit.py b/stripe/api_resources/treasury/received_credit.py index 152cd26be..3a638ecbd 100644 --- a/stripe/api_resources/treasury/received_credit.py +++ b/stripe/api_resources/treasury/received_credit.py @@ -4,7 +4,7 @@ from stripe.api_resources.abstract import APIResourceTestHelpers from stripe.api_resources.abstract import ListableAPIResource -from typing_extensions import Type +from stripe.typing import Type class ReceivedCredit(ListableAPIResource): diff --git a/stripe/api_resources/treasury/received_debit.py b/stripe/api_resources/treasury/received_debit.py index 63e87b54c..76991708c 100644 --- a/stripe/api_resources/treasury/received_debit.py +++ b/stripe/api_resources/treasury/received_debit.py @@ -4,7 +4,7 @@ from stripe.api_resources.abstract import APIResourceTestHelpers from stripe.api_resources.abstract import ListableAPIResource -from typing_extensions import Type +from stripe.typing import Type class ReceivedDebit(ListableAPIResource): diff --git a/stripe/http_client.py b/stripe/http_client.py index 33c81489d..834f0b6a6 100644 --- a/stripe/http_client.py +++ b/stripe/http_client.py @@ -13,7 +13,7 @@ from stripe import error, util from stripe.request_metrics import RequestMetrics -from typing_extensions import NoReturn, Optional, cast, TypedDict, Dict +from stripe.typing import NoReturn, Optional, cast, TypedDict, Dict # - Requests is the preferred HTTP library # - Google App Engine has urlfetch diff --git a/stripe/stripe_object.py b/stripe/stripe_object.py index e22cdf908..f78ef8e36 100644 --- a/stripe/stripe_object.py +++ b/stripe/stripe_object.py @@ -3,7 +3,7 @@ import datetime import json from copy import deepcopy -from typing_extensions import TYPE_CHECKING +from stripe.typing import TYPE_CHECKING import stripe from stripe import api_requestor, util diff --git a/stripe/typing.py b/stripe/typing.py new file mode 100644 index 000000000..7fe209fbe --- /dev/null +++ b/stripe/typing.py @@ -0,0 +1,29 @@ +# Centralized place for typing imports. + +# The last version supported by Python 3.6 is typing_extensions 4.1.1. Unfortunately, +# typing_extensions didn't decide to re-export all of the types from typing until 4.7.0. +# TODO (major): if we drop support for Python 3.6, we can remove this file and just import +# everything from typing_extensions. + +# Rules: don't depend on anything only in typing_extensions > 4.1.1. If we need something +# newer, we might be able to provide a better type for some users with a conditional import +# but we'll cross that bridge when we come to it. + +# Note: we can't re-export *everything* here, unfortunately. Some special +# forms can't be aliased. +# > The following special forms cannot be re-exported: Final, ClassVar, and InitVar in pyright. +# (https://github.com/microsoft/pyright/discussions/4845#discussioncomment-5440032) + +from typing import Optional as Optional +from typing import cast as cast +from typing import Dict as Dict +from typing import Any as Any +from typing import TypeVar as TypeVar +from typing import Union as Union +from typing_extensions import TYPE_CHECKING as TYPE_CHECKING +from typing_extensions import Literal as Literal +from typing_extensions import NoReturn as NoReturn +from typing_extensions import Protocol as Protocol +from typing_extensions import Type as Type +from typing_extensions import TypedDict as TypedDict +from typing_extensions import overload as overload diff --git a/stripe/util.py b/stripe/util.py index 82ecadd1a..3457c0e9d 100644 --- a/stripe/util.py +++ b/stripe/util.py @@ -11,7 +11,7 @@ import stripe from urllib.parse import parse_qsl, quote_plus -from typing_extensions import overload, Literal, Union +from stripe.typing import overload, Literal, Union STRIPE_LOG = os.environ.get("STRIPE_LOG")