PyCQA/flake8-bugbear

Proposed Check: Flag Improper Use of string.find()

开放

#170 创建于 2021年5月22日

 (3 条评论) (0 个反应) (0 位负责人)Python (116 个派生)github user discovery
enhancementhelp wanted

仓库指标

星标
 (1,116 个星标)
PR 合并指标
 (平均合并 4小时 17分钟) (30 天内合并 3 个 PR)

描述

Title: Flag Improper Use of string.find()

Description The string.find() method is easy to mis-use. A common error is:

mystring='abc'
if mystring.find('def'):
  print("def is contained in {}".format(mystring)

The issue is that if string.find fails, it returns -1, which is "truthy". If string.find() locates the output at the beginning of the line, it returns 0 which is "false".

mystring='abc'
if mystring.find('a'):
  print("a is in {}".format(mystring))
else:
  print('a is not in {}'.format(mystring))

It would be awesome if flake8-bugbear could flag any logical test involving string.find() that does not compare to a numerical value.

贡献者指南