Skip to content

Commit

Permalink
Mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronLieb committed Nov 12, 2024
1 parent 94c5c41 commit 15d41f2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion SmsClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, settings: Settings, db: Database | None = None):

@override
def send_message(self, to: str, text: str) -> None:
self.twilio_client.messages.create( # pyright: ignore [reportUnknownMemberType]
self.twilio_client.messages.create(
to=to, from_=settings.twilio_phone_number, body=text
)

Expand Down
2 changes: 1 addition & 1 deletion TextTestClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def handle_beta_message(self, from_: str, text: str):


if __name__ == "__main__":
client = TextTestClient()
client = TextTestClient(settings)

while True:
text = input()
Expand Down
8 changes: 4 additions & 4 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_db() -> Generator[Session]:


class Database:
def __init__(self):
def __init__(self) -> None:
db = next(get_db(), None)
if db is None:
raise Exception("Could not connect to database")
Expand Down Expand Up @@ -86,9 +86,9 @@ def update_reg(
if db_reg is None:
return None

db_reg.state = state # pyright: ignore [reportAttributeAccessIssue]
db_reg.state = state
if username is not None:
db_reg.username = username # pyright: ignore [reportAttributeAccessIssue]
db_reg.username = username
self.db.commit()

return db_reg
Expand Down Expand Up @@ -162,6 +162,6 @@ def set_winner(self, pic_id: int):
if pic is None:
return None

pic.winner = not pic.winner # pyright: ignore [reportAttributeAccessIssue]
pic.winner = not pic.winner
self.db.commit()
return pic
22 changes: 9 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# pyright: reportCallInDefaultInitializer=false

import logging
from typing import Annotated

Expand All @@ -25,7 +23,7 @@

logger = logging.getLogger(__name__)

models.Base.metadata.create_all(bind=engine) # pyright: ignore [reportAny]
models.Base.metadata.create_all(bind=engine)

app = FastAPI()
twilio_client = SmsClient(settings)
Expand Down Expand Up @@ -61,7 +59,7 @@ async def receive_message(
validator = RequestValidator(settings.twilio_auth_token)
form_ = await request.form()

if not validator.validate( # pyright: ignore [reportUnknownMemberType]
if not validator.validate(
str(request.url), form_, request.headers.get("X-Twilio-Signature", "")
):
raise HTTPException(status_code=400, detail="Error in Twilio Signature")
Expand Down Expand Up @@ -101,21 +99,19 @@ def images_page(
if prompt is None:
raise HTTPException(status_code=404, detail="Unable to find prompt")

if not db.get_submission_status(
user_hash, prompt.id # pyright: ignore [reportArgumentType]
):
if not db.get_submission_status(user_hash, prompt.id):
raise HTTPException(status_code=401, detail="No submission for this prompt")

pics = db.get_pics_by_prompt(prompt.id) # pyright: ignore [reportArgumentType]
pics = db.get_pics_by_prompt(prompt.id)

pics = [vars(pic) for pic in pics]
for pic in pics:
pic["click_url"] = pic["url"]

date_str: str = prompt.date.strftime("%b %-d, %Y") # pyright: ignore [reportAny]
date_str: str = prompt.date.strftime("%b %-d, %Y")

og = {"display": False}
winner = db.get_winner_by_prompt(prompt.id) # pyright: ignore [reportArgumentType]
winner = db.get_winner_by_prompt(prompt.id)
if winner is not None:
og["display"] = True
og["url"] = winner.url
Expand All @@ -132,7 +128,7 @@ def history_page(user_hash: str):
pics = db.get_pics_by_hash(user_hash)
html_list: list[str] = []
for pic in pics:
prompt = db.get_prompt(pic.prompt) # pyright: ignore [reportArgumentType]
prompt = db.get_prompt(pic.prompt)
assert prompt is not None
url = BASE_URL + "{}?n={}".format(user_hash, prompt.id)
html_list.append('<li><a href="{}">{}</a></li>'.format(url, prompt.prompt))
Expand Down Expand Up @@ -187,12 +183,12 @@ def winner_admin_page(
if prompt is None:
raise HTTPException(status_code=404, detail="Unable to find prompt")

pics = db.get_pics_by_prompt(prompt.id) # pyright: ignore [reportArgumentType]
pics = db.get_pics_by_prompt(prompt.id)
pics = [vars(pic) for pic in pics]
for pic in pics:
pic["click_url"] = BASE_URL + "win/{}".format(pic["id"])

date_str: str = prompt.date.strftime("%b %-d, %Y") # pyright: ignore [reportAny]
date_str: str = prompt.date.strftime("%b %-d, %Y")

return templates.TemplateResponse(
request=request,
Expand Down
46 changes: 28 additions & 18 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,58 @@
from typing import Optional

from sqlalchemy import Boolean, Column, Date, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, declarative_base, relationship
from sqlalchemy import (
Mapped,
DeclarativeBase,
Boolean,
Date,
ForeignKey,
Integer,
String,
)
from sqlalchemy.orm import mapped_column, declarative_base, relationship

Base = declarative_base()

class Base(DeclarativeBase):
__allow_unmapped__ = True


class User(Base):
__tablename__ = "users"

username = Column(String, primary_key=True)
phone = Column(String, unique=True)
active = Column(Boolean, default=True)
hash = Column(String, unique=True)
username = mapped_column(String, primary_key=True)
phone = mapped_column(String, unique=True)
active = mapped_column(Boolean, default=True)
hash = mapped_column(String, unique=True)

pics: Mapped[Optional["Pic"]] = relationship("Pic", back_populates="uploader")


class Registration(Base):
__tablename__ = "registrations"

phone = Column(String, primary_key=True)
username = Column(String)
state = Column(Integer)
phone = mapped_column(String, primary_key=True)
username = mapped_column(String)
state = mapped_column(Integer)


class Prompt(Base):
__tablename__ = "prompts"

id = Column(Integer, primary_key=True)
prompt = Column(String, nullable=False)
date = Column(Date, nullable=False)
id = mapped_column(Integer, primary_key=True)
prompt = mapped_column(String, nullable=False)
date = mapped_column(Date, nullable=False)

pics: Mapped[Optional["Pic"]] = relationship("Pic", back_populates="parent")


class Pic(Base):
__tablename__ = "pics"

id = Column(Integer, primary_key=True)
url = Column(String)
prompt = Column(Integer, ForeignKey("prompts.id"))
user = Column(String, ForeignKey("users.username"))
winner = Column(Boolean, default=False)
id = mapped_column(Integer, primary_key=True)
url = mapped_column(String)
prompt = mapped_column(Integer, ForeignKey("prompts.id"))
user = mapped_column(String, ForeignKey("users.username"))
winner = mapped_column(Boolean, default=False)

parent = relationship("Prompt", back_populates="pics")
uploader = relationship("User", back_populates="pics")
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mypy]
python_executable=venv/bin/python

0 comments on commit 15d41f2

Please sign in to comment.