From f66d8e9a61806b888d1c02f616669101c636bd7c Mon Sep 17 00:00:00 2001 From: Duck <31627079+duckboycool@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:23:39 -0600 Subject: [PATCH] WebHost: Update some typing in WebHostLib (#5069) --- WebHostLib/autolauncher.py | 3 --- WebHostLib/check.py | 12 ++++++------ WebHostLib/generate.py | 14 +++++++------- WebHostLib/stats.py | 15 ++++++--------- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/WebHostLib/autolauncher.py b/WebHostLib/autolauncher.py index b3301462..719963e3 100644 --- a/WebHostLib/autolauncher.py +++ b/WebHostLib/autolauncher.py @@ -164,9 +164,6 @@ def autogen(config: dict): Thread(target=keep_running, name="AP_Autogen").start() -multiworlds: typing.Dict[type(Room.id), MultiworldInstance] = {} - - class MultiworldInstance(): def __init__(self, config: dict, id: int): self.room_ids = set() diff --git a/WebHostLib/check.py b/WebHostLib/check.py index 4e0cf117..b8e1fd87 100644 --- a/WebHostLib/check.py +++ b/WebHostLib/check.py @@ -1,7 +1,7 @@ import os import zipfile 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 markupsafe import Markup @@ -43,7 +43,7 @@ def mysterycheck(): 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 = {} for uploaded_file in files: if banned_file(uploaded_file.filename): @@ -84,12 +84,12 @@ def get_yaml_data(files) -> Union[Dict[str, str], str, Markup]: 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"})) -> \ - 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)) - results = {} - rolled_results = {} + results: dict[str, str | bool] = {} + rolled_results: dict[str, dict] = {} for filename, text in options.items(): try: if type(text) is dict: diff --git a/WebHostLib/generate.py b/WebHostLib/generate.py index 34033a08..a84b17a8 100644 --- a/WebHostLib/generate.py +++ b/WebHostLib/generate.py @@ -6,7 +6,7 @@ import random import tempfile import zipfile 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 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 -def get_meta(options_source: dict, race: bool = False) -> Dict[str, Union[List[str], Dict[str, Any]]]: - plando_options: Set[str] = set() +def get_meta(options_source: dict, race: bool = False) -> dict[str, list[str] | dict[str, Any]]: + plando_options: set[str] = set() for substr in ("bosses", "items", "connections", "texts"): if options_source.get(f"plando_{substr}", substr in GeneratorOptions.plando_options): plando_options.add(substr) @@ -73,7 +73,7 @@ def generate(race=False): 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"])) 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)) -def gen_game(gen_options: dict, meta: Optional[Dict[str, Any]] = None, owner=None, sid=None): - if not meta: - meta: Dict[str, Any] = {} +def gen_game(gen_options: dict, meta: dict[str, Any] | None = None, owner=None, sid=None): + if meta is None: + meta = {} meta.setdefault("server_options", {}).setdefault("hint_cost", 10) race = meta.setdefault("generator_options", {}).setdefault("race", False) diff --git a/WebHostLib/stats.py b/WebHostLib/stats.py index 36545ac9..6fd73caf 100644 --- a/WebHostLib/stats.py +++ b/WebHostLib/stats.py @@ -1,4 +1,3 @@ -import typing from collections import Counter, defaultdict from colorsys import hsv_to_rgb from datetime import datetime, timedelta, date @@ -18,10 +17,9 @@ from .models import Room PLOT_WIDTH = 600 -def get_db_data(known_games: typing.Set[str]) -> typing.Tuple[typing.Counter[str], - typing.DefaultDict[datetime.date, typing.Dict[str, int]]]: - games_played = defaultdict(Counter) - total_games = Counter() +def get_db_data(known_games: set[str]) -> tuple[Counter[str], defaultdict[date, dict[str, int]]]: + games_played: defaultdict[date, dict[str, int]] = defaultdict(Counter) + total_games: Counter[str] = Counter() cutoff = date.today() - timedelta(days=30) room: Room 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 -def get_color_palette(colors_needed: int) -> typing.List[RGB]: +def get_color_palette(colors_needed: int) -> list[RGB]: colors = [] # colors_needed +1 to prevent first and last color being too close to each other colors_needed += 1 @@ -47,8 +45,7 @@ def get_color_palette(colors_needed: int) -> typing.List[RGB]: return colors -def create_game_played_figure(all_games_data: typing.Dict[datetime.date, typing.Dict[str, int]], - game: str, color: RGB) -> figure: +def create_game_played_figure(all_games_data: dict[date, dict[str, int]], game: str, color: RGB) -> figure: occurences = [] days = [day for day, game_data in all_games_data.items() if game_data[game]] for day in days: @@ -84,7 +81,7 @@ def stats(): days = sorted(games_played) 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): occurences = []