From b0e682f66c0fa157a5f8cab54b78966a2e2d0b3b Mon Sep 17 00:00:00 2001 From: Bruno Marques Date: Sun, 9 Jun 2024 18:40:03 +0100 Subject: [PATCH] Enabled DeprecationWarning by default and fixed references --- EDMCLogging.py | 3 +++ config/__init__.py | 21 +++++++++++---------- l10n.py | 10 +++++----- myNotebook.py | 5 +++-- prefs.py | 7 +++---- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/EDMCLogging.py b/EDMCLogging.py index 74b439f9..b6d4b353 100644 --- a/EDMCLogging.py +++ b/EDMCLogging.py @@ -42,6 +42,7 @@ import logging.handlers import os import pathlib import tempfile +import warnings from contextlib import suppress from fnmatch import fnmatch # So that any warning about accessing a protected member is only in one place. @@ -99,6 +100,8 @@ logging.Logger.trace = lambda self, message, *args, **kwargs: self._log( # type # MAGIC-CONT: See MAGIC tagged comment in Logger.__init__() logging.Formatter.converter = gmtime +warnings.simplefilter('default', DeprecationWarning) + def _trace_if(self: logging.Logger, condition: str, message: str, *args, **kwargs) -> None: if any(fnmatch(condition, p) for p in config_mod.trace_on): diff --git a/config/__init__.py b/config/__init__.py index fe258475..9636d49a 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -41,7 +41,6 @@ import pathlib import re import subprocess import sys -import traceback import warnings from abc import abstractmethod from typing import Any, Callable, Type, TypeVar @@ -329,8 +328,7 @@ class AbstractConfig(abc.ABC): :raises OSError: On Windows, if a Registry error occurs. :return: The data or the default. """ - warnings.warn(DeprecationWarning('get is Deprecated. use the specific getter for your type')) - logger.debug('Attempt to use Deprecated get() method\n' + ''.join(traceback.format_stack())) + warnings.warn('get is Deprecated. use the specific getter for your type', DeprecationWarning, stacklevel=2) if (a_list := self._suppress_call(self.get_list, ValueError, key, default=None)) is not None: return a_list @@ -388,8 +386,7 @@ class AbstractConfig(abc.ABC): See get_int for its replacement. :raises OSError: On Windows, if a Registry error occurs. """ - warnings.warn(DeprecationWarning('getint is Deprecated. Use get_int instead')) - logger.debug('Attempt to use Deprecated getint() method\n' + ''.join(traceback.format_stack())) + warnings.warn('getint is Deprecated. Use get_int instead', DeprecationWarning, stacklevel=2) return self.get_int(key, default=default) @@ -448,15 +445,15 @@ class AbstractConfig(abc.ABC): def get_password(self, account: str) -> None: """Legacy password retrieval.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning) + warnings.warn("password subsystem is no longer supported", DeprecationWarning, stacklevel=2) def set_password(self, account: str, password: str) -> None: """Legacy password setting.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning) + warnings.warn("password subsystem is no longer supported", DeprecationWarning, stacklevel=2) def delete_password(self, account: str) -> None: """Legacy password deletion.""" - warnings.warn("password subsystem is no longer supported", DeprecationWarning) + warnings.warn("password subsystem is no longer supported", DeprecationWarning, stacklevel=2) def get_config(*args, **kwargs) -> AbstractConfig: @@ -489,5 +486,9 @@ def get_update_feed() -> str: return 'https://raw.githubusercontent.com/EDCD/EDMarketConnector/releases/edmarketconnector.xml' -# WARNING: update_feed is deprecated, and will be removed in 6.0 or later. Please migrate to get_update_feed() -update_feed = get_update_feed() +def __getattr__(name: str): + if name == 'update_feed': + warnings.warn('update_feed is deprecated, and will be removed in 6.0 or later. ' + 'Please migrate to get_update_feed()', DeprecationWarning, stacklevel=2) + return get_update_feed() + raise AttributeError(name=name) diff --git a/l10n.py b/l10n.py index 27cd95c5..79bab04a 100755 --- a/l10n.py +++ b/l10n.py @@ -263,15 +263,15 @@ class _Locale: """Locale holds a few utility methods to convert data to and from localized versions.""" def stringFromNumber(self, number: float | int, decimals: int | None = None) -> str: # noqa: N802 - warnings.warn(DeprecationWarning('use _Locale.string_from_number instead.')) + warnings.warn('use _Locale.string_from_number instead.', DeprecationWarning, stacklevel=2) return self.string_from_number(number, decimals) # type: ignore def numberFromString(self, string: str) -> int | float | None: # noqa: N802 - warnings.warn(DeprecationWarning('use _Locale.number_from_string instead.')) + warnings.warn('use _Locale.number_from_string instead.', DeprecationWarning, stacklevel=2) return self.number_from_string(string) def preferredLanguages(self) -> Iterable[str]: # noqa: N802 - warnings.warn(DeprecationWarning('use _Locale.preferred_languages instead.')) + warnings.warn('use _Locale.preferred_languages instead.', DeprecationWarning, stacklevel=2) return self.preferred_languages() def string_from_number(self, number: float | int, decimals: int = 5) -> str: @@ -367,8 +367,8 @@ translations = Translations() # Begin Deprecation Zone class _Translations(Translations): def __init__(self): - logger.warning(DeprecationWarning('Translations and _Translations() are deprecated. ' - 'Please use translations and Translations() instead.')) + warnings.warn('Translations and _Translations() are deprecated. ' + 'Please use translations and Translations() instead.', DeprecationWarning, stacklevel=2) super().__init__() diff --git a/myNotebook.py b/myNotebook.py index 0b083c23..d8b2b41b 100644 --- a/myNotebook.py +++ b/myNotebook.py @@ -11,6 +11,7 @@ from __future__ import annotations import sys import tkinter as tk +import warnings from tkinter import ttk, messagebox from PIL import ImageGrab from l10n import translations as tr @@ -124,8 +125,8 @@ class EntryMenu(ttk.Entry): class Entry(EntryMenu): """Custom ttk.Entry class to fix some display issues.""" - # DEPRECATED: Migrate to EntryMenu. Will remove in 6.0 or later. def __init__(self, master: ttk.Frame | None = None, **kw): + warnings.warn('Migrate to EntryMenu. Will remove in 6.0 or later.', DeprecationWarning, stacklevel=2) EntryMenu.__init__(self, master, **kw) @@ -142,8 +143,8 @@ class Button(ttk.Button): class ColoredButton(tk.Button): """Custom tk.Button class to fix some display issues.""" - # DEPRECATED: Migrate to tk.Button. Will remove in 6.0 or later. def __init__(self, master: ttk.Frame | None = None, **kw): + warnings.warn('Migrate to tk.Button. Will remove in 6.0 or later.', DeprecationWarning, stacklevel=2) tk.Button.__init__(self, master, **kw) diff --git a/prefs.py b/prefs.py index 7f217ac8..136b52de 100644 --- a/prefs.py +++ b/prefs.py @@ -9,6 +9,7 @@ import subprocess import sys import tempfile import tkinter as tk +import warnings from os import system from os.path import expanduser, expandvars, join, normpath from tkinter import colorchooser as tkColorChooser # type: ignore # noqa: N812 @@ -40,10 +41,8 @@ logger = get_main_logger() def help_open_log_folder() -> None: """Open the folder logs are stored in.""" - logger.warning( - DeprecationWarning("This function is deprecated, use open_log_folder instead. " - "This function will be removed in 6.0 or later") - ) + warnings.warn('prefs.help_open_log_folder is deprecated, use open_log_folder instead. ' + 'This function will be removed in 6.0 or later', DeprecationWarning, stacklevel=2) open_folder(pathlib.Path(tempfile.gettempdir()) / appname)