How to use __subclasscheck__ method in freezegun

Best Python code snippet using freezegun

test_typechecks.py

Source:test_typechecks.py Github

copy

Full Screen

...3from test import test_support4class ABC(type):5 def __instancecheck__(cls, inst):6 """Implement isinstance(inst, cls)."""7 return any(cls.__subclasscheck__(c)8 for c in set([type(inst), inst.__class__]))9 def __subclasscheck__(cls, sub):10 """Implement issubclass(sub, cls)."""11 candidates = cls.__dict__.get("__subclass__", set()) | set([cls])12 return any(c in candidates for c in sub.mro())13class Integer:14 __metaclass__ = ABC15 __subclass__ = set([int])16class SubInt(Integer):17 pass18class TypeChecksTest(unittest.TestCase):19 def testIsSubclassInternal(self):20 self.assertEqual(Integer.__subclasscheck__(int), True)21 self.assertEqual(Integer.__subclasscheck__(float), False)22 def testIsSubclassBuiltin(self):23 self.assertEqual(issubclass(int, Integer), True)24 self.assertEqual(issubclass(int, (Integer,)), True)25 self.assertEqual(issubclass(float, Integer), False)26 self.assertEqual(issubclass(float, (Integer,)), False)27 def testIsInstanceBuiltin(self):28 self.assertEqual(isinstance(42, Integer), True)29 self.assertEqual(isinstance(42, (Integer,)), True)30 self.assertEqual(isinstance(3.14, Integer), False)31 self.assertEqual(isinstance(3.14, (Integer,)), False)32 def testIsInstanceActual(self):33 self.assertEqual(isinstance(Integer(), Integer), True)34 self.assertEqual(isinstance(Integer(), (Integer,)), True)35 def testIsSubclassActual(self):36 self.assertEqual(issubclass(Integer, Integer), True)37 self.assertEqual(issubclass(Integer, (Integer,)), True)38 def testSubclassBehavior(self):39 self.assertEqual(issubclass(SubInt, Integer), True)40 self.assertEqual(issubclass(SubInt, (Integer,)), True)41 self.assertEqual(issubclass(SubInt, SubInt), True)42 self.assertEqual(issubclass(SubInt, (SubInt,)), True)43 self.assertEqual(issubclass(Integer, SubInt), False)44 self.assertEqual(issubclass(Integer, (SubInt,)), False)45 self.assertEqual(issubclass(int, SubInt), False)46 self.assertEqual(issubclass(int, (SubInt,)), False)47 self.assertEqual(isinstance(SubInt(), Integer), True)48 self.assertEqual(isinstance(SubInt(), (Integer,)), True)49 self.assertEqual(isinstance(SubInt(), SubInt), True)50 self.assertEqual(isinstance(SubInt(), (SubInt,)), True)51 self.assertEqual(isinstance(42, SubInt), False)52 self.assertEqual(isinstance(42, (SubInt,)), False)53 def test_oldstyle(self):54 # These should just be ignored.55 class X:56 def __instancecheck__(self, inst):57 return True58 def __subclasscheck__(self, cls):59 return True60 class Sub(X): pass61 self.assertNotIsInstance(3, X)62 self.assertIsInstance(X(), X)63 self.assertFalse(issubclass(int, X))64 self.assertTrue(issubclass(Sub, X))65def test_main():66 test_support.run_unittest(TypeChecksTest)67if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run freezegun automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful