Skip to content

Commit 07a969d

Browse files
authored
Merge pull request #178 from Geode-solutions/fix/mypy
fix(Mypy): add support for type checking
2 parents 3562ef7 + cb8c4fd commit 07a969d

File tree

7 files changed

+74
-41
lines changed

7 files changed

+74
-41
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ __pycache__
99
.vscode
1010
uploads
1111
node_modules
12-
schemas.json
12+
schemas.json
13+
.mypy_cache

.vscode/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
"."
44
],
55
"python.testing.unittestEnabled": false,
6-
"python.testing.pytestEnabled": true
6+
"python.testing.pytestEnabled": true,
7+
"mypy-type-checker.args": [
8+
"--config-file=mypy.ini"
9+
],
10+
"mypy-type-checker.interpreter": [
11+
"${workspaceFolder}/venv/bin/python"
12+
]
713
}

commitlint.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default {
2+
extends: ["@commitlint/config-angular"],
3+
rules: {
4+
"scope-empty": [2, "never"],
5+
"subject-empty": [2, "never"],
6+
"subject-max-length": [0],
7+
"body-leading-blank": [0],
8+
"footer-leading-blank": [0],
9+
"header-max-length": [0],
10+
"scope-case": [0],
11+
"subject-case": [0],
12+
"subject-full-stop": [0],
13+
"type-case": [0],
14+
"type-empty": [0],
15+
},
16+
}

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[mypy]
2+
strict = True
3+
files = src/

src/opengeodeweb_back/geode_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import os
33

44
# Third party imports
5-
import opengeode_geosciences as og_gs
6-
import opengeode as og
5+
import opengeode_geosciences as og_gs # type: ignore
6+
import opengeode as og # type: ignore
77
import werkzeug
88
import flask
99

src/opengeodeweb_back/geode_objects.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Standard library imports
22

33
# Third party imports
4-
import opengeode as og
5-
import opengeode_io as og_io
6-
import opengeode_inspector as og_inspector
7-
import opengeode_geosciences as og_gs
8-
import opengeode_geosciencesio as og_gs_io
9-
import geode_viewables as g_v
4+
import opengeode as og # type: ignore
5+
import opengeode_io as og_io # type: ignore
6+
import opengeode_inspector as og_inspector # type: ignore
7+
import opengeode_geosciences as og_gs # type: ignore
8+
import opengeode_geosciencesio as og_gs_io # type: ignore
9+
import geode_viewables as g_v # type: ignore
1010

1111
# Local application imports
1212

src/opengeodeweb_back/utils_functions.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,55 @@
44
import time
55
import uuid
66
import zipfile
7+
from collections.abc import Callable
8+
from typing import Any
79

810
# Third party imports
911
import flask
10-
import fastjsonschema
12+
import fastjsonschema # type: ignore
1113
import importlib.metadata as metadata
1214
import shutil
15+
from werkzeug.exceptions import HTTPException
1316
import werkzeug
1417

1518
# Local application imports
1619
from . import geode_functions
1720

1821

19-
def increment_request_counter(current_app):
22+
def increment_request_counter(current_app: flask.Flask) -> None:
2023
if "REQUEST_COUNTER" in current_app.config:
21-
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER"))
24+
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER", 0))
2225
REQUEST_COUNTER += 1
2326
current_app.config.update(REQUEST_COUNTER=REQUEST_COUNTER)
2427

2528

26-
def decrement_request_counter(current_app):
29+
def decrement_request_counter(current_app: flask.Flask) -> None:
2730
if "REQUEST_COUNTER" in current_app.config:
28-
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER"))
31+
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER", 0))
2932
REQUEST_COUNTER -= 1
3033
current_app.config.update(REQUEST_COUNTER=REQUEST_COUNTER)
3134

3235

33-
def update_last_request_time(current_app):
36+
def update_last_request_time(current_app: flask.Flask) -> None:
3437
if "LAST_REQUEST_TIME" in current_app.config:
3538
LAST_REQUEST_TIME = time.time()
3639
current_app.config.update(LAST_REQUEST_TIME=LAST_REQUEST_TIME)
3740

3841

