Issue
Let's say I have a function in python that returns a dict with some objects.
class MyObj:
pass
def my_func():
o = MyObj()
return {'some string' : o, 'additional info': 'some other text'}
At some point I notice that it would make sense to rename the key 'some string' as it is misleading and not well describing what is actually stored with this key. However, if I were to just change the key, people who use this bit of code, would be really annoyed because I didn't give them time via a deprecation period to adapt their code.
Current attempt
So the way I thought about implementing a deprecation warning is using a thin wrapper around dict:
from warnings import warn
class MyDict(dict):
def __getitem__(self, key):
if key == 'some string':
warn('Please use the new key: `some object` instead of `some string`')
return super().__getitem__(key)
This way I can create the dict with the old and the new key pointing towards the same object
class MyObj:
pass
def my_func():
o = MyObj()
return MyDict({'some string' : o, 'some object' : o, 'additional info': 'some other text'})
Questions:
- What are potential ways code might break if I add this change?
- Is there an easier (as in, less amount of changes, using existing solutions, using common patterns) way to achieve this?