Test that StructMeta can be inherited in Python code.
()
| 113 | |
| 114 | |
| 115 | def test_struct_meta_inheritance(): |
| 116 | """Test that StructMeta can be inherited in Python code.""" |
| 117 | |
| 118 | class CustomMeta(StructMeta): |
| 119 | """A custom metaclass that inherits from StructMeta. |
| 120 | |
| 121 | This metaclass adds a kw_only_default parameter that can be used to |
| 122 | set the default kw_only value for all subclasses. |
| 123 | |
| 124 | When a class is created with this metaclass: |
| 125 | 1. If kw_only is explicitly specified, use that value |
| 126 | 2. If kw_only is not specified but kw_only_default is, use kw_only_default |
| 127 | 3. If neither is specified but a parent class has kw_only_default defined, |
| 128 | use the parent's kw_only_default |
| 129 | 4. Otherwise, default to False |
| 130 | """ |
| 131 | |
| 132 | # Class attribute to store kw_only_default settings for each class |
| 133 | _kw_only_default_settings = {} |
| 134 | |
| 135 | def __new__(mcls, name, bases, namespace, **kwargs): |
| 136 | # Check if kw_only is explicitly specified |
| 137 | kw_only_specified = "kw_only" in kwargs |
| 138 | |
| 139 | # Process kw_only_default parameter |
| 140 | kw_only_default = kwargs.pop("kw_only_default", None) |
| 141 | |
| 142 | # If kw_only_default is specified, store it |
| 143 | if kw_only_default is not None: |
| 144 | # Remember this setting for future subclasses |
| 145 | mcls._kw_only_default_settings[name] = kw_only_default |
| 146 | else: |
| 147 | # Check if any parent class has kw_only_default defined |
| 148 | for base in bases: |
| 149 | base_name = base.__name__ |
| 150 | if base_name in mcls._kw_only_default_settings: |
| 151 | # Use parent's kw_only_default |
| 152 | kw_only_default = mcls._kw_only_default_settings[base_name] |
| 153 | break |
| 154 | |
| 155 | # If kw_only is not specified but kw_only_default is available, use it |
| 156 | if not kw_only_specified and kw_only_default is not None: |
| 157 | kwargs["kw_only"] = kw_only_default |
| 158 | |
| 159 | # Create the class |
| 160 | return super().__new__(mcls, name, bases, namespace, **kwargs) |
| 161 | |
| 162 | # Test basic functionality - without kw_only_default |
| 163 | class SimpleModel(metaclass=CustomMeta): |
| 164 | x: int |
| 165 | y: str |
| 166 | |
| 167 | # Verify the class was created correctly |
| 168 | assert isinstance(SimpleModel, CustomMeta) |
| 169 | assert issubclass(CustomMeta, StructMeta) |
| 170 | |
| 171 | # Test creating an instance with positional arguments (should work) |
| 172 | instance = SimpleModel(1, "test") |
nothing calls this directly
no test coverage detected
searching dependent graphs…