(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute])
| 947 | |
| 948 | |
| 949 | def _add_slots(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute]) -> None: |
| 950 | if any(p.slots is None for p in ctx.cls.info.mro[1:-1]): |
| 951 | # At least one type in mro (excluding `self` and `object`) |
| 952 | # does not have concrete `__slots__` defined. Ignoring. |
| 953 | return |
| 954 | |
| 955 | # Unlike `@dataclasses.dataclass`, `__slots__` is rewritten here. |
| 956 | ctx.cls.info.slots = {attr.name for attr in attributes} |
| 957 | |
| 958 | # Also, inject `__slots__` attribute to class namespace: |
| 959 | slots_type = TupleType( |
| 960 | [ctx.api.named_type("builtins.str") for _ in attributes], |
| 961 | fallback=ctx.api.named_type("builtins.tuple"), |
| 962 | ) |
| 963 | add_attribute_to_class(api=ctx.api, cls=ctx.cls, name="__slots__", typ=slots_type) |
| 964 | |
| 965 | |
| 966 | def _add_match_args(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute]) -> None: |
no test coverage detected
searching dependent graphs…