PyCQA/flake8-bugbear

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

Offen

#170 geöffnet am 22.05.2021

 (3 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Python (116 Forks)github user discovery
enhancementhelp wanted

Repository-Metriken

Stars
 (1.116 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 4h 17m) (3 gemergte PRs in 30 T)

Beschreibung

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.

Contributor Guide