Skip to content

Instantly share code, notes, and snippets.

@Gab-km Gab-km/obsolete.py
Last active Jul 31, 2017

Embed
What would you like to do?
C# の Obsolete 属性を Python に持ち込んでみた。
def obsolete(message=None, deprecated=False):
"""今後使用しない関数やメソッドにマークを付けます。
@message 警告に表示するテキスト
@deprecated すでに非推奨の場合、True
今後非推奨になる予定の場合、False
@see http://msdn.microsoft.com/ja-jp/library/system.obsoleteattribute.aspx
"""
def outer(fn):
import warnings
def inner(*args, **kwargs):
if deprecated:
warnings.warn(message, DeprecationWarning, stacklevel=3)
else:
warnings.warn(message, PendingDeprecationWarning, stacklevel=3)
return fn(*args, **kwargs)
return inner
return outer
import warnings
warnings.simplefilter("default") # これを実行するか、インタプリタ起動オプションに `-Wd` を渡すと、警告が表示されるようになる。
from .obsolete import obsolete
@obsolete("This function will be obsolete in the future.")
def old_add(x, y):
return x + y
print(old_add(1, 2))
#=> ~/src/py/try_obsolete.py:10 PendingDeprecationWarning: This function will be obsolete in the future.
# 3
@obsolete("This function is now deprecated.", True)
def deprecated_add(x, y):
return x + y
print(deprecated_add(2, 3))
#=> ~/src/py/try_obsolete.py:18 DeprecationWarning: This function is now deprecated.
# 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.