Python警告-预期类型为';并[Integral,slice]';,得到';str';相反
我下面用python编写的代码给了我一个警告:Python警告-预期类型为';并[Integral,slice]';,得到';str';相反,python,dictionary,types,pycharm,warnings,Python,Dictionary,Types,Pycharm,Warnings,我下面用python编写的代码给了我一个警告: some_new_object['someVar'] = cd['someVar'] 警告是 Expected type 'Union[Integral, slice]', got 'str' instead 代码: 如果我将函数warn_test更改为: def warn_test(in_list): try: new_list = [] some_new_object = some_object()
some_new_object['someVar'] = cd['someVar']
警告是
Expected type 'Union[Integral, slice]', got 'str' instead
代码:
如果我将函数warn_test更改为:
def warn_test(in_list):
try:
new_list = []
some_new_object = some_object()
for cd in in_list:
if cd['someVar']:
some_new_object['someVar'] = cd['someVar']
new_list.append(some_new_object.copy())
for cd in new_list:
in_list.append(cd)
return in_list
except Exception:
print 'baaa'
它没有给我任何警告
有人能帮我理解为什么会得到警告,以及如何在第二次迭代中访问cd['someVar']
而不得到警告
我知道这段代码很奇怪,我正在做的一个项目需要它,我做了这个测试,在这里分享,但它给了我同样的警告,所以一个解决方案将在我的系统中修复它。(此系统必须具备无警告功能)迟到总比不迟到好
一般来说,我发现如果变量/方法返回是强类型的,那么这些警告就会消失。
一些新的对象
不是dict对象。但是,您可以使用setattr(obj,key,value)
设置属性这是pycharm发出的警告吗?如果是这样的话,请准确地显示代码中哪一行触发了警告,然后也许有人可以找出类型检查器不喜欢它的原因。我发布了一行警告:warnTest方法中的somenew_对象['someVar']=cd['someVar']。那就是我发布的第一个代码块中的第17行。你能举个代码示例吗?比如方法:def the_method(self,file_path:str)->(list,list,int,int):变量:list_a:list=[],_number:int,等等。。。
def warn_test(in_list):
try:
new_list = []
some_new_object = some_object()
for cd in in_list:
if cd['someVar']:
some_new_object['someVar'] = cd['someVar']
new_list.append(some_new_object.copy())
for cd in new_list:
in_list.append(cd)
return in_list
except Exception:
print 'baaa'