From a2d45d3a4a09f8617c612c42de6c1c726b8ec021 Mon Sep 17 00:00:00 2001 From: apple1417 Date: Sun, 11 May 2025 13:28:01 +1200 Subject: [PATCH] move a few warnings to use python's system the ones in `options.py` were just plain overlooked the other two have non-trivial stack levels, which skip file prefixes solves --- Readme.md | 2 +- mod.py | 7 ++++++- mod_factory.py | 9 ++++++--- options.py | 7 +++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index db61718..b8d14ca 100644 --- a/Readme.md +++ b/Readme.md @@ -20,7 +20,7 @@ game specific things: # Changelog ### v1.10 -- Linting fixups. +- Moved a few warnings to go through Python's system, so they get attributed to the right place. ### v1.9 - Added a new `CoopSupport.HostOnly` value. diff --git a/mod.py b/mod.py index 49f72d9..5855f1a 100644 --- a/mod.py +++ b/mod.py @@ -2,6 +2,7 @@ import inspect import sys +import warnings from dataclasses import dataclass, field from enum import Enum, Flag, auto from functools import cache @@ -19,6 +20,8 @@ if TYPE_CHECKING: from collections.abc import Callable, Iterator, Sequence +_WARNING_SKIPS: tuple[str] = (str(Path(__file__).parent),) + class Game(Flag): """A flags enum of the supported games.""" @@ -197,9 +200,11 @@ def __post_init__(self) -> None: # noqa: C901 - difficult to split up case KeybindType() if find_keybinds: new_keybinds.append(value) case GroupedOption() | NestedOption() if find_options: - logging.dev_warning( + warnings.warn( f"{self.name}: {type(value).__name__} instances must be explicitly" f" specified in the options list!", + stacklevel=2, + skip_file_prefixes=_WARNING_SKIPS, ) case BaseOption() if find_options: new_options.append(value) diff --git a/mod_factory.py b/mod_factory.py index 099ac41..ad7c540 100644 --- a/mod_factory.py +++ b/mod_factory.py @@ -3,13 +3,12 @@ import inspect import operator import tomllib +import warnings from collections.abc import Callable, Sequence from pathlib import Path from types import ModuleType from typing import Any, TypedDict -from unrealsdk import logging - from .command import AbstractCommand from .dot_sdkmod import open_in_mod_dir from .hook import HookType @@ -19,6 +18,8 @@ from .options import BaseOption, GroupedOption, NestedOption from .settings import SETTINGS_DIR +_WARNING_SKIPS: tuple[str] = (str(Path(__file__).parent),) + def build_mod[T: Mod = Mod]( *, @@ -321,9 +322,11 @@ def update_fields_with_module_search( # noqa: C901 - difficult to split up new_keybinds.append(value) case GroupedOption() | NestedOption() if find_options: - logging.dev_warning( + warnings.warn( f"{module.__name__}: {type(value).__name__} instances must be explicitly" f" specified in the options list!", + stacklevel=2, + skip_file_prefixes=_WARNING_SKIPS, ) case BaseOption() if find_options: new_options.append(value) diff --git a/options.py b/options.py index 26f900e..d603676 100644 --- a/options.py +++ b/options.py @@ -1,3 +1,4 @@ +import warnings from abc import ABC, abstractmethod from collections.abc import Callable, Mapping, Sequence from dataclasses import KW_ONLY, dataclass, field @@ -136,9 +137,10 @@ def __call__(self, on_change: Callable[[Self, J], None]) -> Self: This option instance. """ if self.on_change is not None: - logging.dev_warning( + warnings.warn( f"{self.__class__.__qualname__}.__call__ was called on an option which already has" f" a on change callback.", + stacklevel=2, ) self.on_change = on_change @@ -385,9 +387,10 @@ def __call__(self, on_press: Callable[[Self], None]) -> Self: This option instance. """ if self.on_press is not None: - logging.dev_warning( + warnings.warn( f"{self.__class__.__qualname__}.__call__ was called on an option which already has" f" a on press callback.", + stacklevel=2, ) self.on_press = on_press