Skip to content

Commit

Permalink
clean: replace typing.Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
bolinocroustibat committed Sep 11, 2023
1 parent 8964f62 commit 953611b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 34 deletions.
5 changes: 2 additions & 3 deletions api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import tomllib
from pathlib import Path
from typing import Optional

import nltk
import sentry_sdk
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 4 additions & 5 deletions commands/tweet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from asyncio import run as aiorun
from typing import Optional

import requests
import tweepy
Expand All @@ -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":
Expand All @@ -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:
Expand Down Expand Up @@ -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))

Expand Down
3 changes: 1 addition & 2 deletions common/generate_word.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import random
from datetime import datetime
from typing import Optional

from tortoise.exceptions import IntegrityError

Expand All @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions en/alter_text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import math
import random
from typing import Optional

from nltk import pos_tag
from nltk.tokenize import word_tokenize
Expand All @@ -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.
Expand Down
15 changes: 7 additions & 8 deletions en/generate_definition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import random
from datetime import datetime
from typing import Optional

import requests
from tortoise.contrib.postgres.functions import Random
Expand All @@ -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"]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}")

Expand Down
5 changes: 2 additions & 3 deletions fr/alter_text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import math
import random
from typing import Optional

import spacy
from spacy.language import Language
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions fr/classify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from typing import Optional


def classify_fr(word: str) -> dict:
"""
Classify French word
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions fr/generate_definition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime
from typing import Optional

import requests
from tortoise.contrib.postgres.functions import Random
Expand All @@ -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"]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}"
Expand Down

0 comments on commit 953611b

Please sign in to comment.