PyCQA/flake8-bugbear

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

オープン

#170 opened on 2021/05/22

 (3 件のコメント) (0 件のリアクション) (0 人の担当者)Python (116 件のフォーク)github user discovery
enhancementhelp wanted

Repository metrics

Stars
 (1,116 個のスター)
PR merge metrics
 (平均マージ 4h 17m) (30d で 3 merged PRs)

説明

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.

コントリビューターガイド