Commits

Torsten Marek  committed ffc9061

Emit W0102 (dangerous default value) when sets are used as default arguments for functions. Closes #92911

  • Participants
  • Parent commits 7b6b5d1

Comments (0)

Files changed (4)

 
 	--
 
+    * Emit W0102 when sets are used as default arguments in functions
+      (patch by tmarek@google.com)
+
     * #77982 Do not emit E0602 for loop variables of comprehensions
       used as argument values inside a decorator (patch by tmarek@google.com)
 

File checkers/base.py

                 else:
                     msg = '%s (%s)' % (default.as_string(), value.as_string())
                 self.add_message('W0102', node=node, args=(msg,))
+            if value.qname() == '__builtin__.set':
+                if isinstance(default, astng.CallFunc):
+                    msg = default.as_string()
+                else:
+                    msg = '%s (%s)' % (default.as_string(), value.qname())
+                self.add_message('W0102', node=node, args=(msg,))
 
     @check_messages('W0101', 'W0150')
     def visit_return(self, node):

File test/input/func_dangerous_default.py

 def function3(value):
     """docstring"""
     print value
+
+def function4(value = set()):
+    """set is mutable and dangerous."""
+    print value
+
+def function5(value = frozenset()):
+    """frozenset is immutable and safe."""
+    print value
+
+GLOBAL_SET = set()
+
+def function6(value = GLOBAL_SET):
+    """set is mutable and dangerous."""
+    print value
+

File test/messages/func_dangerous_default.txt

 W:  7:function1: Dangerous default value [] as argument
 W: 11:function2: Dangerous default value HEHE ({}) as argument
+W: 19:function4: Dangerous default value set() as argument
+W: 29:function6: Dangerous default value GLOBAL_SET (__builtin__.set) as argument