Warn on unnecessary use of `global`

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

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

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

Research direction

Start by inspecting the repository's existing lint-rule implementations and tests; no specific files or entry points are named in the issue. The change is complete when code that declares global only to read a name produces a warning, while declarations needed for assignment do not.

Written by the indexing model from the issue text.

Description

CONSTANT = 1

def fn(x):
    global CONSTANT  # <- unnecessary, unidiomatic, and a (small) performance hit
    return x + CONSTANT

The global statement is only required when assigning to a global variable; you can reference outer namespaces without it. I think it'd be nice to have a lint rule that pointed this out, to help newer Pythonistas understand how scoping works.

Dominant language
Python
Stars
1.1k
Forks
147
Avg merge
1d 8h
Merged PRs (30d)
39

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from PyCQA/flake8-bugbear

All issues in PyCQA/flake8-bugbear

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.