WebHost: Update some typing in WebHostLib (#5069)

This commit is contained in:
Duck
2025-07-25 17:23:39 -06:00
committed by GitHub
parent 8499c2fd24
commit f66d8e9a61
4 changed files with 19 additions and 25 deletions

View File

@@ -164,9 +164,6 @@ def autogen(config: dict):
Thread(target=keep_running, name="AP_Autogen").start() Thread(target=keep_running, name="AP_Autogen").start()
multiworlds: typing.Dict[type(Room.id), MultiworldInstance] = {}
class MultiworldInstance(): class MultiworldInstance():
def __init__(self, config: dict, id: int): def __init__(self, config: dict, id: int):
self.room_ids = set() self.room_ids = set()

View File

@@ -1,7 +1,7 @@
import os import os
import zipfile import zipfile
import base64 import base64
from typing import Union, Dict, Set, Tuple from collections.abc import Set
from flask import request, flash, redirect, url_for, render_template from flask import request, flash, redirect, url_for, render_template
from markupsafe import Markup from markupsafe import Markup
@@ -43,7 +43,7 @@ def mysterycheck():
return redirect(url_for("check"), 301) return redirect(url_for("check"), 301)
def get_yaml_data(files) -> Union[Dict[str, str], str, Markup]: def get_yaml_data(files) -> dict[str, str] | str | Markup:
options = {} options = {}
for uploaded_file in files: for uploaded_file in files:
if banned_file(uploaded_file.filename): if banned_file(uploaded_file.filename):
@@ -84,12 +84,12 @@ def get_yaml_data(files) -> Union[Dict[str, str], str, Markup]:
return options return options
def roll_options(options: Dict[str, Union[dict, str]], def roll_options(options: dict[str, dict | str],
plando_options: Set[str] = frozenset({"bosses", "items", "connections", "texts"})) -> \ plando_options: Set[str] = frozenset({"bosses", "items", "connections", "texts"})) -> \
Tuple[Dict[str, Union[str, bool]], Dict[str, dict]]: tuple[dict[str, str | bool], dict[str, dict]]:
plando_options = PlandoOptions.from_set(set(plando_options)) plando_options = PlandoOptions.from_set(set(plando_options))
results = {} results: dict[str, str | bool] = {}
rolled_results = {} rolled_results: dict[str, dict] = {}
for filename, text in options.items(): for filename, text in options.items():
try: try:
if type(text) is dict: if type(text) is dict:

View File

@@ -6,7 +6,7 @@ import random
import tempfile import tempfile
import zipfile import zipfile
from collections import Counter from collections import Counter
from typing import Any, Dict, List, Optional, Union, Set from typing import Any
from flask import flash, redirect, render_template, request, session, url_for from flask import flash, redirect, render_template, request, session, url_for
from pony.orm import commit, db_session from pony.orm import commit, db_session
@@ -23,8 +23,8 @@ from .models import Generation, STATE_ERROR, STATE_QUEUED, Seed, UUID
from .upload import upload_zip_to_db from .upload import upload_zip_to_db
def get_meta(options_source: dict, race: bool = False) -> Dict[str, Union[List[str], Dict[str, Any]]]: def get_meta(options_source: dict, race: bool = False) -> dict[str, list[str] | dict[str, Any]]:
plando_options: Set[str] = set() plando_options: set[str] = set()
for substr in ("bosses", "items", "connections", "texts"): for substr in ("bosses", "items", "connections", "texts"):
if options_source.get(f"plando_{substr}", substr in GeneratorOptions.plando_options): if options_source.get(f"plando_{substr}", substr in GeneratorOptions.plando_options):
plando_options.add(substr) plando_options.add(substr)
@@ -73,7 +73,7 @@ def generate(race=False):
return render_template("generate.html", race=race, version=__version__) return render_template("generate.html", race=race, version=__version__)
def start_generation(options: Dict[str, Union[dict, str]], meta: Dict[str, Any]): def start_generation(options: dict[str, dict | str], meta: dict[str, Any]):
results, gen_options = roll_options(options, set(meta["plando_options"])) results, gen_options = roll_options(options, set(meta["plando_options"]))
if any(type(result) == str for result in results.values()): if any(type(result) == str for result in results.values()):
@@ -104,9 +104,9 @@ def start_generation(options: Dict[str, Union[dict, str]], meta: Dict[str, Any])
return redirect(url_for("view_seed", seed=seed_id)) return redirect(url_for("view_seed", seed=seed_id))
def gen_game(gen_options: dict, meta: Optional[Dict[str, Any]] = None, owner=None, sid=None): def gen_game(gen_options: dict, meta: dict[str, Any] | None = None, owner=None, sid=None):
if not meta: if meta is None:
meta: Dict[str, Any] = {} meta = {}
meta.setdefault("server_options", {}).setdefault("hint_cost", 10) meta.setdefault("server_options", {}).setdefault("hint_cost", 10)
race = meta.setdefault("generator_options", {}).setdefault("race", False) race = meta.setdefault("generator_options", {}).setdefault("race", False)

View File

@@ -1,4 +1,3 @@
import typing
from collections import Counter, defaultdict from collections import Counter, defaultdict
from colorsys import hsv_to_rgb from colorsys import hsv_to_rgb
from datetime import datetime, timedelta, date from datetime import datetime, timedelta, date
@@ -18,10 +17,9 @@ from .models import Room
PLOT_WIDTH = 600 PLOT_WIDTH = 600
def get_db_data(known_games: typing.Set[str]) -> typing.Tuple[typing.Counter[str], def get_db_data(known_games: set[str]) -> tuple[Counter[str], defaultdict[date, dict[str, int]]]:
typing.DefaultDict[datetime.date, typing.Dict[str, int]]]: games_played: defaultdict[date, dict[str, int]] = defaultdict(Counter)
games_played = defaultdict(Counter) total_games: Counter[str] = Counter()
total_games = Counter()
cutoff = date.today() - timedelta(days=30) cutoff = date.today() - timedelta(days=30)
room: Room room: Room
for room in select(room for room in Room if room.creation_time >= cutoff): for room in select(room for room in Room if room.creation_time >= cutoff):
@@ -32,7 +30,7 @@ def get_db_data(known_games: typing.Set[str]) -> typing.Tuple[typing.Counter[str
return total_games, games_played return total_games, games_played
def get_color_palette(colors_needed: int) -> typing.List[RGB]: def get_color_palette(colors_needed: int) -> list[RGB]:
colors = [] colors = []
# colors_needed +1 to prevent first and last color being too close to each other # colors_needed +1 to prevent first and last color being too close to each other
colors_needed += 1 colors_needed += 1
@@ -47,8 +45,7 @@ def get_color_palette(colors_needed: int) -> typing.List[RGB]:
return colors return colors
def create_game_played_figure(all_games_data: typing.Dict[datetime.date, typing.Dict[str, int]], def create_game_played_figure(all_games_data: dict[date, dict[str, int]], game: str, color: RGB) -> figure:
game: str, color: RGB) -> figure:
occurences = [] occurences = []
days = [day for day, game_data in all_games_data.items() if game_data[game]] days = [day for day, game_data in all_games_data.items() if game_data[game]]
for day in days: for day in days:
@@ -84,7 +81,7 @@ def stats():
days = sorted(games_played) days = sorted(games_played)
color_palette = get_color_palette(len(total_games)) color_palette = get_color_palette(len(total_games))
game_to_color: typing.Dict[str, RGB] = {game: color for game, color in zip(total_games, color_palette)} game_to_color: dict[str, RGB] = {game: color for game, color in zip(total_games, color_palette)}
for game in sorted(total_games): for game in sorted(total_games):
occurences = [] occurences = []