code: def test_dynamic_attributes():
      from pybind11_tests import DynamicClass, CppDerivedDynamicClass
  
      instance = DynamicClass()
      assert not hasattr(instance, "foo")
      assert "foo" not in dir(instance)
  
      # Dynamically add attribute
      instance.foo = 42
      assert hasattr(instance, "foo")
      assert instance.foo == 42
      assert "foo" in dir(instance)
  
      # __dict__ should be accessible and replaceable
      assert "foo" in instance.__dict__
      instance.__dict__ = {"bar": True}
      assert not hasattr(instance, "foo")
      assert hasattr(instance, "bar")
  
      with pytest.raises(TypeError) as excinfo:
          instance.__dict__ = []
      assert (
          str(excinfo.value)
          == "__dict__ must be set to a dictionary, not a 'list'"
      )
  
      cstats = ConstructorStats.get(DynamicClass)
      assert cstats.alive() == 1
      del instance
      assert cstats.alive() == 0
  
      # Derived classes should work as well
      class PythonDerivedDynamicClass(DynamicClass):
          pass
  
      for cls in CppDerivedDynamicClass, PythonDerivedDynamicClass:
          derived = cls()
          derived.foobar = 100
          assert derived.foobar == 100
  
          assert cstats.alive() == 1
          del derived
          assert cstats.alive() == 0
  
file_path: turicreate/src/external/coremltools_wrap/coremltools/deps/pybind11/tests/test_methods_and_attributes.py
imports: import pytest
  from pybind11_tests import ExampleMandA, ConstructorStats
label_desc: Use image node_id 7 for calling a global function with example usage: test_dynamic_attributes() without return types