Handle decorators in class bodies. `x.default` will set a default value on x `x.validator` and `x.default` will get removed to avoid throwing a type error.
(stmt: Decorator, attr_map: dict[str, Attribute])
| 575 | |
| 576 | |
| 577 | def _cleanup_decorator(stmt: Decorator, attr_map: dict[str, Attribute]) -> None: |
| 578 | """Handle decorators in class bodies. |
| 579 | |
| 580 | `x.default` will set a default value on x |
| 581 | `x.validator` and `x.default` will get removed to avoid throwing a type error. |
| 582 | """ |
| 583 | remove_me = [] |
| 584 | for func_decorator in stmt.decorators: |
| 585 | if ( |
| 586 | isinstance(func_decorator, MemberExpr) |
| 587 | and isinstance(func_decorator.expr, NameExpr) |
| 588 | and func_decorator.expr.name in attr_map |
| 589 | ): |
| 590 | if func_decorator.name == "default": |
| 591 | attr_map[func_decorator.expr.name].has_default = True |
| 592 | |
| 593 | if func_decorator.name in ("default", "validator"): |
| 594 | # These are decorators on the attrib object that only exist during |
| 595 | # class creation time. In order to not trigger a type error later we |
| 596 | # just remove them. This might leave us with a Decorator with no |
| 597 | # decorators (Emperor's new clothes?) |
| 598 | # TODO: It would be nice to type-check these rather than remove them. |
| 599 | # default should be Callable[[], T] |
| 600 | # validator should be Callable[[Any, 'Attribute', T], Any] |
| 601 | # where T is the type of the attribute. |
| 602 | remove_me.append(func_decorator) |
| 603 | for dec in remove_me: |
| 604 | stmt.decorators.remove(dec) |
| 605 | |
| 606 | |
| 607 | def _attribute_from_auto_attrib( |
no test coverage detected
searching dependent graphs…