Skip to content

Commit 9d4e4f4

Browse files
committed
Fix pyright issues
Add missing type annotations to functions that require them.
1 parent 80f75b7 commit 9d4e4f4

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

mavis/reporting/helpers/auth_helper.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from datetime import datetime, timedelta, timezone
22
import jwt
3-
import urllib
3+
from urllib.parse import quote
4+
from typing import Any
45

56
from flask import (
67
request,
78
session,
89
redirect,
910
current_app,
1011
)
12+
from flask.sessions import SessionMixin
1113

1214
from functools import wraps
1315

@@ -34,7 +36,7 @@ def decorated_function(*args, **kwargs):
3436

3537
log_user_in(verification_data, session)
3638
return redirect(
37-
url_helper.url_without_param(request.full_path, "code")
39+
str(url_helper.url_without_param(request.full_path, "code"))
3840
)
3941
except KeyError:
4042
pass
@@ -44,9 +46,9 @@ def decorated_function(*args, **kwargs):
4446
return_url = url_helper.externalise_current_url(current_app, request)
4547
target_url = mavis_helper.mavis_url(
4648
current_app,
47-
"/start?redirect_uri=" + urllib.parse.quote(return_url),
49+
"/start?redirect_uri=" + quote(return_url),
4850
)
49-
return redirect(target_url)
51+
return redirect(str(target_url))
5052

5153
return f(*args, **kwargs)
5254

@@ -85,7 +87,7 @@ def encode_jwt(payload, current_app=current_app):
8587
return jwt.encode(payload, secret, algorithm="HS512")
8688

8789

88-
def log_user_in(data, session=session):
90+
def log_user_in(data: dict[str, Any], session: SessionMixin):
8991
jwt_data = data["jwt_data"]
9092

9193
session["last_visit"] = datetime.now().astimezone(timezone.utc)

mavis/reporting/helpers/mavis_helper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from http import HTTPStatus
22
import requests
33
import urllib.parse
4-
import werkzeug
4+
from werkzeug.exceptions import Unauthorized
55

66
from flask import redirect
77

@@ -49,7 +49,7 @@ def api_call(current_app, session, path, params={}):
4949
response = get_request(url, headers=headers)
5050
if response.status_code in [HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN]:
5151
session.clear()
52-
raise (werkzeug.exceptions.Unauthorized)
52+
raise Unauthorized()
5353

5454
return response
5555

@@ -60,7 +60,7 @@ def login_and_return_after(current_app, return_url):
6060
"/start?redirect_uri=" + urllib.parse.quote_plus(return_url),
6161
)
6262
current_app.logger.warning("REDIRECTING TO %s", target_url)
63-
return redirect(target_url)
63+
return redirect(str(target_url))
6464

6565

6666
def get_request(url, headers={}):

mavis/reporting/helpers/url_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from urllib.parse import urlparse, parse_qs, parse_qsl, urlencode, urlunparse, urljoin
22

33

4-
def url_without_param(url, param):
4+
def url_without_param(url: str, param: str) -> str:
55
parsed_url = urlparse(url)
66
query_params_as_dict = parse_qs(parsed_url.query)
77
if param in query_params_as_dict:
@@ -16,7 +16,7 @@ def url_without_param(url, param):
1616
return url
1717

1818

19-
def externalise_current_url(current_app, request):
19+
def externalise_current_url(current_app, request) -> str:
2020
return urljoin(
2121
current_app.config["ROOT_URL"] or request.host_url, request.full_path
2222
)

mavis/reporting/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def download():
5151

5252

5353
@main.errorhandler(404)
54-
def page_not_found():
54+
def page_not_found(_error):
5555
return render_template("errors/404.html"), 404
5656

5757

tests/helpers/test_auth_helper.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from os import urandom
2-
2+
from typing import cast
33
from datetime import datetime, timedelta, timezone
44

5+
from flask.sessions import SessionMixin
56
from mavis.reporting.helpers import auth_helper
67

78

@@ -57,7 +58,7 @@ def test_that_log_user_in_sets_last_visit_to_now(app):
5758
with app.app_context():
5859
configure_app(app)
5960
mock_session = {}
60-
auth_helper.log_user_in(mock_user_info(), mock_session)
61+
auth_helper.log_user_in(mock_user_info(), cast(SessionMixin, mock_session))
6162
assert mock_session["last_visit"] is not None
6263
assert datetime.now().astimezone(timezone.utc) - mock_session[
6364
"last_visit"
@@ -68,7 +69,7 @@ def test_that_log_user_in_copies_cis2_info_from_the_given_data(app):
6869
mock_session = {}
6970
with app.app_context():
7071
configure_app(app)
71-
auth_helper.log_user_in(mock_user_info(), mock_session)
72+
auth_helper.log_user_in(mock_user_info(), cast(SessionMixin, mock_session))
7273
assert mock_session["cis2_info"] == mock_user_info()["jwt_data"]["cis2_info"]
7374

7475

@@ -78,7 +79,7 @@ def test_that_log_user_in_copies_user_from_the_given_data(app):
7879

7980
with app.app_context():
8081
configure_app(app)
81-
auth_helper.log_user_in(fake_data, mock_session)
82+
auth_helper.log_user_in(fake_data, cast(SessionMixin, mock_session))
8283
assert mock_session["user"] == fake_data["jwt_data"]["user"]
8384

8485

@@ -89,7 +90,7 @@ def test_that_log_user_in_sets_minimal_jwt(
8990
fake_data = mock_user_info()
9091
with app.app_context():
9192
configure_app(app)
92-
auth_helper.log_user_in(fake_data, mock_session)
93+
auth_helper.log_user_in(fake_data, cast(SessionMixin, mock_session))
9394
assert mock_session["jwt"] is not None
9495
jwt_payload = auth_helper.decode_jwt(mock_session["jwt"])
9596

tests/helpers/test_mavis_helper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from http import HTTPStatus
33
import pytest
4-
import werkzeug
4+
from werkzeug.exceptions import Unauthorized
55

66
from flask import current_app
77
from unittest.mock import patch
@@ -60,7 +60,7 @@ def __init__(self, **kwargs):
6060
self.json_obj = kwargs.get("json_obj", None)
6161

6262
def json(self):
63-
return self.json_obj or json.loads(self.text)
63+
return self.json_obj or json.loads(self.text or "{}")
6464

6565

6666
def test_that_verify_auth_code_is_called_correctly(
@@ -146,7 +146,7 @@ def test_that_an_unauthorized_api_call_raises_an_exception_and_clears_the_sessio
146146
"mavis.reporting.helpers.mavis_helper.get_request",
147147
return_value=mock_response,
148148
):
149-
with pytest.raises(werkzeug.exceptions.Unauthorized):
149+
with pytest.raises(Unauthorized):
150150
mavis_helper.api_call(
151151
app,
152152
mock_session,

0 commit comments

Comments
 (0)