stripe: Add assertions to fix errors flagged by mypy 1.10.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-04-30 11:02:47 -07:00 committed by Tim Abbott
parent b195dc5a89
commit 31c7b2bfd7
2 changed files with 14 additions and 1 deletions

View File

@ -3092,6 +3092,7 @@ class BillingSession(ABC):
licenses_base = ledger_entry.licenses licenses_base = ledger_entry.licenses
if invoice_item_created: if invoice_item_created:
assert invoice_period is not None
flat_discount, flat_discounted_months = self.get_flat_discount_info(plan.customer) flat_discount, flat_discounted_months = self.get_flat_discount_info(plan.customer)
if plan.fixed_price is None and flat_discounted_months > 0: if plan.fixed_price is None and flat_discounted_months > 0:
num_months = ( num_months = (
@ -3442,14 +3443,19 @@ class BillingSession(ABC):
# the updated quantity. # the updated quantity.
stripe_invoice = stripe.Invoice.retrieve(last_sent_invoice.stripe_invoice_id) stripe_invoice = stripe.Invoice.retrieve(last_sent_invoice.stripe_invoice_id)
assert stripe_invoice.status == "open" assert stripe_invoice.status == "open"
assert isinstance(stripe_invoice.customer, str)
assert stripe_invoice.statement_descriptor is not None
assert stripe_invoice.metadata is not None
invoice_items = stripe_invoice.lines.data invoice_items = stripe_invoice.lines.data
# Stripe does something weird and puts the discount item first, so we need to reverse the order here. # Stripe does something weird and puts the discount item first, so we need to reverse the order here.
invoice_items.reverse() invoice_items.reverse()
for invoice_item in invoice_items: for invoice_item in invoice_items:
assert invoice_item.description is not None
price_args: PriceArgs = {} price_args: PriceArgs = {}
# If amount is positive, this must be non-discount item we need to update. # If amount is positive, this must be non-discount item we need to update.
if invoice_item.amount > 0: if invoice_item.amount > 0:
assert invoice_item.price is not None assert invoice_item.price is not None
assert invoice_item.price.unit_amount is not None
price_args = { price_args = {
"quantity": licenses, "quantity": licenses,
"unit_amount": invoice_item.price.unit_amount, "unit_amount": invoice_item.price.unit_amount,

View File

@ -16,6 +16,7 @@ from typing import (
Callable, Callable,
Dict, Dict,
List, List,
Literal,
Mapping, Mapping,
Optional, Optional,
Sequence, Sequence,
@ -524,13 +525,19 @@ class StripeTestCase(ZulipTestCase):
}, },
}, },
) )
assert isinstance(checkout_setup_intent.customer, str)
assert checkout_setup_intent.metadata is not None
assert checkout_setup_intent.usage in {"off_session", "on_session"}
usage = cast(
Literal["off_session", "on_session"], checkout_setup_intent.usage
) # https://github.com/python/mypy/issues/12535
stripe_setup_intent = stripe.SetupIntent.create( stripe_setup_intent = stripe.SetupIntent.create(
payment_method=payment_method.id, payment_method=payment_method.id,
confirm=True, confirm=True,
payment_method_types=checkout_setup_intent.payment_method_types, payment_method_types=checkout_setup_intent.payment_method_types,
customer=checkout_setup_intent.customer, customer=checkout_setup_intent.customer,
metadata=checkout_setup_intent.metadata, metadata=checkout_setup_intent.metadata,
usage=checkout_setup_intent.usage, usage=usage,
) )
[stripe_session] = iter(stripe.checkout.Session.list(customer=customer_stripe_id, limit=1)) [stripe_session] = iter(stripe.checkout.Session.list(customer=customer_stripe_id, limit=1))
stripe_session_dict = orjson.loads(orjson.dumps(stripe_session)) stripe_session_dict = orjson.loads(orjson.dumps(stripe_session))