The assert visiting method for tirx. Parameters ---------- self : Parser The visiting parser. node : doc.Assert The doc AST assert node. The assert message can be either: - A plain string: ``assert cond, "message"`` - A tuple of (kind, [parts...]): ``as
(self: Parser, node: doc.Assert)
| 802 | |
| 803 | @dispatch.register(token="tirx", type_name="Assert") |
| 804 | def visit_assert(self: Parser, node: doc.Assert) -> None: |
| 805 | """The assert visiting method for tirx. |
| 806 | |
| 807 | Parameters |
| 808 | ---------- |
| 809 | self : Parser |
| 810 | The visiting parser. |
| 811 | |
| 812 | node : doc.Assert |
| 813 | The doc AST assert node. |
| 814 | |
| 815 | The assert message can be either: |
| 816 | - A plain string: ``assert cond, "message"`` |
| 817 | - A tuple of (kind, [parts...]): ``assert cond, ("ValueError", ["part0", "part1"])`` |
| 818 | """ |
| 819 | cond = self.eval_expr(node.test) |
| 820 | msg = self.eval_expr(node.msg) |
| 821 | |
| 822 | kind = "RuntimeError" |
| 823 | message = msg |
| 824 | |
| 825 | if isinstance(msg, tuple): |
| 826 | if len(msg) != 2: |
| 827 | self.report_error( |
| 828 | node, |
| 829 | f"Assert message tuple must have exactly 2 elements (kind, [parts...]), " |
| 830 | f"got {len(msg)} elements", |
| 831 | ) |
| 832 | kind_str, parts = msg |
| 833 | if isinstance(kind_str, tvm.tirx.StringImm): |
| 834 | kind_str = kind_str.value |
| 835 | if not isinstance(kind_str, str): |
| 836 | self.report_error( |
| 837 | node, |
| 838 | f"Assert message tuple first element must be a string (error kind like " |
| 839 | f'"ValueError"), got {type(kind_str).__name__}', |
| 840 | ) |
| 841 | kind = kind_str |
| 842 | message = parts |
| 843 | |
| 844 | if isinstance(message, list | tuple): |
| 845 | message = [p.value if isinstance(p, tvm.tirx.StringImm) else str(p) for p in message] |
| 846 | |
| 847 | frame = T.Assert(cond, message, error_kind=kind) |
| 848 | frame.add_callback(partial(frame.__exit__, None, None, None)) |
| 849 | frame.__enter__() |
| 850 | |
| 851 | |
| 852 | @dispatch.register(token="tirx", type_name="Return") |
nothing calls this directly
no test coverage detected
searching dependent graphs…