Skip to content

Commit

Permalink
Upgrade to Python 3.11 (Mypy is still failing)
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Mar 5, 2024
1 parent b5e4016 commit 106f639
Show file tree
Hide file tree
Showing 26 changed files with 272 additions and 250 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ boxoffice/static/build
.pytest_cache
boxoffice/static/img/fa5-packed.svg
monkeytype.sqlite3

.env
.env.testing
.envrc
9 changes: 4 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.278
rev: v0.3.0
hooks:
- id: ruff
args: ['--fix', '--exit-non-zero-on-fix']
Expand All @@ -20,11 +20,10 @@ repos:
'--remove-duplicate-keys',
]
- repo: https://github.com/asottile/pyupgrade
rev: v3.9.0
rev: v3.15.1
hooks:
- id: pyupgrade
args:
['--keep-runtime-typing', '--py3-plus', '--py36-plus', '--py37-plus']
args: ['--keep-runtime-typing', '--py311-plus']
- repo: https://github.com/asottile/yesqa
rev: v1.5.0
hooks:
Expand Down Expand Up @@ -106,7 +105,7 @@ repos:
- id: flake8
additional_dependencies: *flake8deps
- repo: https://github.com/PyCQA/pylint
rev: v3.0.0a6
rev: v3.1.0
hooks:
- id: pylint
args: [
Expand Down
6 changes: 3 additions & 3 deletions boxoffice/assets/js/libs/jquery-ui-1.8.4.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -4925,9 +4925,9 @@ jQuery.effects ||
if (a == 'disabled')
this.headers
.add(this.headers.next())
[
b ? 'addClass' : 'removeClass'
]('ui-accordion-disabled ui-state-disabled');
[b ? 'addClass' : 'removeClass'](
'ui-accordion-disabled ui-state-disabled'
);
},
_keydown: function (a) {
if (!(this.options.disabled || a.altKey || a.ctrlKey)) {
Expand Down
4 changes: 1 addition & 3 deletions boxoffice/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List

from typing_extensions import TypedDict


Expand All @@ -9,7 +7,7 @@ class StateList(TypedDict):
short_code_text: str


indian_states: List[StateList] = [
indian_states: list[StateList] = [
{'short_code': 1, 'name': 'Jammu and Kashmir', 'short_code_text': 'JK'},
{'short_code': 2, 'name': 'Himachal Pradesh', 'short_code_text': 'HP'},
{'short_code': 3, 'name': 'Punjab', 'short_code_text': 'PB'},
Expand Down
4 changes: 1 addition & 3 deletions boxoffice/forms/category.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from typing import Optional

from baseframe import __, forms
from baseframe.forms.validators import StopValidation

Expand All @@ -28,7 +26,7 @@ def available_seq(form: CategoryForm, field: forms.Field) -> None:


class CategoryForm(forms.Form):
edit_obj: Optional[Category]
edit_obj: Category | None

title = forms.StringField(
__("Category title"),
Expand Down
9 changes: 5 additions & 4 deletions boxoffice/forms/order.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import Any, Callable, List, Optional
from collections.abc import Callable
from typing import Any

from werkzeug.datastructures import ImmutableMultiDict

Expand All @@ -11,7 +12,7 @@
__all__ = ['LineItemForm', 'BuyerForm', 'OrderSessionForm', 'InvoiceForm']


def trim(length: int) -> Callable[[Optional[str]], str]:
def trim(length: int) -> Callable[[str | None], str]:
"""
Return data trimmed to the given length.
Expand All @@ -20,7 +21,7 @@ def trim(length: int) -> Callable[[Optional[str]], str]:
field = forms.StringField(__("Some field"), filters=[trim(25)])
"""

def _inner(data: Optional[str]) -> str:
def _inner(data: str | None) -> str:
return str((data or '')[0:length])

return _inner
Expand All @@ -35,7 +36,7 @@ class LineItemForm(forms.Form):
)

@classmethod
def process_list(cls, line_items_json: List[Any]):
def process_list(cls, line_items_json: list[Any]):
"""
Return a list of LineItemForm objects.
Expand Down
7 changes: 3 additions & 4 deletions boxoffice/mailclient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from decimal import Decimal
from typing import Optional
from uuid import UUID

from flask import render_template
Expand All @@ -17,7 +16,7 @@
@rq.job('boxoffice')
def send_receipt_mail(
order_id: UUID,
subject: Optional[str] = None,
subject: str | None = None,
template: str = 'order_confirmation_mail.html.jinja2',
):
"""Send buyer a link to fill attendee details and get cash receipt."""
Expand Down Expand Up @@ -59,7 +58,7 @@ def send_participant_assignment_mail(
order_id: UUID,
menu_title: str,
team_member: str,
subject: Optional[str] = None,
subject: str | None = None,
):
with app.test_request_context():
if subject is None:
Expand Down Expand Up @@ -89,7 +88,7 @@ def send_participant_assignment_mail(

@rq.job('boxoffice')
def send_line_item_cancellation_mail(
line_item_id: UUID, refund_amount: Decimal, subject: Optional[str] = None
line_item_id: UUID, refund_amount: Decimal, subject: str | None = None
):
with app.test_request_context():
if subject is None:
Expand Down
2 changes: 1 addition & 1 deletion boxoffice/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# flake8: noqa

from datetime import datetime
from typing import Annotated, TypeAlias

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import DeclarativeBase, Mapped
from sqlalchemy_json import mutable_json_type
from typing_extensions import Annotated, TypeAlias
import sqlalchemy as sa

from coaster.sqlalchemy import (
Expand Down
4 changes: 2 additions & 2 deletions boxoffice/models/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING
from uuid import UUID

from . import BaseScopedNameMixin, Mapped, Model, relationship, sa
Expand All @@ -22,7 +22,7 @@ class Category(BaseScopedNameMixin, Model):
)
menu: Mapped[ItemCollection] = relationship(back_populates='categories')
seq: Mapped[int]
tickets: Mapped[List[Item]] = relationship(
tickets: Mapped[list[Item]] = relationship(
cascade='all, delete-orphan', back_populates='category'
)
parent: Mapped[ItemCollection] = sa.orm.synonym('menu')
Expand Down
29 changes: 15 additions & 14 deletions boxoffice/models/discount_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from __future__ import annotations

from collections.abc import Sequence
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, List, Optional, Sequence
from typing import TYPE_CHECKING, Any
from uuid import UUID
import secrets
import string
Expand Down Expand Up @@ -87,28 +88,28 @@ class DiscountPolicy(BaseScopedNameMixin, Model):
item_quantity_min: Mapped[int] = sa.orm.mapped_column(default=1)

# TODO: Add check constraint requiring percentage if is_price_based is false
percentage: Mapped[Optional[int]]
percentage: Mapped[int | None]
# price-based discount
is_price_based: Mapped[bool] = sa.orm.mapped_column(default=False)

discount_code_base: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(20))
secret: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(50))
discount_code_base: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(20))
secret: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(50))

# Coupons generated in bulk are not stored in the database during generation. This
# field allows specifying the number of times a coupon, generated in bulk, can be
# used This is particularly useful for generating referral discount coupons. For
# instance, one could generate a signed coupon and provide it to a user such that
# the user can share the coupon `n` times `n` here is essentially
# bulk_coupon_usage_limit.
bulk_coupon_usage_limit: Mapped[Optional[int]] = sa.orm.mapped_column(default=1)
bulk_coupon_usage_limit: Mapped[int | None] = sa.orm.mapped_column(default=1)

discount_coupons: Mapped[List[DiscountCoupon]] = relationship(
discount_coupons: Mapped[list[DiscountCoupon]] = relationship(
cascade='all, delete-orphan', back_populates='discount_policy'
)
tickets: Mapped[List[Item]] = relationship(
tickets: Mapped[list[Item]] = relationship(
secondary=item_discount_policy, back_populates='discount_policies'
)
prices: Mapped[List[Price]] = relationship(cascade='all, delete-orphan')
prices: Mapped[list[Price]] = relationship(cascade='all, delete-orphan')
line_items: DynamicMapped[LineItem] = relationship(
lazy='dynamic', back_populates='discount_policy'
)
Expand All @@ -132,7 +133,7 @@ class DiscountPolicy(BaseScopedNameMixin, Model):
}

def roles_for(
self, actor: Optional[User] = None, anchors: Sequence[Any] = ()
self, actor: User | None = None, anchors: Sequence[Any] = ()
) -> LazyRoleSet:
roles = super().roles_for(actor, anchors)
if (
Expand All @@ -155,7 +156,7 @@ def is_automatic(self) -> bool:
def is_coupon(self) -> bool:
return self.discount_type == DiscountTypeEnum.COUPON

def gen_signed_code(self, identifier: Optional[str] = None) -> str:
def gen_signed_code(self, identifier: str | None = None) -> str:
"""
Generate a signed code.
Expand All @@ -175,7 +176,7 @@ def is_signed_code_format(code) -> bool:
return len(code.split('.')) == 3 if code else False

@classmethod
def get_from_signed_code(cls, code, organization_id) -> Optional[DiscountPolicy]:
def get_from_signed_code(cls, code, organization_id) -> DiscountPolicy | None:
"""Return a discount policy given a valid signed code, None otherwise."""
if not cls.is_signed_code_format(code):
return None
Expand Down Expand Up @@ -205,7 +206,7 @@ def make_bulk(cls, discount_code_base, **kwargs) -> DiscountPolicy:
@classmethod
def get_from_ticket(
cls, ticket: Item, qty, coupon_codes: Sequence[str] = ()
) -> List[PolicyCoupon]:
) -> list[PolicyCoupon]:
"""
Return a list of (discount_policy, discount_coupon) tuples.
Expand Down Expand Up @@ -325,7 +326,7 @@ def __init__(self, *args, **kwargs):
discount_policy: Mapped[DiscountPolicy] = relationship(
back_populates='discount_coupons'
)
line_items: Mapped[List[LineItem]] = relationship(back_populates='discount_coupon')
line_items: Mapped[list[LineItem]] = relationship(back_populates='discount_coupon')

@classmethod
def is_signed_code_usable(cls, policy, code):
Expand All @@ -350,7 +351,7 @@ def update_used_count(self):
@dataclass
class PolicyCoupon:
policy: DiscountPolicy
coupon: Optional[DiscountCoupon]
coupon: DiscountCoupon | None


create_title_trgm_trigger = sa.DDL(
Expand Down
30 changes: 15 additions & 15 deletions boxoffice/models/invoice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING
from uuid import UUID

from baseframe import _
Expand Down Expand Up @@ -37,25 +37,25 @@ class Invoice(UuidMixin, BaseMixin, Model):
status: Mapped[int] = sa.orm.mapped_column(
sa.SmallInteger, default=InvoiceStatus.DRAFT
)
invoicee_name: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(255))
invoicee_company: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(255))
invoicee_email: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(254))
invoice_no: Mapped[Optional[int]]
invoicee_name: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(255))
invoicee_company: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(255))
invoicee_email: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(254))
invoice_no: Mapped[int | None]
fy_start_at: Mapped[timestamptz]
fy_end_at: Mapped[timestamptz]
invoiced_at: Mapped[Optional[timestamptz]]
street_address_1: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(255))
street_address_2: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(255))
city: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(255))
state: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(255))
invoiced_at: Mapped[timestamptz | None]
street_address_1: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(255))
street_address_2: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(255))
city: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(255))
state: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(255))
# ISO 3166-2 code. Eg: KA for Karnataka
state_code: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(3))
state_code: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(3))
# ISO country code
country_code: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(2))
postcode: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(8))
country_code: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(2))
postcode: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(8))
# GSTIN in the case of India
buyer_taxid: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(255))
seller_taxid: Mapped[Optional[str]] = sa.orm.mapped_column(sa.Unicode(255))
buyer_taxid: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(255))
seller_taxid: Mapped[str | None] = sa.orm.mapped_column(sa.Unicode(255))

customer_order_id: Mapped[UUID] = sa.orm.mapped_column(
sa.ForeignKey('customer_order.id'), index=True
Expand Down
Loading

0 comments on commit 106f639

Please sign in to comment.