PyCQA / PyCQA/flake8-bugbear

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

Open
#170 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted
Dominant language
Python
Stars
1.1k
Forks
123
Avg merge
2d 5h
Merged PRs (30d)
5

Description

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

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation file or test is named in the issue. Start by examining the repository’s existing checker entry points and test layout, then use the two string.find() examples as baseline cases. Done means logical tests using string.find() without a numeric comparison are flagged, while valid comparisons are not.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.