PyCQA / PyCQA/flake8-bugbear

B023: Why flake8 returns error only for a temporal variable for for loop?

Open
#396 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

flake8 causes error: B023 Function definition does not bind loop variable 'chk_key' for the following code.

        ret_df = pd.DataFrame(index=target_df.index)
        for chk_key in [nalod, nlod]:
            ret_df[chk_key] = (
                False if conf_dict[chk_key] is None
                else info_df.apply(
                        lambda x:
                        float(
                            x['INFO.dict'].get(chk_key, np.nan)
                        ) < conf_dict[chk_key],
                        axis=1
                )
            )

I cannot understand the reason. chk_key is only a temporal variable for for loop. How should I fix it?
I'd like to fix it as soon as possible.

I set a value to chk_key at the top of the function for trial. However, flake8 returns the same message.

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

Start by tracing the B023 check against the supplied Python loop and lambda example; the payload names no repository file or test. Compare the diagnostic with the intended closure behavior and use the result to determine whether the rule or its documentation needs changing.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.