From d8158d6a72643b29c37f507cd03f3ef77c8c10b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Thu, 25 May 2023 13:37:58 +0200 Subject: [PATCH] Use ruff linter --- .pre-commit-config.yaml | 11 +++-------- canaille/__init__.py | 1 - canaille/app/__init__.py | 2 -- canaille/app/commands.py | 2 -- canaille/app/models.py | 13 ++++++------- canaille/backends/ldap/backend.py | 1 - canaille/backends/ldap/ldapobject.py | 2 +- pyproject.toml | 3 +++ tests/app/test_mails.py | 2 +- tests/core/test_profile_creation.py | 4 ++-- tests/core/test_profile_edition.py | 4 ++-- tests/core/test_profile_photo.py | 4 ++-- tests/core/test_profile_settings.py | 4 ++-- tests/oidc/test_authorization_code_flow.py | 4 ++-- tests/oidc/test_client_admin.py | 4 +--- tests/oidc/test_consent.py | 4 ++-- tests/oidc/test_dynamic_client_registration.py | 2 +- .../test_dynamic_client_registration_management.py | 2 +- tests/oidc/test_token_admin.py | 4 ++-- 19 files changed, 31 insertions(+), 42 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6a16ad32..6bd2df8c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,15 +7,10 @@ repos: - id: trailing-whitespace - id: end-of-file-fixer - id: check-toml - - repo: https://github.com/PyCQA/autoflake - rev: v2.1.1 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: 'v0.0.269' hooks: - - id: autoflake - - repo: https://github.com/hadialqattan/pycln - rev: v2.1.3 - hooks: - - id: pycln - args: [--all] + - id: ruff - repo: https://github.com/asottile/reorder_python_imports rev: v3.9.0 hooks: diff --git a/canaille/__init__.py b/canaille/__init__.py index a58193b7..379b213e 100644 --- a/canaille/__init__.py +++ b/canaille/__init__.py @@ -5,7 +5,6 @@ from logging.config import dictConfig import toml from flask import Flask from flask import g -from flask import request from flask import session from flask_themer import FileSystemThemeLoader from flask_themer import render_template diff --git a/canaille/app/__init__.py b/canaille/app/__init__.py index 0f5aae4e..6517dba2 100644 --- a/canaille/app/__init__.py +++ b/canaille/app/__init__.py @@ -3,10 +3,8 @@ import hashlib import json import re -from canaille.app import models from flask import current_app from flask import request -from flask_babel import gettext as _ def obj_to_b64(obj): diff --git a/canaille/app/commands.py b/canaille/app/commands.py index c532811b..09abd64b 100644 --- a/canaille/app/commands.py +++ b/canaille/app/commands.py @@ -10,8 +10,6 @@ from flask.cli import with_appcontext def with_backendcontext(func): @functools.wraps(func) def _func(*args, **kwargs): - from canaille.backends import Backend - if not current_app.config["TESTING"]: # pragma: no cover with Backend.get().session(): result = func(*args, **kwargs) diff --git a/canaille/app/models.py b/canaille/app/models.py index 0326f80f..6e08b101 100644 --- a/canaille/app/models.py +++ b/canaille/app/models.py @@ -1,7 +1,6 @@ -# nopycln: file -from canaille.backends.ldap.models import AuthorizationCode -from canaille.backends.ldap.models import Client -from canaille.backends.ldap.models import Consent -from canaille.backends.ldap.models import Group -from canaille.backends.ldap.models import Token -from canaille.backends.ldap.models import User +from canaille.backends.ldap.models import AuthorizationCode # noqa: F401 +from canaille.backends.ldap.models import Client # noqa: F401 +from canaille.backends.ldap.models import Consent # noqa: F401 +from canaille.backends.ldap.models import Group # noqa: F401 +from canaille.backends.ldap.models import Token # noqa: F401 +from canaille.backends.ldap.models import User # noqa: F401 diff --git a/canaille/backends/ldap/backend.py b/canaille/backends/ldap/backend.py index 08f50b6c..0ffcc504 100644 --- a/canaille/backends/ldap/backend.py +++ b/canaille/backends/ldap/backend.py @@ -133,7 +133,6 @@ class LDAPBackend(Backend): @classmethod def validate(cls, config): - from canaille.app.configuration import ConfigurationException from canaille.app import models try: diff --git a/canaille/backends/ldap/ldapobject.py b/canaille/backends/ldap/ldapobject.py index 4fd2faad..05068b92 100644 --- a/canaille/backends/ldap/ldapobject.py +++ b/canaille/backends/ldap/ldapobject.py @@ -418,7 +418,7 @@ class LDAPObject(metaclass=LDAPObjectMetaclass): formatted_changes = { name: value for name, value in python_attrs_to_ldap(changes).items() - if value is not None and value != None + if value is not None } attributes = [(name, values) for name, values in formatted_changes.items()] conn.add_s(self.id, attributes) diff --git a/pyproject.toml b/pyproject.toml index 6261338f..ffab72cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -110,6 +110,9 @@ source = [ omit = [".tox/*"] branch = true +[tool.ruff] +ignore = ["E501", "E722"] + [tool.tox] legacy_tox_ini = """ [tox] diff --git a/tests/app/test_mails.py b/tests/app/test_mails.py index 9f0cb07b..f61a7f50 100644 --- a/tests/app/test_mails.py +++ b/tests/app/test_mails.py @@ -93,7 +93,7 @@ def test_mail_with_default_no_logo(testclient, logged_admin, smtpd): testclient.app.config["LOGO"] = None assert len(smtpd.messages) == 0 - res = testclient.get(f"/admin/mail") + res = testclient.get("/admin/mail") res.form["email"] = "test@test.com" res = res.form.submit() assert ( diff --git a/tests/core/test_profile_creation.py b/tests/core/test_profile_creation.py index c5138be7..16e0bcb5 100644 --- a/tests/core/test_profile_creation.py +++ b/tests/core/test_profile_creation.py @@ -65,9 +65,9 @@ def test_user_creation_edition_and_deletion( def test_profile_creation_dynamic_validation(testclient, logged_admin, user): - res = testclient.get(f"/profile") + res = testclient.get("/profile") res = testclient.post( - f"/profile", + "/profile", { "csrf_token": res.form["csrf_token"].value, "email": "john@doe.com", diff --git a/tests/core/test_profile_edition.py b/tests/core/test_profile_edition.py index 8df7a487..bb2f22a9 100644 --- a/tests/core/test_profile_edition.py +++ b/tests/core/test_profile_edition.py @@ -178,9 +178,9 @@ def test_edition_remove_fields( def test_profile_edition_dynamic_validation(testclient, logged_admin, user): - res = testclient.get(f"/profile/admin") + res = testclient.get("/profile/admin") res = testclient.post( - f"/profile/admin", + "/profile/admin", { "csrf_token": res.form["csrf_token"].value, "email": "john@doe.com", diff --git a/tests/core/test_profile_photo.py b/tests/core/test_profile_photo.py index 0f13ad18..d6a4e30d 100644 --- a/tests/core/test_profile_photo.py +++ b/tests/core/test_profile_photo.py @@ -35,12 +35,12 @@ def test_photo(testclient, user, jpeg_photo): def test_photo_invalid_user(testclient, user): - res = testclient.get("/profile/invalid/photo", status=404) + testclient.get("/profile/invalid/photo", status=404) def test_photo_absent(testclient, user): assert not user.photo - res = testclient.get("/profile/user/photo", status=404) + testclient.get("/profile/user/photo", status=404) def test_photo_invalid_path(testclient, user): diff --git a/tests/core/test_profile_settings.py b/tests/core/test_profile_settings.py index 82951b86..1b817b4d 100644 --- a/tests/core/test_profile_settings.py +++ b/tests/core/test_profile_settings.py @@ -43,9 +43,9 @@ def test_edition( def test_profile_settings_edition_dynamic_validation(testclient, logged_admin): - res = testclient.get(f"/profile/admin/settings") + res = testclient.get("/profile/admin/settings") res = testclient.post( - f"/profile/admin/settings", + "/profile/admin/settings", { "csrf_token": res.form["csrf_token"].value, "password1": "short", diff --git a/tests/oidc/test_authorization_code_flow.py b/tests/oidc/test_authorization_code_flow.py index ed371f82..80b523b6 100644 --- a/tests/oidc/test_authorization_code_flow.py +++ b/tests/oidc/test_authorization_code_flow.py @@ -101,7 +101,7 @@ def test_authorization_code_flow( def test_invalid_client(testclient, logged_user, keypair): - res = testclient.get( + testclient.get( "/oauth/authorize", params=dict( response_type="code", @@ -885,7 +885,7 @@ def test_refresh_token_with_invalid_user(testclient, client): params = parse_qs(urlsplit(res.location).query) code = params["code"][0] - authcode = models.AuthorizationCode.get(code=code) + models.AuthorizationCode.get(code=code) res = testclient.post( "/oauth/token", diff --git a/tests/oidc/test_client_admin.py b/tests/oidc/test_client_admin.py index 8d1747db..067a81f1 100644 --- a/tests/oidc/test_client_admin.py +++ b/tests/oidc/test_client_admin.py @@ -207,9 +207,7 @@ def test_client_delete(testclient, logged_admin): consent_id="consent_id", subject=logged_admin, client=client, scope="openid" ) consent.save() - code = models.AuthorizationCode( - authorization_code_id="id", client=client, subject=client - ) + models.AuthorizationCode(authorization_code_id="id", client=client, subject=client) res = testclient.get("/admin/client/edit/" + client.client_id) res = res.forms["clientaddform"].submit(name="action", value="delete").follow() diff --git a/tests/oidc/test_consent.py b/tests/oidc/test_consent.py index 8ba92560..993d355e 100644 --- a/tests/oidc/test_consent.py +++ b/tests/oidc/test_consent.py @@ -71,7 +71,7 @@ def test_restoration_already_restored(testclient, client, consent, logged_user, def test_invalid_consent_revokation(testclient, client, logged_user): - res = testclient.get(f"/consent/revoke/invalid", status=302) + res = testclient.get("/consent/revoke/invalid", status=302) assert ("success", "The access has been revoked") not in res.flashes assert ("error", "Could not revoke this access") in res.flashes @@ -83,7 +83,7 @@ def test_someone_else_consent_revokation(testclient, client, consent, logged_mod def test_invalid_consent_restoration(testclient, client, logged_user): - res = testclient.get(f"/consent/restore/invalid", status=302) + res = testclient.get("/consent/restore/invalid", status=302) assert ("success", "The access has been restored") not in res.flashes assert ("error", "Could not restore this access") in res.flashes diff --git a/tests/oidc/test_dynamic_client_registration.py b/tests/oidc/test_dynamic_client_registration.py index fa1ece9e..6eec3d4a 100644 --- a/tests/oidc/test_dynamic_client_registration.py +++ b/tests/oidc/test_dynamic_client_registration.py @@ -112,7 +112,7 @@ def test_client_registration_with_authentication_invalid_token( "grant_types": ["authorization_code"], "response_types": ["code"], } - headers = {"Authorization": f"Bearer invalid-token"} + headers = {"Authorization": "Bearer invalid-token"} res = testclient.post_json("/oauth/register", payload, headers=headers, status=400) assert res.json == { "error": "access_denied", diff --git a/tests/oidc/test_dynamic_client_registration_management.py b/tests/oidc/test_dynamic_client_registration_management.py index a66ce039..78da1ba2 100644 --- a/tests/oidc/test_dynamic_client_registration_management.py +++ b/tests/oidc/test_dynamic_client_registration_management.py @@ -150,7 +150,7 @@ def test_delete(testclient, backend, user): headers = {"Authorization": "Bearer static-token"} with warnings.catch_warnings(record=True): - res = testclient.delete( + testclient.delete( f"/oauth/register/{client.client_id}", headers=headers, status=204 ) assert not models.Client.get(client_id=client.client_id) diff --git a/tests/oidc/test_token_admin.py b/tests/oidc/test_token_admin.py index 4205e8de..e915f58f 100644 --- a/tests/oidc/test_token_admin.py +++ b/tests/oidc/test_token_admin.py @@ -124,7 +124,7 @@ def test_token_view(testclient, token, logged_admin): def test_token_not_found(testclient, logged_admin): - res = testclient.get("/admin/token/" + "yolo", status=404) + testclient.get("/admin/token/" + "yolo", status=404) def test_revoke_token(testclient, token, logged_admin): @@ -138,4 +138,4 @@ def test_revoke_token(testclient, token, logged_admin): def test_revoke_invalid_token(testclient, logged_admin): - testclient.get(f"/admin/token/invalid/revoke", status=404) + testclient.get("/admin/token/invalid/revoke", status=404)