Skip to content

Commit 367110c

Browse files
authored
Typecheck scripts too (#159)
* typecheck the scripts with mypy * add more (but not all) of the annotations flagged when running with TYPECHECK_OPTIONS="--strict" * fix merge issues that created lint errors * bump and specify minimum version of major packages; resolves mypy errors
1 parent 022beeb commit 367110c

File tree

6 files changed

+361
-262
lines changed

6 files changed

+361
-262
lines changed

backend/Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@ lint: uv.lock
2020
typecheck: uv.lock
2121
$(PYTHON) run ty check $(TYPECHECK_OPTIONS)
2222

23-
typecheck-mypy: uv.lock
24-
$(PYTHON) run mypy $(TYPECHECK_OPTIONS) -p tenantfirstaid --python-executable .venv/bin/python3 --check-untyped-defs
23+
MYPY_CMD := $(PYTHON) run mypy $(TYPECHECK_OPTIONS)
24+
MYPY_ARGS := --python-executable .venv/bin/python3 --pretty --check-untyped-defs
25+
26+
typecheck-mypy-scripts: uv.lock
27+
$(MYPY_CMD) -p scripts $(MYPY_ARGS)
28+
29+
typecheck-mypy-tenantfirstaid: uv.lock
30+
$(MYPY_CMD) -p tenantfirstaid $(MYPY_ARGS)
31+
32+
typecheck-mypy: typecheck-mypy-tenantfirstaid typecheck-mypy-scripts
2533

2634
typecheck-pyrefly: uv.lock
2735
$(PYTHON) run pyrefly check $(TYPECHECK_OPTIONS) --python-interpreter .venv/bin/python3

backend/pyproject.toml

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ name = "tenant-first-aid"
33
version = "0.2.0"
44
requires-python = ">=3.12"
55
dependencies = [
6-
"flask",
7-
"valkey",
8-
"gunicorn",
9-
"google-auth",
10-
"google-genai",
11-
"google-cloud-aiplatform",
6+
"flask>=3.1.1",
7+
"valkey>=6.1.0",
8+
"gunicorn>=23.0.0",
9+
"google-auth>=2.40.3",
10+
"google-genai>=1.28.0",
11+
"google-cloud-aiplatform>=1.106.0",
1212
"openai==1.89",
1313
"jsonlines",
1414
"simplejson",
1515
"python-dotenv",
1616
"pandas>=2.3.0",
17-
"vertexai",
17+
"vertexai>=1.43.0",
1818
]
1919

2020
[tool.setuptools.packages.find]
@@ -27,16 +27,20 @@ build-backend = "setuptools.build_meta"
2727

2828
[dependency-groups]
2929
dev = [
30-
"ipdb>=0.13.13",
31-
"mypy>=1.16.1",
32-
"pyrefly>=0.21.0",
33-
"pytest>=8.4.0",
34-
"pytest-cov>=6.1.1",
35-
"pytest-mock>=3.14.1",
36-
"ruff>=0.12.0",
37-
"ty>=0.0.1a11",
38-
"types-Flask>=1.1.6",
39-
"types-simplejson>=3.20.0.20250326",
30+
"ipdb>=0.13.13",
31+
"mypy>=1.16.1",
32+
"pandas-stubs>=2.2.3.250527",
33+
"pyrefly>=0.21.0",
34+
"pytest>=8.4.0",
35+
"pytest-cov>=6.1.1",
36+
"pytest-mock>=3.14.1",
37+
"ruff>=0.12.0",
38+
"ty>=0.0.1a11",
39+
"types-Flask>=1.1.6",
40+
"types-simplejson>=3.20.0.20250326",
4041
]
4142

42-
gen_convo = ["pandas", "openai"]
43+
gen_convo = [
44+
"pandas",
45+
"openai",
46+
]

backend/scripts/create_vector_store.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
from pathlib import Path
33
from openai import OpenAI
44

5-
6-
if Path(".env").exists():
5+
dotenv_path = Path(__file__).parent.parent / ".env"
6+
if dotenv_path.exists():
77
from dotenv import load_dotenv
88

9-
load_dotenv(override=True)
9+
load_dotenv(dotenv_path=dotenv_path, override=True)
1010

1111
API_KEY = os.getenv("OPENAI_API_KEY", os.getenv("GITHUB_API_KEY"))
1212

@@ -64,11 +64,11 @@
6464
path.write_text(path.read_text(encoding="utf-8"), encoding="utf-8")
6565

6666
print(f"Uploading {file_path} to vector store '{vector_store.name}'.")
67-
file = client.files.create(
67+
file_obj = client.files.create(
6868
file=open(file_path, "rb"),
6969
purpose="assistants",
7070
)
71-
file_ids.append(file.id)
71+
file_ids.append(file_obj.id)
7272

7373
# Add files to the vector store
7474
batch_upload = client.vector_stores.file_batches.create(

backend/tenantfirstaid/chat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(self):
5656
system_instruction=DEFAULT_INSTRUCTIONS,
5757
)
5858

59-
def prepare_developer_instructions(self, city: str, state: str):
59+
def prepare_developer_instructions(self, city: str, state: str) -> str:
6060
# Add city and state filters if they are set
6161
instructions = DEFAULT_INSTRUCTIONS
6262
instructions += (
@@ -117,11 +117,11 @@ def generate_gemini_chat_response(
117117

118118

119119
class ChatView(View):
120-
def __init__(self, tenant_session):
120+
def __init__(self, tenant_session) -> None:
121121
self.tenant_session = tenant_session
122122
self.chat_manager = ChatManager()
123123

124-
def dispatch_request(self, *args, **kwargs):
124+
def dispatch_request(self, *args, **kwargs) -> Response:
125125
data = request.json
126126
user_msg = data["message"]
127127

backend/tenantfirstaid/session.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TenantSessionData(TypedDict):
1919

2020
# The class to manage tenant sessions using Valkey and Flask sessions
2121
class TenantSession:
22-
def __init__(self):
22+
def __init__(self) -> None:
2323
print(
2424
"Connecting to Valkey:",
2525
{
@@ -42,13 +42,13 @@ def __init__(self):
4242

4343
# Retrieves the session ID from Flask session or creates a new one
4444
def get_flask_session_id(self) -> str:
45-
session_id = session.get("session_id")
45+
session_id: str | None = session.get("session_id")
4646
if not session_id:
4747
session_id = str(uuid.uuid4())
4848
session["session_id"] = session_id
4949

5050
@after_this_request
51-
def save_session(response):
51+
def save_session(response: Response) -> Response:
5252
session.modified = True
5353
return response
5454

@@ -61,9 +61,12 @@ def get(self) -> TenantSessionData:
6161
if not saved_session:
6262
return self.getNewSessionData()
6363

64-
return json.loads(s=saved_session)
64+
obj = json.loads(s=saved_session)
65+
return TenantSessionData(
66+
city=obj["city"], state=obj["state"], messages=obj["messages"]
67+
)
6568

66-
def set(self, value: TenantSessionData):
69+
def set(self, value: TenantSessionData) -> None:
6770
session_id = self.get_flask_session_id()
6871
self.db_con.set(session_id, json.dumps(value))
6972

@@ -76,7 +79,7 @@ class InitSessionView(View):
7679
def __init__(self, tenant_session: TenantSession):
7780
self.tenant_session = tenant_session
7881

79-
def dispatch_request(self, *args, **kwargs):
82+
def dispatch_request(self, *args, **kwargs) -> Response:
8083
data: Dict[str, Any] = request.json
8184
session_id: Optional[str] = self.tenant_session.get_flask_session_id()
8285

0 commit comments

Comments
 (0)