Last active
July 31, 2017 04:12
-
-
Save Gab-km/318584fa571313eba8b3 to your computer and use it in GitHub Desktop.
C# の Obsolete 属性を Python に持ち込んでみた。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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