39-
def before_request(current_app):
42+
def before_request(current_app: flask.Flask) -> None:
4043
increment_request_counter(current_app)
4144

4245

43-
def teardown_request(current_app):
46+
def teardown_request(current_app: flask.Flask) -> None:
4447
decrement_request_counter(current_app)
4548
update_last_request_time(current_app)
4649

4750

48-
def kill_task(current_app):
49-
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER"))
50-
LAST_PING_TIME = float(current_app.config.get("LAST_PING_TIME"))
51-
LAST_REQUEST_TIME = float(current_app.config.get("LAST_REQUEST_TIME"))
52-
MINUTES_BEFORE_TIMEOUT = float(current_app.config.get("MINUTES_BEFORE_TIMEOUT"))
51+
def kill_task(current_app: flask.Flask) -> None:
52+
REQUEST_COUNTER = int(current_app.config.get("REQUEST_COUNTER", 0))
53+
LAST_PING_TIME = float(current_app.config.get("LAST_PING_TIME", 0))
54+
LAST_REQUEST_TIME = float(current_app.config.get("LAST_REQUEST_TIME", 0))
55+
MINUTES_BEFORE_TIMEOUT = float(current_app.config.get("MINUTES_BEFORE_TIMEOUT", 0))
5356
current_time = time.time()
5457
minutes_since_last_request = (current_time - LAST_REQUEST_TIME) / 60
5558
minutes_since_last_ping = (current_time - LAST_PING_TIME) / 60
@@ -64,12 +67,12 @@ def kill_task(current_app):
6467
kill_server()
6568

6669

67-
def kill_server():
70+
def kill_server() -> None:
6871
print("Server timed out due to inactivity, shutting down...", flush=True)
6972
os._exit(0)
7073

7174

72-
def versions(list_packages: list):
75+
def versions(list_packages: list[str]) -> list[dict[str, str]]:
7376
list_with_versions = []
7477
for package in list_packages:
7578
list_with_versions.append(
@@ -78,7 +81,7 @@ def versions(list_packages: list):
7881
return list_with_versions
7982

8083

81-
def validate_request(request, schema):
84+
def validate_request(request: flask.Request, schema: dict[str, str]) -> None:
8285
json_data = request.get_json(force=True, silent=True)
8386

8487
if json_data is None:
@@ -92,22 +95,26 @@ def validate_request(request, schema):
9295
flask.abort(400, error_msg)
9396

9497

95-
def set_interval(func, sec, args=None):
96-
def func_wrapper():
97-
set_interval(func, sec, args)
98-
func(args)
98+
def set_interval(
99+
function: Callable[[Any], None], seconds: float, args: Any
100+
) -> threading.Timer:
101+
def function_wrapper() -> None:
102+
set_interval(function, seconds, args)
103+
function(args)
99104

100-
t = threading.Timer(sec, func_wrapper)
101-
t.daemon = True
102-
t.start()
103-
return t
105+
timer = threading.Timer(seconds, function_wrapper)
106+
timer.daemon = True
107+
timer.start()
108+
return timer
104109

105110

106-
def extension_from_filename(filename):
111+
def extension_from_filename(filename: str) -> str:
107112
return os.path.splitext(filename)[1][1:]
108113

109114

110-
def send_file(upload_folder, saved_files, new_file_name):
115+
def send_file(
116+
upload_folder: str, saved_files: str, new_file_name: str
117+
) -> flask.Response:
111118
if len(saved_files) == 1:
112119
mimetype = "application/octet-binary"
113120
else:
@@ -132,13 +139,13 @@ def send_file(upload_folder, saved_files, new_file_name):
132139
return response
133140

134141

135-
def handle_exception(e):
136-
response = e.get_response()
142+
def handle_exception(exception: HTTPException) -> flask.Response:
143+
response = exception.get_response()
137144
response.data = flask.json.dumps(
138145
{
139-
"code": e.code,
140-
"name": e.name,
141-
"description": e.description,
146+
"code": exception.code,
147+
"name": exception.name,
148+
"description": exception.description,
142149
}
143150
)
144151
response.content_type = "application/json"

0 commit comments

Comments
 (0)