From 6a170b4c10e3243cf71e996ae16ac5a97ad5dec8 Mon Sep 17 00:00:00 2001 From: Sean Breckenridge Date: Thu, 28 Oct 2021 23:12:26 -0700 Subject: [PATCH] smarter deprecation warning using regex --- my/reddit/__init__.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/my/reddit/__init__.py b/my/reddit/__init__.py index 248d2f9..f2b60ca 100644 --- a/my/reddit/__init__.py +++ b/my/reddit/__init__.py @@ -15,10 +15,27 @@ REQUIRES = [ 'git+https://github.com/karlicoss/rexport', ] -from my.core import warnings as W +import re +import traceback + +# some hacky traceback to inspect the current stack +# to see if the user is using the old style of importing +warn = False +for f in traceback.extract_stack(): + line = f.line or '' # just in case it's None, who knows.. + + # cover the most common ways of previously interacting with the module + if 'import my.reddit ' in (line + ' '): + warn = True + elif 'from my import reddit' in line: + warn = True + elif re.match(r"from my\.reddit\simport\s(comments|saved|submissions|upvoted)", line): + warn = True # TODO: add link to instructions to migrate -W.high("DEPRECATED! Instead of my.reddit, import from my.reddit.all instead.") +if warn: + from my.core import warnings as W + W.high("DEPRECATED! Instead of my.reddit, import from my.reddit.all instead.") from .rexport import *