Skip to content

Commit 232d32e

Browse files
committed
🚨 Address errors raised by ruff
1 parent 93e30d0 commit 232d32e

9 files changed

Lines changed: 29 additions & 25 deletions

File tree

mozilla_django_oidc_db/constants.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from collections.abc import Mapping
21

32
from .typing import EndpointFieldNames
43

54
# Mapping the configuration model fieldnames for endpoints to their
65
# corresponding names in the OIDC spec
7-
OIDC_MAPPING: Mapping[EndpointFieldNames, str] = {
6+
OIDC_MAPPING: dict[EndpointFieldNames, str] = {
87
"oidc_op_authorization_endpoint": "authorization_endpoint",
98
"oidc_op_token_endpoint": "token_endpoint",
109
"oidc_op_user_endpoint": "userinfo_endpoint",

mozilla_django_oidc_db/forms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
2-
from collections.abc import Mapping
2+
from collections.abc import Mapping, Sequence
3+
from typing import ClassVar
34
from urllib.parse import urljoin
45

56
from django import forms
@@ -15,7 +16,7 @@
1516

1617

1718
class OIDCProviderForm(forms.ModelForm):
18-
required_endpoints = [
19+
required_endpoints: ClassVar[Sequence[str]] = [
1920
"oidc_op_authorization_endpoint",
2021
"oidc_op_token_endpoint",
2122
"oidc_op_user_endpoint",

mozilla_django_oidc_db/setup_configuration/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from typing import Annotated, Literal
1+
from collections.abc import Sequence
2+
from typing import Annotated, ClassVar, Literal
23

4+
from django.db.models import Model
35
from django.utils.translation import gettext_lazy as _
46

57
from django_setup_configuration.fields import DjangoModelRef
@@ -76,7 +78,7 @@ class OIDCConfigProviderModel(ConfigurationModel):
7678
endpoint_config: OIDCProviderConfigUnion
7779

7880
class Meta:
79-
django_model_refs = {
81+
django_model_refs: ClassVar[dict[type[Model], Sequence[str]]] = {
8082
OIDCProvider: [
8183
"oidc_token_use_basic_auth",
8284
"oidc_use_nonce",
@@ -217,7 +219,7 @@ class AdminOIDCConfigurationModelItem(ConfigurationModel):
217219
)
218220

219221
class Meta:
220-
django_model_refs = {
222+
django_model_refs: ClassVar[dict[type[Model], Sequence[str]]] = {
221223
OIDCClient: [
222224
"oidc_rp_client_id",
223225
"oidc_rp_client_secret",
@@ -227,7 +229,7 @@ class Meta:
227229
"userinfo_claims_source",
228230
]
229231
}
230-
extra_kwargs = {
232+
extra_kwargs: ClassVar[dict[str, dict[str, object]]] = {
231233
"oidc_rp_client_id": {"examples": ["modify-this"]},
232234
"oidc_rp_client_secret": {"examples": ["modify-this"]},
233235
"oidc_rp_idp_sign_key": {"examples": ["modify-this"]},

mozilla_django_oidc_db/tests/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def keycloak_login(
3030
``state`` query parameters. Consume this with ``response = client.get(url)``.
3131
"""
3232
cm = Session() if session is None else nullcontext(session)
33-
with cm as session:
34-
login_page = session.get(login_url)
33+
with cm as _session:
34+
login_page = _session.get(login_url)
3535
assert login_page.status_code == 200
3636

3737
# process keycloak's login form and submit the username + password to
@@ -40,7 +40,7 @@ def keycloak_login(
4040
login_form = document("form#kc-form-login")
4141
submit_url = login_form.attr("action")
4242
assert isinstance(submit_url, str)
43-
login_response = session.post(
43+
login_response = _session.post(
4444
submit_url,
4545
data={
4646
"username": username,
@@ -52,6 +52,9 @@ def keycloak_login(
5252
)
5353

5454
assert login_response.status_code == 302
55-
assert (redirect_uri := login_response.headers["Location"]).startswith(host)
55+
56+
redirect_uri = login_response.headers["Location"]
57+
58+
assert redirect_uri.startswith(host)
5659

5760
return redirect_uri

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,6 @@ section-order = [
160160

161161
[tool.ruff.lint.isort.sections]
162162
"django" = ["django"]
163+
164+
[tool.ruff.lint.per-file-ignores]
165+
"*/migrations/*.py" = ["RUF012"]

testapp/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"USER": os.getenv("PGUSER", "mozilla_django_oidc_db"),
1919
"PASSWORD": os.getenv("PGPASSWORD", "mozilla_django_oidc_db"),
2020
"HOST": os.getenv("DB_HOST", "localhost"),
21-
"PORT": os.getenv("DB_PORT", 5432),
21+
"PORT": int(os.getenv("DB_PORT", "5432")),
2222
}
2323
}
2424

tests/test_admin_form.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import requests_mock
1010
from requests.exceptions import RequestException
1111

12-
from mozilla_django_oidc_db.forms import OIDCProviderForm
12+
from mozilla_django_oidc_db.constants import EndpointFieldNames
13+
from mozilla_django_oidc_db.forms import OIDC_MAPPING, OIDCProviderForm
1314
from mozilla_django_oidc_db.models import OIDCClient
1415
from tests.factories import UserFactory
1516

@@ -65,10 +66,8 @@ def test_derive_endpoints_extra_field():
6566
class ExtendedOIDCProviderForm(OIDCProviderForm):
6667
required_endpoints = OIDCProviderForm.required_endpoints
6768
# Define an extra field to derive from the configuration
68-
oidc_mapping = dict(
69-
**OIDCProviderForm.oidc_mapping,
70-
**{"logout_endpoint": "end_session_endpoint"},
71-
)
69+
oidc_mapping: dict[EndpointFieldNames, str] = OIDC_MAPPING
70+
oidc_mapping["oidc_op_logout_endpoint"] = "end_session_endpoint"
7271

7372
form = ExtendedOIDCProviderForm(data=form_data)
7473

@@ -89,7 +88,7 @@ class ExtendedOIDCProviderForm(OIDCProviderForm):
8988
# The endpoint that was added to the mapping on the extended form
9089
# should be present in the cleaned data
9190
assert (
92-
form.cleaned_data["logout_endpoint"]
91+
form.cleaned_data["oidc_op_logout_endpoint"]
9392
== "http://provider.com/auth/realms/master/protocol/openid-connect/logout"
9493
)
9594

tests/test_callback_flow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ def test_wrong_config_model_used(
224224
init_view = OIDCAuthenticationRequestInitView.as_view(identifier="test-oidc")
225225
init_view(auth_request)
226226
# there is only one state expected
227-
state_key = list(auth_request.session["oidc_states"].keys())[0]
227+
state_key = next(iter(auth_request.session["oidc_states"].keys()))
228228
callback_url = reverse("oidc_authentication_callback")
229229
session = client.session
230-
for key in auth_request.session.keys():
230+
for key in auth_request.session.keys(): # ruff: ignore[SIM118]
231231
session[key] = auth_request.session[key]
232232
session.save()
233233

tests/test_logout.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,4 @@ def test_logout_response_has_redirect(dummy_config: OIDCClient, requests_mock):
9797
headers={"Location": "http://testserver/endpoint-that-does-not-exist"},
9898
)
9999

100-
try:
101-
do_op_logout(dummy_config, id_token="dummy-id-token")
102-
except Exception:
103-
pytest.fail("Logout should not crash")
100+
do_op_logout(dummy_config, id_token="dummy-id-token")

0 commit comments

Comments
 (0)