A dataclass.
| 2239 | |
| 2240 | @dataclasses.dataclass |
| 2241 | class ModelDC: |
| 2242 | """A dataclass.""" |
| 2243 | |
| 2244 | foo: str = "bar" |
| 2245 | ls: list[dict] = dataclasses.field(default_factory=list) |
| 2246 | |
| 2247 | def set_foo(self, val: str): |
| 2248 | """Set the attribute foo. |
| 2249 | |
| 2250 | Args: |
| 2251 | val: The value to set. |
| 2252 | """ |
| 2253 | self.foo = val |
| 2254 | |
| 2255 | def double_foo(self) -> str: |
| 2256 | """Concatenate foo with foo. |
| 2257 | |
| 2258 | Returns: |
| 2259 | foo + foo |
| 2260 | """ |
| 2261 | return self.foo + self.foo |
| 2262 | |
| 2263 | def copy(self, **kwargs) -> ModelDC: |
| 2264 | """Create a copy of the dataclass with updated fields. |
| 2265 | |
| 2266 | Returns: |
| 2267 | A new instance of ModelDC with updated fields. |
| 2268 | """ |
| 2269 | return dataclasses.replace(self, **kwargs) |
| 2270 | |
| 2271 | def append_to_ls(self, item: dict): |
| 2272 | """Append an item to the list attribute ls. |
| 2273 | |
| 2274 | Args: |
| 2275 | item: The item to append. |
| 2276 | """ |
| 2277 | self.ls.append(item) |
| 2278 | |
| 2279 | @classmethod |
| 2280 | def from_dict(cls, data: dict) -> ModelDC: |
| 2281 | """Create an instance of ModelDC from a dictionary. |
| 2282 | |
| 2283 | Args: |
| 2284 | data: The dictionary to create the instance from. |
| 2285 | |
| 2286 | Returns: |
| 2287 | An instance of ModelDC. |
| 2288 | """ |
| 2289 | return cls(**data) |
| 2290 | |
| 2291 | |
| 2292 | @pytest.mark.asyncio |
no outgoing calls