Commits

Michael Foord committed bd52c83

unittest.mock.MagicMock objects are now unorderable by default

  • Participants
  • Parent commits 7d83390

Comments (0)

Files changed (3)

File Lib/unittest/mock.py

     signature, func = result
 
     src = "lambda %s: None" % signature
-    context = {'_mock_': mock}
-    checksig = eval(src, context)
+    checksig = eval(src, {})
     _copy_func_details(func, checksig)
 
     name = original.__name__
     if not name.isidentifier():
         name = 'funcopy'
-    context = {'checksig': checksig, 'mock': mock}
+    context = {'_checksig_': checksig, 'mock': mock}
     src = """def %s(*args, **kwargs):
-    checksig(*args, **kwargs)
+    _checksig_(*args, **kwargs)
     return mock(*args, **kwargs)""" % name
     exec (src, context)
     funcopy = context[name]
 
     def __dir__(self):
         """Filter the output of `dir(mock)` to only useful members."""
+        if not FILTER_DIR:
+            return object.__dir__(self)
+
         extras = self._mock_methods or []
         from_type = dir(type(self))
         from_dict = list(self.__dict__)
 
-        if FILTER_DIR:
-            from_type = [e for e in from_type if not e.startswith('_')]
-            from_dict = [e for e in from_dict if not e.startswith('_') or
-                         _is_magic(e)]
+        from_type = [e for e in from_type if not e.startswith('_')]
+        from_dict = [e for e in from_dict if not e.startswith('_') or
+                     _is_magic(e)]
         return sorted(set(extras + from_type + from_dict +
                           list(self._mock_children)))
 
 }
 
 _return_values = {
+    '__lt__': NotImplemented,
+    '__gt__': NotImplemented,
+    '__le__': NotImplemented,
+    '__ge__': NotImplemented,
     '__int__': 1,
     '__contains__': False,
     '__len__': 0,
         _parent._mock_children[_name] = mock
 
     if is_type and not instance and 'return_value' not in kwargs:
-        # XXXX could give a name to the return_value mock?
         mock.return_value = create_autospec(spec, spec_set, instance=True,
                                             _name='()', _parent=mock)
 

File Lib/unittest/test/testmock/testmagicmethods.py

         self. assertTrue(mock <= 3)
         self. assertTrue(mock >= 3)
 
+        self.assertRaises(TypeError, lambda: MagicMock() < object())
+        self.assertRaises(TypeError, lambda: object() < MagicMock())
+        self.assertRaises(TypeError, lambda: MagicMock() < MagicMock())
+        self.assertRaises(TypeError, lambda: MagicMock() > object())
+        self.assertRaises(TypeError, lambda: object() > MagicMock())
+        self.assertRaises(TypeError, lambda: MagicMock() > MagicMock())
+        self.assertRaises(TypeError, lambda: MagicMock() <= object())
+        self.assertRaises(TypeError, lambda: object() <= MagicMock())
+        self.assertRaises(TypeError, lambda: MagicMock() <= MagicMock())
+        self.assertRaises(TypeError, lambda: MagicMock() >= object())
+        self.assertRaises(TypeError, lambda: object() >= MagicMock())
+        self.assertRaises(TypeError, lambda: MagicMock() >= MagicMock())
+
 
     def test_equality(self):
         for mock in Mock(), MagicMock():
 Library
 -------
 
+- Issue #14295: Add unittest.mock
+
 - Issue #7652: Add --with-system-libmpdec option to configure for linking
   the _decimal module against an installed libmpdec.