From 953611b1445651272ca475decde7536e693128ff Mon Sep 17 00:00:00 2001 From: Adrien Carpentier Date: Mon, 11 Sep 2023 14:40:45 +0800 Subject: [PATCH] clean: replace typing.Optional --- api.py | 5 ++--- commands/tweet.py | 9 ++++----- common/generate_word.py | 3 +-- en/alter_text.py | 3 +-- en/generate_definition.py | 15 +++++++-------- fr/alter_text.py | 5 ++--- fr/classify.py | 5 +---- fr/generate_definition.py | 13 ++++++------- 8 files changed, 24 insertions(+), 34 deletions(-) diff --git a/api.py b/api.py index 21700b0..1568efa 100644 --- a/api.py +++ b/api.py @@ -1,6 +1,5 @@ import tomllib from pathlib import Path -from typing import Optional import nltk import sentry_sdk @@ -88,7 +87,7 @@ async def generate_word(request: Request, lang: str): if lang not in ["en", "fr", "it", "es"]: raise HTTPException(status_code=400, detail="Language not supported.") ip: str = request.client.host - response: Optional[dict] = await generate_word_and_save(lang=lang, ip=ip) + response: dict | None = await generate_word_and_save(lang=lang, ip=ip) if response: return response else: @@ -173,7 +172,7 @@ async def get_random_definition_from_db(request: Request, lang: str): @app.get("/{lang}/alter", tags=["alter"]) @limiter.limit("6/minute") async def alter_text( - request: Request, lang: str, text: str, percentage: Optional[float] = 0.4 + request: Request, lang: str, text: str, percentage: float | None = 0.4 ): """ Alter a text with random non existing words. diff --git a/commands/tweet.py b/commands/tweet.py index 073d200..dcdb6da 100755 --- a/commands/tweet.py +++ b/commands/tweet.py @@ -1,5 +1,4 @@ from asyncio import run as aiorun -from typing import Optional import requests import tweepy @@ -20,7 +19,7 @@ async def _send_tweet(lang: str, dry_run: bool = False) -> None: await prepare_db() - tweet: Optional[str] = None + tweet: str | None = None if lang == "en": tweet = await generate_tweet_en() elif lang == "fr": @@ -43,9 +42,9 @@ async def _send_tweet(lang: str, dry_run: bool = False) -> None: # If tweet os too long, regenerate it. # Don't try more than 6 times for security reasons. if lang == "en": - tweet: str = await generate_tweet_en() + tweet = await generate_tweet_en() if lang == "fr": - tweet: str = await generate_tweet_fr() + tweet = await generate_tweet_fr() tries += 1 if dry_run: @@ -78,7 +77,7 @@ async def _send_tweet(lang: str, dry_run: bool = False) -> None: def main( lang: str = typer.Argument(..., help="Language ('en' or 'fr')"), - dry_run: Optional[bool] = typer.Option(False, help="Dry run"), + dry_run: bool | None = typer.Option(False, help="Dry run"), ): aiorun(_send_tweet(lang, dry_run)) diff --git a/common/generate_word.py b/common/generate_word.py index 1beaaeb..4bb02c7 100644 --- a/common/generate_word.py +++ b/common/generate_word.py @@ -1,7 +1,6 @@ import json import random from datetime import datetime -from typing import Optional from tortoise.exceptions import IntegrityError @@ -11,7 +10,7 @@ from models import GeneratedWordEN, GeneratedWordFR -async def generate_word_and_save(lang: str, ip: str) -> Optional[dict]: +async def generate_word_and_save(lang: str, ip: str) -> dict | None: already_generated = True # assume it has already been generated so we enter the while loop at least once # noqa: E501 retries = 0 while already_generated and retries < 10: diff --git a/en/alter_text.py b/en/alter_text.py index d94a103..0c0bcc7 100644 --- a/en/alter_text.py +++ b/en/alter_text.py @@ -1,6 +1,5 @@ import math import random -from typing import Optional from nltk import pos_tag from nltk.tokenize import word_tokenize @@ -25,7 +24,7 @@ async def alter_text_en( - text: str, percentage: float, forced_replacements: Optional[dict] = {} + text: str, percentage: float, forced_replacements: dict | None = {} ) -> str: """ Alter a text randomly using NLTK POS tagging. diff --git a/en/generate_definition.py b/en/generate_definition.py index 2f25b48..6c5caaa 100644 --- a/en/generate_definition.py +++ b/en/generate_definition.py @@ -1,6 +1,5 @@ import random from datetime import datetime -from typing import Optional import requests from tortoise.contrib.postgres.functions import Random @@ -11,7 +10,7 @@ from .alter_text import alter_text_en -async def generate_definition_en(percentage: float, ip: Optional[str] = None) -> dict: +async def generate_definition_en(percentage: float, ip: str | None = None) -> dict: random_definition = await get_random_definition_en() real_string = random_definition["real_string"] @@ -69,9 +68,9 @@ async def get_random_definition_en() -> dict: """ count = 0 definition = None - real_string: Optional[str] = None - type: Optional[str] = None - example: Optional[str] = None + real_string: str | None = None + type: str | None = None + example: str | None = None while (not definition) or (type not in ALLOWED_TYPES_EN): if count > 0: @@ -100,9 +99,9 @@ async def get_definition_from_word_en(word: str) -> dict: """ Returns the type, definition and example of a given word using the dictionaryapi. """ - type: Optional[str] = None - definition: Optional[str] = None - example: Optional[str] = None + type: str | None = None + definition: str | None = None + example: str | None = None response = requests.get(f"{DICTIONNARY_EN_API_URL}{word}") diff --git a/fr/alter_text.py b/fr/alter_text.py index 435c3c2..ed8c5d7 100644 --- a/fr/alter_text.py +++ b/fr/alter_text.py @@ -1,6 +1,5 @@ import math import random -from typing import Optional import spacy from spacy.language import Language @@ -61,7 +60,7 @@ def create_melt_tagger(nlp, name): async def alter_text_fr( - text: str, percentage: float, forced_replacements: Optional[dict] = {} + text: str, percentage: float, forced_replacements: dict | None = {} ) -> str: """ Alter a text randomly using Spacy and Lefff @@ -114,7 +113,7 @@ def get_replacable_tokens_ids(doc: Doc, not_to_replace: list[str]) -> list[int]: async def replace_tokens( - doc: Doc, tokens_ids_to_replace: list, forced_replacements: Optional[dict] + doc: Doc, tokens_ids_to_replace: list, forced_replacements: dict | None ) -> Doc: """ Replace the tokens in the doc diff --git a/fr/classify.py b/fr/classify.py index 882b087..80bb50a 100644 --- a/fr/classify.py +++ b/fr/classify.py @@ -1,6 +1,3 @@ -from typing import Optional - - def classify_fr(word: str) -> dict: """ Classify French word @@ -144,7 +141,7 @@ def classify_fr(word: str) -> dict: } -def get_char_from_position(word: str, position: int) -> Optional[str]: +def get_char_from_position(word: str, position: int) -> str | None: try: return word[position] except Exception: diff --git a/fr/generate_definition.py b/fr/generate_definition.py index 877e849..6dd6ab0 100644 --- a/fr/generate_definition.py +++ b/fr/generate_definition.py @@ -1,5 +1,4 @@ from datetime import datetime -from typing import Optional import requests from tortoise.contrib.postgres.functions import Random @@ -10,7 +9,7 @@ from models import GeneratedDefinitionFR, GeneratedWordFR, RealWordFR -async def generate_definition_fr(percentage: float, ip: Optional[str] = None) -> dict: +async def generate_definition_fr(percentage: float, ip: str | None = None) -> dict: random_definition = await get_random_definition_fr() real_string = random_definition["real_string"] @@ -70,9 +69,9 @@ async def get_random_definition_fr() -> dict: """ count = 0 definition = None - real_string: Optional[str] = None - type: Optional[str] = None - gender: Optional[str] = None + real_string: str | None = None + type: str | None = None + gender: str | None = None while (not definition) or (type not in ALLOWED_TYPES_FR.values()): if count > 0: @@ -125,8 +124,8 @@ async def get_definition_from_word_fr(word: str) -> dict: """ Returns the type and definition of a given word using the French Dicolink API. """ - type: Optional[str] = None - definition: Optional[str] = None + type: str | None = None + definition: str | None = None response = requests.get( f"{DICTIONNARY_FR_API_URL}{word}/definitions?limite=1&api_key={DICTIONNARY_FR_API_KEY}"