You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Many CreateParams attributes on StripeObjects are defined to be non-optional. This means that if that keyword argument is passed into the .create( function, it must be a non-null value (that also adheres to the defined typing).
However, the API supports passing null values on many of these attributes and that seems to contextually make sense.
One example is the promotion_code attribute on the SubscriptionCreateParams class.
The following call succeeds without issue:
importstripeapi_key="***"# loaded from envapi_version="2023-10-16"# At the time of writing this ticket, I am using this API versioncustomer_id="cus_xxx"# use any customer_id with a payment source in your own envprice_id="price_xxx". # use an existing active price on a product in your stripe envpromotion_code=Nonesubscription=stripe.Subscription.create(
customer=customer_id,
items=[{"price": price_id}],
payment_behavior="error_if_incomplete",
promotion_code=promotion_code,
)
however, mypy will find issues with the fact that promotion_code is None and prefers it not to be passed at all.
The workaround at this current moment (other than ignoring the type issue) would be to add a conditional
however, this is quite tedious and becomes unruly if you have more than one attribute you need to check (a permutation of checks will occur)
To Reproduce
Write the follow or similar code
importstripeapi_key="***"# loaded from envapi_version="2023-10-16"# At the time of writing this ticket, I am using this API versioncustomer_id="cus_xxx"# use any customer_id with a payment source in your own envprice_id="price_xxx". # use an existing active price on a product in your stripe envpromotion_code=Nonesubscription=stripe.Subscription.create(
customer=customer_id,
items=[{"price": price_id}],
payment_behavior="error_if_incomplete",
promotion_code=promotion_code,
)
Run mypy checker
You will see the following error: error: Argument "promotion_code" to "create" of "Subscription" has incompatible type "str | None"; expected "str" [arg-type]
Expected behavior
Using this line as an example, any nullable CreateParams attribute should be noted as Optional
Suggested Solution
promotion_code: NotRequired["str|None"]
^This solution most similarly reflects the way that Optional is implemented on the base RequestOptions class
Hi @tetsujiono! Thanks for pointing this out, we'll investigate whether or not to add this for QoL improvement. In the meantime, this is a small improvement:
We have a situation where we're using Stripe Connect as well as our own Stripe account for various things. There are places where we specify (eg) application_fee_amount which can either be an actual amount (for Stripe Connect payment intents) or None for payment intents using our own Stripe account.
Describe the bug
Many
CreateParams
attributes onStripeObject
s are defined to be non-optional. This means that if that keyword argument is passed into the.create(
function, it must be a non-null value (that also adheres to the defined typing).However, the API supports passing null values on many of these attributes and that seems to contextually make sense.
One example is the promotion_code attribute on the
Subscription
CreateParams
class.The following call succeeds without issue:
however, mypy will find issues with the fact that
promotion_code
isNone
and prefers it not to be passed at all.The workaround at this current moment (other than ignoring the type issue) would be to add a conditional
however, this is quite tedious and becomes unruly if you have more than one attribute you need to check (a permutation of checks will occur)
To Reproduce
error: Argument "promotion_code" to "create" of "Subscription" has incompatible type "str | None"; expected "str" [arg-type]
Expected behavior
Using this line as an example, any nullable
CreateParams
attribute should be noted as OptionalSuggested Solution
^This solution most similarly reflects the way that Optional is implemented on the base
RequestOptions
classCode snippets
OS
macOS
Language version
Python 3.11.3
Library version
stripe-python v8.9.0
API version
2023-10-16
Additional context
No response
The text was updated successfully, but these errors were encountered: