记住删除因不再支持而失效的代码的简单方法
项目描述
一些愚蠢的徽章
我真的感到非常烦恼,越来越经常,特别是在旧项目中,找出哪些代码是为了处理旧版本的Python或另一个库的解决方案。
这个库应该提供一种简单的方法,在您能够删除一些处理不支持库版本的旧代码时尽早警告您。
默认情况下,已预配置Python 2/3便利函数,但与其他库和更细粒度的版本编号一起工作。
# warn you you have dead code if you
# drop Python2 support
from insupportable import support
if support('PY2'):
print("You are on python 2")
else:
print("You are on python 3")
# warn you you have dead code if you
# drop Python2 support
from insupportable import support
if support('PY3'):
print("You are on python 3")
else:
print("You are on python 2")
设置以删除Python 2支持。
快速而肮脏的方法,修改全局配置,不建议这样做,因为它可能影响其他也使用此功能的库,但非常有用。
support.config(PY2=False)
if support(PY2):
print("You are on python 2")
else:
print("You are on python 3")
警告以下内容
mymodule/myfile.py:3: UserWarning: You are not supporting PY2 anymore
if support(PY2):
mymodule/myfile.py:3: UserWarning: PY2 is the last supported feature of this group, you can simplifiy this logic.
if support(PY2):
更复杂的方法,创建一个只在本地生效的配置上下文
from insupportable import S
support = S(PY2=False).support
....
高级配置/自定义功能
示例
support.config(config=({
'WindowsPhone':True,
'Android' :False,
'iOS' :False
},))
if support('WindowsPhone'):
print('Click on start menu')
else:
print("Probably Android - but you don't support it anymore")
mymodule/myfile.py:1: UserWarning: WindowsPhone is the last supported feature of this group, you can simplifiy this logic.
if support('WindowsPhone'):
待办事项
编写类似以下谓词和文档
if workaround('tornado==2.2'):
# do something special
谓词将根据tornado的版本决定是否产生,如果最小tornado版本高于2.2,则发出警告。
弃用装饰器
@deprecated_since('2.3.1',will_remove='3.0.0')
def my_api('something'):
"""deprecated fucntion that should
warn user when function is **called**
when module version is >= 3.0, the decorator should warn **developper** at **compile** time
that code has to be removed.
"""
此案例比支持()更有趣,因为它有两个传递,函数本身的装饰,以及函数的执行。我认为< cite>will_remove。我们是否应该为API等某些情况添加在日期后/在日期弃用的选项?
@deprecate_after(date='2015/10/15'):
def marty_from_the_future(self):
"""
Docs has some invalid ssl certificates ?
"""
自由软件:MIT许可证