global
This is how the comment reads inline, under the flagged line.
Why it matters
Hidden global state makes functions order-dependent and tests interfere with each other.
What to do
Pass state in and return it, or keep it on an object.
Examples
def bump():
global count
count += 1def bump(count):
return count + 1What it catches
It reads a pattern on each line, aware of strings and comments. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
def f():
global x
x = 1
Left alone
def f(x):
return x
Research
`global` statements and module-level mutable state. Hidden coupling makes behavior depend on call order.
- Ruff rules
- owner's code bar
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"global-statement"
],
"severity": {
"global-statement": "medium"
}
}
}
global xmutates module state from inside a function.