flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

Mutable module level variables

Open hmvp opened this issue 5 years ago • 2 comments

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)

hmvp avatar Feb 24 '20 10:02 hmvp

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.

cooperlees avatar Feb 27 '20 17:02 cooperlees

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/

sobolevn avatar Mar 08 '20 13:03 sobolevn