Skip to content

Commit 897bef9

Browse files
committed
add python 3 type hints
1 parent cf95e04 commit 897bef9

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

backend/deploy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import shutil
44
import zipfile
5+
from typing import Dict, List, Tuple, Union
56

67
# Third-party imports
78
from flask import request
@@ -11,7 +12,7 @@
1112
from main import basedir
1213

1314
class DeployResource(Resource):
14-
def post(self):
15+
def post(self) -> Union[Dict[str, Union[str, int, List[str]]], Tuple[Dict[str, str], int]]:
1516
"""Upload and extract a zip file to the deploy directory"""
1617
if 'file' not in request.files:
1718
return {'error': 'No file provided'}, 400

backend/main.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import argparse
33
import logging
44
import os
5+
from typing import Tuple, Union
56

67
# Third-party imports
7-
from flask import Flask, jsonify, request, send_from_directory
8+
from flask import Flask, jsonify, request, send_from_directory, Response
89
from flask_restful import Api
910
from flask_sqlalchemy import SQLAlchemy
1011

@@ -23,14 +24,14 @@
2324

2425
# Add CORS headers
2526
@app.after_request
26-
def after_request(response):
27+
def after_request(response: Response) -> Response:
2728
response.headers.add('Access-Control-Allow-Origin', '*')
2829
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
2930
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
3031
return response
3132

3233
@app.before_request
33-
def handle_preflight():
34+
def handle_preflight() -> Union[Response, None]:
3435
if request.method == "OPTIONS":
3536
response = jsonify({'message': 'OK'})
3637
response.headers.add('Access-Control-Allow-Origin', '*')
@@ -53,7 +54,7 @@ def handle_preflight():
5354

5455
# API health check endpoint to distinguish from static file serving
5556
@app.route('/api/status')
56-
def api_status():
57+
def api_status() -> Response:
5758
"""Health check endpoint to identify backend server"""
5859
return jsonify({
5960
'status': 'ok',
@@ -65,7 +66,7 @@ def api_status():
6566
@app.route('/blocks/', defaults={'path': ''})
6667
@app.route('/blocks/<path:path>')
6768
@app.route('/blocks//<path:path>') # Handle double slash
68-
def serve_frontend(path):
69+
def serve_frontend(path: str) -> Union[Response, Tuple[Response, int]]:
6970
"""Serve static assets from dist/ directory with base path"""
7071
# Normalize path - remove leading slashes and clean up double slashes
7172
path = path.lstrip('/')
@@ -101,7 +102,7 @@ def serve_frontend(path):
101102

102103
@app.route('/', defaults={'path': ''})
103104
@app.route('/<path:path>')
104-
def serve_static(path):
105+
def serve_static(path: str) -> Union[Response, Tuple[Response, int]]:
105106
"""Serve static assets from dist/ directory"""
106107
# If path is empty, serve index.html
107108
if path == '':

backend/storage.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Standard library imports
2+
from typing import Dict, List, Tuple, Union
3+
14
# Third-party imports
25
from flask import request
36
from flask_restful import Resource
@@ -11,7 +14,7 @@ class StorageEntry(db.Model):
1114
entry_key = db.Column(db.String(255), nullable=False, unique=True)
1215
entry_value = db.Column(db.Text, nullable=False)
1316

14-
def to_dict(self):
17+
def to_dict(self) -> Dict[str, str]:
1518
return {
1619
'key': self.entry_key,
1720
'value': self.entry_value
@@ -22,15 +25,15 @@ class StorageFile(db.Model):
2225
file_path = db.Column(db.String(500), nullable=False, unique=True)
2326
file_content = db.Column(db.Text, nullable=False)
2427

25-
def to_dict(self):
28+
def to_dict(self) -> Dict[str, str]:
2629
return {
2730
'path': self.file_path,
2831
'content': self.file_content
2932
}
3033

3134
# Storage Resources for key-value and file operations
3235
class StorageEntryResource(Resource):
33-
def get(self, entry_key):
36+
def get(self, entry_key: str) -> Dict[str, str]:
3437
"""Fetch entry value by key"""
3538
entry = StorageEntry.query.filter_by(entry_key=entry_key).first()
3639
if entry:
@@ -40,7 +43,7 @@ def get(self, entry_key):
4043
default_value = request.args.get('default', '')
4144
return {'value': default_value}
4245

43-
def post(self, entry_key):
46+
def post(self, entry_key: str) -> Union[Dict[str, str], Tuple[Dict[str, str], int]]:
4447
"""Save entry value"""
4548
data = request.get_json()
4649
if not data or 'value' not in data:
@@ -61,7 +64,7 @@ def post(self, entry_key):
6164
return {'error': 'Failed to save entry'}, 500
6265

6366
class StorageResource(Resource):
64-
def get(self, path):
67+
def get(self, path: str) -> Union[Dict[str, Union[List[str], str]], Tuple[Dict[str, str], int]]:
6568
"""Get file content or list directory based on path"""
6669
# Handle empty path as root directory
6770
if not path:
@@ -107,7 +110,7 @@ def get(self, path):
107110
else:
108111
return {'error': 'File not found'}, 404
109112

110-
def post(self, path):
113+
def post(self, path: str) -> Union[Dict[str, str], Tuple[Dict[str, str], int]]:
111114
"""Save file content (only for files, not directories)"""
112115
if path.endswith('/'):
113116
return {'error': 'Cannot save content to a directory path'}, 400
@@ -130,7 +133,7 @@ def post(self, path):
130133
db.session.rollback()
131134
return {'error': 'Failed to save file'}, 500
132135

133-
def delete(self, path):
136+
def delete(self, path: str) -> Union[Dict[str, str], Tuple[Dict[str, str], int]]:
134137
"""Delete file or directory"""
135138
if path.endswith('/'):
136139
# Delete directory (all files starting with this path)
@@ -152,7 +155,7 @@ def delete(self, path):
152155
return {'error': 'Failed to delete'}, 500
153156

154157
class StorageFileRenameResource(Resource):
155-
def post(self):
158+
def post(self) -> Union[Dict[str, str], Tuple[Dict[str, str], int]]:
156159
"""Rename file or directory"""
157160
data = request.get_json()
158161
if not data or 'old_path' not in data or 'new_path' not in data:
@@ -182,7 +185,7 @@ def post(self):
182185
return {'error': 'Failed to rename'}, 500
183186

184187
class StorageRootResource(Resource):
185-
def get(self):
188+
def get(self) -> Dict[str, List[str]]:
186189
"""List all top-level files and directories"""
187190
files = StorageFile.query.all()
188191
# Extract top-level files and directories

0 commit comments

Comments
 (0)