flake8-bugbear
flake8-bugbear copied to clipboard
Mutable module level variables
Mutable module level variables might lead to bugs
file: a.py
SOME_CONSTANT = [1,5,7]
file: b.py
from a import SOME_CONSTANT
if bla:
SOME_CONSTANT.remove(5)
assert i in SOME_CONSTANT:
The correct thing to do would be to use: a.py
SOME_CONSTANT = (1,5,7)
Mutable module level variables should be reported similar to B006: Do not use mutable data structures for argument defaults.
While this might lead to false positives we could either:
- Only signal on YELLING_CASE variables
- Make it an opinionated and thus disabled by default lint (in which case I would argue #57 belongs there as well)
Let's refer to "module level" as Global scope. In regards to #57, and this, I potentially agree with you. I would like another maintainers approval before I'll state we'll accept a PR.
@ambv - I don't mind the sounds of this lint IF it's optional and disabled by default. Can I ask for a second opinion here based on this criteria? I agree it's going to be 'noisy', unless the code base turning this on wants to design their code that way.
JFYI: it is covered in wemake-python-styleguide that uses flake8-bugbear as a dependency.
Example:
» flake8 ex.py
ex.py
1:1 WPS407 Found mutable module constant
IS_MUTABLE = [1, 2, 3]
^
Full list of violations and explanations:
https://wemake-python-stylegui.de/en/0.13.4/pages/usage/violations/