fix: Lint Python code with ruff (#3799)

This commit is contained in:
Christian Clauss
2024-08-12 17:09:30 +02:00
committed by GitHub
parent 65ece35966
commit 432914e310
41 changed files with 112 additions and 120 deletions

View File

@@ -53,7 +53,7 @@ class GroupService(BaseService):
all_ids = self.repos.recipes.all_ids(target_id)
used_size = sum(
fs_stats.get_dir_size(f"{self.directories.RECIPE_DATA_DIR}/{str(recipe_id)}") for recipe_id in all_ids
fs_stats.get_dir_size(f"{self.directories.RECIPE_DATA_DIR}/{recipe_id!s}") for recipe_id in all_ids
)
return GroupStorage.bytes(used_size, ALLOWED_SIZE)

View File

@@ -191,7 +191,7 @@ class ShoppingListService:
created_items = self.list_items.create_many(filtered_create_items) if filtered_create_items else []
updated_items = self.list_items.update_many(update_items) if update_items else []
for list_id in set(item.shopping_list_id for item in created_items + updated_items):
for list_id in {item.shopping_list_id for item in created_items + updated_items}:
self.remove_unused_recipe_references(list_id)
return ShoppingListItemsCollectionOut(
@@ -278,7 +278,7 @@ class ShoppingListService:
self.list_items.delete_many(delete_items) if delete_items else [], # type: ignore
)
for list_id in set(item.shopping_list_id for item in updated_items + deleted_items):
for list_id in {item.shopping_list_id for item in updated_items + deleted_items}:
self.remove_unused_recipe_references(list_id)
return ShoppingListItemsCollectionOut(
@@ -291,7 +291,7 @@ class ShoppingListService:
self.list_items.delete_many(set(delete_items)) if delete_items else [], # type: ignore
)
for list_id in set(item.shopping_list_id for item in deleted_items):
for list_id in {item.shopping_list_id for item in deleted_items}:
self.remove_unused_recipe_references(list_id)
return ShoppingListItemsCollectionOut(created_items=[], updated_items=[], deleted_items=deleted_items)

View File

@@ -122,7 +122,7 @@ def parse_ingredient(tokens) -> tuple[str, str]:
# no opening bracket anywhere -> just ignore the last bracket
ingredient, note = parse_ingredient_with_comma(tokens)
else:
# opening bracket found -> split in ingredient and note, remove brackets from note # noqa: E501
# opening bracket found -> split in ingredient and note, remove brackets from note
note = " ".join(tokens[start:])[1:-1]
ingredient = " ".join(tokens[:start])
else:

View File

@@ -95,7 +95,7 @@ def insideParenthesis(token, tokens):
else:
line = " ".join(tokens)
return (
re.match(r".*\(.*" + re.escape(token) + r".*\).*", line) is not None # noqa: W605 - invalid dscape sequence
re.match(r".*\(.*" + re.escape(token) + r".*\).*", line) is not None # - invalid dscape sequence
)
@@ -188,7 +188,7 @@ def import_data(lines):
# turn B-NAME/123 back into "name"
tag, confidence = re.split(r"/", columns[-1], maxsplit=1)
tag = re.sub(r"^[BI]\-", "", tag).lower() # noqa: W605 - invalid dscape sequence
tag = re.sub(r"^[BI]\-", "", tag).lower() # - invalid dscape sequence
# ====================
# Confidence Getter
@@ -261,6 +261,6 @@ def export_data(lines):
for i, token in enumerate(tokens):
features = getFeatures(token, i + 1, tokens)
output.append(joinLine([token] + features))
output.append(joinLine([token, *features]))
output.append("")
return "\n".join(output)

View File

@@ -136,7 +136,7 @@ class RecipeDataService(BaseService):
if ext not in img.IMAGE_EXTENSIONS:
ext = "jpg" # Guess the extension
file_name = f"{str(self.recipe_id)}.{ext}"
file_name = f"{self.recipe_id!s}.{ext}"
file_path = Recipe.directory_from_id(self.recipe_id).joinpath("images", file_name)
async with AsyncClient(transport=AsyncSafeTransport()) as client:

View File

@@ -162,7 +162,7 @@ def clean_instructions(steps_object: list | dict | str, default: list | None = N
# }
#
steps_object = typing.cast(dict, steps_object)
return clean_instructions([x for x in steps_object.values()])
return clean_instructions(list(steps_object.values()))
case str(step_as_str):
# Strings are weird, some sites return a single string with newlines
# others returns a json string for some reasons
@@ -481,7 +481,7 @@ def clean_tags(data: str | list[str]) -> list[str]:
case [str(), *_]:
return [tag.strip().title() for tag in data if tag.strip()]
case str(data):
return clean_tags([t for t in data.split(",")])
return clean_tags(data.split(","))
case _:
return []
# should probably raise exception

View File

@@ -55,7 +55,7 @@ async def create_from_url(url: str, translator: Translator) -> tuple[Recipe, Scr
new_recipe.image = "no image"
if new_recipe.name is None or new_recipe.name == "":
new_recipe.name = f"No Recipe Name Found - {str(uuid4())}"
new_recipe.name = f"No Recipe Name Found - {uuid4()!s}"
new_recipe.slug = slugify(new_recipe.name)
return new_recipe, extras