Use ruff linter

This commit is contained in:
Éloi Rivard 2023-05-25 13:37:58 +02:00
parent ca8374acf4
commit d8158d6a72
19 changed files with 31 additions and 42 deletions

View file

@ -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:

View file

@ -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

View file

@ -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):

View file

@ -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)

View file

@ -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

View file

@ -133,7 +133,6 @@ class LDAPBackend(Backend):
@classmethod
def validate(cls, config):
from canaille.app.configuration import ConfigurationException
from canaille.app import models
try:

View file

@ -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)

View file

@ -110,6 +110,9 @@ source = [
omit = [".tox/*"]
branch = true
[tool.ruff]
ignore = ["E501", "E722"]
[tool.tox]
legacy_tox_ini = """
[tox]

View file

@ -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 (

View file

@ -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",

View file

@ -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",

View file

@ -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):

View file

@ -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",

View file

@ -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",

View file

@ -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()

View file

@ -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

View file

@ -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",

View file

@ -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)

View file

@ -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)