(self, info: TypeInfo, attributes: list[DataclassAttribute])
| 441 | ) |
| 442 | |
| 443 | def add_slots(self, info: TypeInfo, attributes: list[DataclassAttribute]) -> None: |
| 444 | existing_slots = info.names.get("__slots__") |
| 445 | slots_defined_by_plugin = existing_slots is not None and existing_slots.plugin_generated |
| 446 | if existing_slots is not None and not slots_defined_by_plugin: |
| 447 | # This means we have a slots conflict. |
| 448 | # Class explicitly specifies a different `__slots__` field. |
| 449 | # And `@dataclass(slots=True)` is used. |
| 450 | # In runtime this raises a type error. |
| 451 | self._api.fail( |
| 452 | '"{}" both defines "__slots__" and is used with "slots=True"'.format( |
| 453 | self._cls.name |
| 454 | ), |
| 455 | self._cls, |
| 456 | ) |
| 457 | return |
| 458 | |
| 459 | if any(p.slots is None for p in info.mro[1:-1]): |
| 460 | # At least one type in mro (excluding `self` and `object`) |
| 461 | # does not have concrete `__slots__` defined. Ignoring. |
| 462 | return |
| 463 | |
| 464 | generated_slots = {attr.name for attr in attributes} |
| 465 | info.slots = generated_slots |
| 466 | |
| 467 | # Now, insert `.__slots__` attribute to class namespace: |
| 468 | slots_type = TupleType( |
| 469 | [self._api.named_type("builtins.str") for _ in generated_slots], |
| 470 | self._api.named_type("builtins.tuple"), |
| 471 | ) |
| 472 | add_attribute_to_class( |
| 473 | self._api, |
| 474 | self._cls, |
| 475 | "__slots__", |
| 476 | slots_type, |
| 477 | overwrite_existing=slots_defined_by_plugin, |
| 478 | ) |
| 479 | |
| 480 | def reset_init_only_vars(self, info: TypeInfo, attributes: list[DataclassAttribute]) -> None: |
| 481 | """Remove init-only vars from the class and reset init var declarations.""" |
no test coverage detected