Exemple #1
0
    def test_warn_on_type_dict_non_split_keys(self):
        class C:
            pass

        msg = type = attr = None

        def cb(*args):
            nonlocal msg, type, attr
            msg = args[0]
            type = args[1]
            attr = args[2]

        cinder.cinder_set_warn_handler(cb)
        try:
            cinder.warn_on_inst_dict(C)

            a = C()
            a.foo = 42
            a.bar = 100

            a = C()
            a.baz = 100

            a = C()
            a.quox = 100
            self.assertEqual(msg, "WARN001: Dictionary created for flagged instance")
            self.assertEqual(type, C)
            self.assertEqual(attr, "quox")
            self.assertEqual(a.quox, 100)
        finally:
            cinder.cinder_set_warn_handler(None)
Exemple #2
0
def freeze_type(obj: Type[object]) -> Type[object]:
    if cinder_freeze is not None:
        if TYPE_FREEZE_ENABLED:
            cinder_freeze(obj)
        elif warn_on_inst_dict is not None:
            warn_on_inst_dict(obj)

    return obj
Exemple #3
0
    def test_warn_on_type_dict_no_callback(self):
        class C:
            pass

        cinder.warn_on_inst_dict(C)

        a = C()
        a.foo = 42
        self.assertEqual(a.foo, 42)
Exemple #4
0
    def test_warn_on_frozen_type(self):
        class C:
            pass

        cinder.freeze_type(C)

        with self.assertRaisesRegex(
                TypeError, "can't call warn_on_inst_dict on a frozen type"):
            cinder.warn_on_inst_dict(C)
Exemple #5
0
    def test_warn_on_type(self):
        class C:
            pass

        msg = type = attr = None

        def cb(*args):
            nonlocal msg, type, attr
            msg = args[0]
            type = args[1]
            attr = args[2]

        cinder.warn_on_inst_dict(C)
        cinder.freeze_type(C)

        cinder.cinder_set_warn_handler(cb)
        C.foo = 42

        self.assertEqual(
            msg, "WARN002: Type modified that was flagged for immutability")
        self.assertEqual(type, C)
        self.assertEqual(attr, "foo")
Exemple #6
0
def loose_slots(obj: Type[object]) -> Type[object]:
    """Indicates that a type defined in a strict module should support assigning
    additional variables to __dict__ to support migration."""
    if warn_on_inst_dict is not None:
        warn_on_inst_dict(obj)
    return obj
Exemple #7
0
 def test_warn_on_type_dict_non_type(self):
     with self.assertRaises(TypeError):
         cinder.warn_on_inst_dict(42)