From 6edc0b3cfe6cd743305ca7181cb053a7727014ff Mon Sep 17 00:00:00 2001 From: Viljar Femoen Date: Thu, 7 Nov 2024 13:59:48 +0100 Subject: [PATCH] Improve traceback message --- src/instamatic/TEMController/__init__.py | 1 + src/instamatic/utils/deprecated.py | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/instamatic/TEMController/__init__.py b/src/instamatic/TEMController/__init__.py index 4add11de..2bc4ea6f 100644 --- a/src/instamatic/TEMController/__init__.py +++ b/src/instamatic/TEMController/__init__.py @@ -8,6 +8,7 @@ warnings.warn( 'The `TEMController` module is deprecated since version 2.0.6. Use the `controller`-module instead', VisibleDeprecationWarning, + stacklevel=2, ) diff --git a/src/instamatic/utils/deprecated.py b/src/instamatic/utils/deprecated.py index 4d75f9a9..046c9e4a 100644 --- a/src/instamatic/utils/deprecated.py +++ b/src/instamatic/utils/deprecated.py @@ -2,12 +2,14 @@ from __future__ import annotations + class VisibleDeprecationWarning(UserWarning): - """ - Numpy-inspired deprecation warning which will be shown by default. + """Numpy-inspired deprecation warning which will be shown by default. + The default `DeprecationWarning` does not show by default. """ + def deprecated(since: str, alternative: str, removed: str = None): """Mark a function as deprecated, printing a warning whenever it is used. @@ -34,7 +36,7 @@ def wrapped(*args, **kwargs): msg = f'Function {func.__name__} is deprecated since {since}, use {alternative} instead.' if removed is not None: msg += f' Will be removed in version {removed}.' - warnings.warn(msg, VisibleDeprecationWarning) + warnings.warn(msg, VisibleDeprecationWarning, stacklevel=2) return func(*args, **kwargs) return wrapped