How to use __init_subclass__ method in molecule

Best Python code snippet using molecule_python

test_subclassinit.py

Source:test_subclassinit.py Github

copy

Full Screen

...4class Test(unittest.TestCase):5 def test_init_subclass(self):6 class A:7 initialized = False8 def __init_subclass__(cls):9 super().__init_subclass__()10 cls.initialized = True11 class B(A):12 pass13 self.assertFalse(A.initialized)14 self.assertTrue(B.initialized)15 def test_init_subclass_dict(self):16 class A(dict):17 initialized = False18 def __init_subclass__(cls):19 super().__init_subclass__()20 cls.initialized = True21 class B(A):22 pass23 self.assertFalse(A.initialized)24 self.assertTrue(B.initialized)25 def test_init_subclass_kwargs(self):26 class A:27 def __init_subclass__(cls, **kwargs):28 cls.kwargs = kwargs29 class B(A, x=3):30 pass31 self.assertEqual(B.kwargs, dict(x=3))32 def test_init_subclass_error(self):33 class A:34 def __init_subclass__(cls):35 raise RuntimeError36 with self.assertRaises(RuntimeError):37 class B(A):38 pass39 def test_init_subclass_wrong(self):40 class A:41 def __init_subclass__(cls, whatever):42 pass43 with self.assertRaises(TypeError):44 class B(A):45 pass46 def test_init_subclass_skipped(self):47 class BaseWithInit:48 def __init_subclass__(cls, **kwargs):49 super().__init_subclass__(**kwargs)50 cls.initialized = cls51 class BaseWithoutInit(BaseWithInit):52 pass53 class A(BaseWithoutInit):54 pass55 self.assertIs(A.initialized, A)56 self.assertIs(BaseWithoutInit.initialized, BaseWithoutInit)57 def test_init_subclass_diamond(self):58 class Base:59 def __init_subclass__(cls, **kwargs):60 super().__init_subclass__(**kwargs)61 cls.calls = []62 class Left(Base):63 pass64 class Middle:65 def __init_subclass__(cls, middle, **kwargs):66 super().__init_subclass__(**kwargs)67 cls.calls += [middle]68 class Right(Base):69 def __init_subclass__(cls, right="right", **kwargs):70 super().__init_subclass__(**kwargs)71 cls.calls += [right]72 class A(Left, Middle, Right, middle="middle"):73 pass74 self.assertEqual(A.calls, ["right", "middle"])75 self.assertEqual(Left.calls, [])76 self.assertEqual(Right.calls, [])77 def test_set_name(self):78 class Descriptor:79 def __set_name__(self, owner, name):80 self.owner = owner81 self.name = name82 class A:83 d = Descriptor()84 self.assertEqual(A.d.name, "d")85 self.assertIs(A.d.owner, A)86 def test_set_name_metaclass(self):87 class Meta(type):88 def __new__(cls, name, bases, ns):89 ret = super().__new__(cls, name, bases, ns)90 self.assertEqual(ret.d.name, "d")91 self.assertIs(ret.d.owner, ret)92 return 093 class Descriptor:94 def __set_name__(self, owner, name):95 self.owner = owner96 self.name = name97 class A(metaclass=Meta):98 d = Descriptor()99 self.assertEqual(A, 0)100 def test_set_name_error(self):101 class Descriptor:102 def __set_name__(self, owner, name):103 1/0104 with self.assertRaises(RuntimeError) as cm:105 class NotGoingToWork:106 attr = Descriptor()107 exc = cm.exception108 self.assertRegex(str(exc), r'\bNotGoingToWork\b')109 self.assertRegex(str(exc), r'\battr\b')110 self.assertRegex(str(exc), r'\bDescriptor\b')111 self.assertIsInstance(exc.__cause__, ZeroDivisionError)112 def test_set_name_wrong(self):113 class Descriptor:114 def __set_name__(self):115 pass116 with self.assertRaises(RuntimeError) as cm:117 class NotGoingToWork:118 attr = Descriptor()119 exc = cm.exception120 self.assertRegex(str(exc), r'\bNotGoingToWork\b')121 self.assertRegex(str(exc), r'\battr\b')122 self.assertRegex(str(exc), r'\bDescriptor\b')123 self.assertIsInstance(exc.__cause__, TypeError)124 def test_set_name_lookup(self):125 resolved = []126 class NonDescriptor:127 def __getattr__(self, name):128 resolved.append(name)129 class A:130 d = NonDescriptor()131 self.assertNotIn('__set_name__', resolved,132 '__set_name__ is looked up in instance dict')133 def test_set_name_init_subclass(self):134 class Descriptor:135 def __set_name__(self, owner, name):136 self.owner = owner137 self.name = name138 class Meta(type):139 def __new__(cls, name, bases, ns):140 self = super().__new__(cls, name, bases, ns)141 self.meta_owner = self.owner142 self.meta_name = self.name143 return self144 class A:145 def __init_subclass__(cls):146 cls.owner = cls.d.owner147 cls.name = cls.d.name148 class B(A, metaclass=Meta):149 d = Descriptor()150 self.assertIs(B.owner, B)151 self.assertEqual(B.name, 'd')152 self.assertIs(B.meta_owner, B)153 self.assertEqual(B.name, 'd')154 def test_set_name_modifying_dict(self):155 notified = []156 class Descriptor:157 def __set_name__(self, owner, name):158 setattr(owner, name + 'x', None)159 notified.append(name)...

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 molecule 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