Skip to content

Commit

Permalink
refact: use f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
sfermigier committed Nov 5, 2024
1 parent 0507af8 commit e963d4c
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 39 deletions.
8 changes: 3 additions & 5 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ lint.extend-ignore = [
"E712", # ...
"E713", # Test for membership should be `not in`
"PLR2044", # Line with empty comment
"RUF027", # Possible f-string without an `f` prefix
"S101", # Use of `assert` detected
"S320", # OK, we're using defusedxml
# "TCH001", # Move import into a type-checking block
# "TCH002", # Move import into a type-checking block
# "TCH003", # Move import into a type-checking block
"ISC001", # Conflicts with formatter

# Fix these (if possible / reasonable)
"A001", # Variable `...` is shadowing a python builtin
Expand Down Expand Up @@ -101,6 +98,7 @@ lint.extend-ignore = [
"RET505", # Unnecessary `else` after `return` statement
"RUF005", # Consider iterable unpacking instead of concatenation
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
#
"S104", # Possible binding to all interfaces
"S301", # `pickle`...
"S403", # `pickle`, `cPickle`, `dill`, and `shelve` modules are possibly insecure
Expand All @@ -111,13 +109,13 @@ lint.extend-ignore = [
"S606", # Subprocess related...
"S607", # Subprocess related...
"S701", # By default, jinja2 sets `autoescape` to `False`.
#
"SIM102", # Use a single `if` statement instead of nested `if` statements
"SIM108", # Use ternary operator
"SIM115", # Use context handler for opening files
"SIM117", # Use a single `with` statement with multiple contexts instead of nested `with` statements
"SLF001", # Private member accessed
"T201", # `print` found
"UP032", # Use f-strings
]

[lint.mccabe]
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ ignore =
F841
# ambiguous variable name
E741
# Line too long
E501
# abstract base class, but it has no abstract methods
B024
# something something abstract class
Expand Down
4 changes: 1 addition & 3 deletions src/abilian/core/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ def __repr__(self) -> str:
class_ = self.__class__
mod_ = class_.__module__
classname = class_.__name__
return "<{}.{} instance at 0x{:x} entity id={!r}>".format(
mod_, classname, id(self), self.entity_id
)
return f"<{mod_}.{classname} instance at 0x{id(self):x} entity id={self.entity_id!r}>"


@sa.event.listens_for(Attachment, "before_insert", propagate=True)
Expand Down
8 changes: 1 addition & 7 deletions src/abilian/core/models/subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,7 @@ def __str__(self) -> str:

def __repr__(self) -> str:
cls = self.__class__
return "<{mod}.{cls} id={id!r} email={email!r} at 0x{addr:x}>".format(
mod=cls.__module__,
cls=cls.__name__,
id=self.id,
email=self.email,
addr=id(self),
)
return f"<{cls.__module__}.{cls.__name__} id={self.id!r} email={self.email!r} at 0x{id(self):x}>"


@listens_for(User, "mapper_configured", propagate=True)
Expand Down
4 changes: 1 addition & 3 deletions src/abilian/sbe/apps/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,7 @@ def file_name(self) -> str:
return self.title

def __repr__(self) -> str:
return "<Document id={!r} name={!r} path={!r} content_length={:d} at 0x{:x}>".format(
self.id, self.title, self.path, self.content_length, id(self)
)
return f"<Document id={self.id!r} name={self.title!r} path={self.path!r} content_length={self.content_length:d} at 0x{id(self):x}>"

# locking management; used for checkin/checkout - this could be generalized to
# any entity
Expand Down
4 changes: 2 additions & 2 deletions src/abilian/sbe/apps/documents/views/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ def document_download(doc_id: int, attach: bool = False) -> Response:
):
# Note: we omit text/html for security reasons.
quoted_filename = quote(doc.title.encode("utf8"))
response.headers["content-disposition"] = 'attachment;filename="{}"'.format(
quoted_filename
response.headers["content-disposition"] = (
f'attachment;filename="{quoted_filename}"'
)

return response
Expand Down
4 changes: 2 additions & 2 deletions src/abilian/sbe/apps/social/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def users_dt_json():
name = escape(user.name or "")

cell0 = (
'<a href="{url}"><img src="{src}" width="{size}" height="{size}">'
"</a>".format(url=user_url, src=mugshot, size=MUGSHOT_SIZE)
f'<a href="{user_url}"><img src="{mugshot}" width="{MUGSHOT_SIZE}" height="{MUGSHOT_SIZE}">'
"</a>"
)
cell1 = f'<div class="info"><a href="{user_url}">{name}</a></div>'
cell2 = age(user.created_at)
Expand Down
8 changes: 3 additions & 5 deletions src/abilian/services/audit/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,9 @@ def setup_auditable_entity(self, entity_class: Any):
backref_attr = ".".join(inferred_backref)
else:
raise ValueError(
"Audit setup class<{cls}: Could not guess backref name"
' of relationship "{related_attr}", please use tuple annotation '
"on __auditable_entity__".format(
cls=entity_class.__name__, related_attr=related_attr
)
f"Audit setup class<{entity_class.__name__}: Could not guess backref name"
f' of relationship "{related_attr}", please use tuple annotation '
"on __auditable_entity__"
)

meta.related = related_path
Expand Down
7 changes: 1 addition & 6 deletions src/abilian/web/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,7 @@ def __str__(self) -> str:
return str(url_for(self.name, **self.get_kwargs()))

def __repr__(self) -> str:
return "{cls}({name!r}, *{args!r}, **{kwargs!r})".format(
cls=self.__class__.__name__,
name=self.name,
args=self.args,
kwargs=self.kwargs,
)
return f"{self.__class__.__name__}({self.name!r}, *{self.args!r}, **{self.kwargs!r})"


class Action:
Expand Down
4 changes: 1 addition & 3 deletions src/abilian/web/admin/panels/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def data(self, *args, **kw) -> dict:
r for r in security.get_roles(user, no_group_roles=True) if r.assignable
]
columns = [
'<a href="{url}"><img src="{src}" width="{size}" height="{size}"></a>'.format(
url=user_url, src=mugshot, size=MUGSHOT_SIZE
),
f'<a href="{user_url}"><img src="{mugshot}" width="{MUGSHOT_SIZE}" height="{MUGSHOT_SIZE}"></a>',
f'<a href="{user_url}">{name}</a>',
f'<a href="{user_url}"><em>{email}</em></a>',
"\u2713" if user.can_login else "",
Expand Down
4 changes: 1 addition & 3 deletions src/abilian/web/forms/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ def _core_field_repr(self) -> str:
Useful for tracing field errors (like in Sentry).
"""
return "<{}.{} at 0x{:x} name={!r}>".format(
self.__class__.__module__, self.__class__.__name__, id(self), self.name
)
return f"<{self.__class__.__module__}.{self.__class__.__name__} at 0x{id(self):x} name={self.name!r}>"

patch_logger.info(f"{Field.__module__}.Field.__repr__")
Field.__repr__ = _core_field_repr
Expand Down

0 comments on commit e963d4c

Please sign in to comment.