Skip to content

Commit

Permalink
fix nathan wang type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
azliu0 committed Apr 15, 2024
1 parent 9de7ad5 commit 5867ec2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 50 deletions.
63 changes: 27 additions & 36 deletions server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Initialize the Flask app."""

from typing import List, cast
from typing import List, Type, cast

import numpy
from apiflask import APIFlask
from flask import redirect, render_template
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from psycopg2.extensions import AsIs, register_adapter
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass


def addapt_numpy_float64(numpy_float64):
Expand All @@ -32,32 +33,31 @@ def addapt_numpy_int64(numpy_int64):
register_adapter(numpy.int64, addapt_numpy_int64)

cors = CORS()
db = SQLAlchemy()


# def init():
# documents = Document.query.order_by(Document.id.desc()).all()

# if len(documents) > 0:
# return
# # initialize with some default documents to create the index
# document = Document(
# "what is hackmit?",
# "HackMIT is a weekend-long event where thousands of students from around the
# world come together to work on cool new software and/or hardware projects.",
# "https://hackmit.org",
# "what is hackmit?",
# )
# db.session.add(document)
# document = Document(
# "what is blueprint?",
# "Blueprint is a weekend-long learnathon and hackathon for high school
# students hosted at MIT",
# "https://blueprint.hackmit.org",
# "what is blueprint?",
# )
# db.session.add(document)
# db.session.commit()


class Base(DeclarativeBase, MappedAsDataclass):
"""Base SQLAlchemy class. Don't use this class; use db.Model instead.
Since we use Flask SQLAlchemy, this class shouldn't be used directly.
Instead, use db.Model.
"""

pass


class ProperlyTypedSQLAlchemy(SQLAlchemy):
"""Temporary type hinting workaround for Flask SQLAlchemy.
This is a temporary workaround for the following issue:
https://github.com/pallets-eco/flask-sqlalchemy/issues/1312
This workaround may not be correct.
"""

Model: Type[Base]


db = SQLAlchemy(model_class=Base)
db = cast(ProperlyTypedSQLAlchemy, db)


def create_app():
Expand Down Expand Up @@ -94,15 +94,6 @@ def create_app():

app.register_blueprint(seed)

# from server.controllers.admin import update_embeddings

# on server start, embed some hard-coded documents and update index
# this doesn't work for some reason
# from server.controllers.admin import update_embeddings

# init()
# update_embeddings()

db.create_all()

@app.errorhandler(404)
Expand Down
14 changes: 7 additions & 7 deletions server/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def email():
db.session.add(thread)
db.session.commit()
email = Email(

Check failure on line 31 in server/cli.py

View workflow job for this annotation

GitHub Actions / Run tests

Argument missing for parameter "thread" (reportCallIssue)
datetime.datetime.now(datetime.timezone.utc),
"azliu@mit.edu",
subject,
body,
"message-id",
False,
thread.id,
date=datetime.datetime.now(datetime.timezone.utc),
sender="azliu@mit.edu",
subject=subject,
body=body,
message_id="message-id",
is_reply=False,
thread_id=thread.id,
)

if email is not None and thread is not None:
Expand Down
2 changes: 1 addition & 1 deletion server/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Document(db.Model):

__tablename__ = "Documents"

id: Mapped[str] = mapped_column(primary_key=True)
id: Mapped[str] = mapped_column(primary_key=True, init=False)
question: Mapped[str] = mapped_column(nullable=False)
label: Mapped[str] = mapped_column(nullable=False)
content: Mapped[str] = mapped_column(nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion server/models/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Email(db.Model):

__tablename__ = "Emails"

id: Mapped[str] = mapped_column(db.Integer, primary_key=True)
id: Mapped[str] = mapped_column(primary_key=True, init=False)
date: Mapped[DateTime] = mapped_column(nullable=False)
sender: Mapped[str] = mapped_column(nullable=False)
subject: Mapped[str] = mapped_column(nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion server/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Response(db.Model):
__tablename__ = "Responses"
__table_args__ = (UniqueConstraint("email_id", name="unique_email_id"),)

id: Mapped[str] = mapped_column(primary_key=True)
id: Mapped[str] = mapped_column(primary_key=True, init=False)
response: Mapped[str] = mapped_column(nullable=False)
questions: Mapped[List[str]] = mapped_column(nullable=False)
documents: Mapped[List[List[int]]] = mapped_column(nullable=False)
Expand Down
8 changes: 4 additions & 4 deletions server/models/thread.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Thread."""

from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING, List, Optional

from sqlalchemy import select
from sqlalchemy.ext.hybrid import hybrid_property
Expand All @@ -19,15 +19,15 @@ class Thread(db.Model):
id(str): The ID of the thread.
resolved(bool): Whether the thread is resolved.
last_email(int): The ID of the last email in the thread.
last_email(str): The ID of the last email in the thread.
emails(list): The emails in the thread.
"""

__tablename__ = "Threads"

id: Mapped[str] = mapped_column(primary_key=True)
id: Mapped[str] = mapped_column(primary_key=True, init=False)
last_email: Mapped[Optional[str]] = mapped_column(nullable=True)
resolved: Mapped[bool] = mapped_column(nullable=False, default=False)
last_email: Mapped[int] = mapped_column(nullable=False)

emails: Mapped[List[Email]] = relationship(
"Email",
Expand Down

0 comments on commit 5867ec2

Please sign in to comment.