Kavya Gunthoju
Kavya Gunthoju

Reputation: 1

Deprecate import from a.py when Class changed from a.py to b.py

I have a class 'Model' in a.py, Now the class has moved to b.py. So whenever end user use from a import Model, I wanted to through an deprecation or some warning message. Any idea how to do that?

Upvotes: 0

Views: 179

Answers (2)

S-c-r-a-t-c-h-y
S-c-r-a-t-c-h-y

Reputation: 431

I came up with this simple solution :

class Model:
    def __init__(self):
        # prints a warning message in the console
        print('The "Model" class has been moved to b.py, please consider modifying your code !')

m = Model() # instantiate the model class to throw the message

This should work like you expected it Edit: this is the code for a.py

Upvotes: 0

DonKnacki
DonKnacki

Reputation: 427

you should use warning (https://docs.python.org/3/library/warnings.html) and more specially DeprecationWarning (https://docs.python.org/3/library/exceptions.html#DeprecationWarning)

a good answer already exist here : How to warn about class (name) deprecation

Upvotes: 1

Related Questions