Generate all the ordering methods for this class.
(ctx: mypy.plugin.ClassDefContext, adder: MethodAdder)
| 819 | |
| 820 | |
| 821 | def _add_order(ctx: mypy.plugin.ClassDefContext, adder: MethodAdder) -> None: |
| 822 | """Generate all the ordering methods for this class.""" |
| 823 | bool_type = ctx.api.named_type("builtins.bool") |
| 824 | object_type = ctx.api.named_type("builtins.object") |
| 825 | # Make the types be: |
| 826 | # AT = TypeVar('AT') |
| 827 | # def __lt__(self: AT, other: AT) -> bool |
| 828 | # This way comparisons with subclasses will work correctly. |
| 829 | fullname = f"{ctx.cls.info.fullname}.{SELF_TVAR_NAME}" |
| 830 | tvd = TypeVarType( |
| 831 | SELF_TVAR_NAME, |
| 832 | fullname, |
| 833 | # Namespace is patched per-method below. |
| 834 | id=TypeVarId(-1, namespace=""), |
| 835 | values=[], |
| 836 | upper_bound=object_type, |
| 837 | default=AnyType(TypeOfAny.from_omitted_generics), |
| 838 | ) |
| 839 | self_tvar_expr = TypeVarExpr( |
| 840 | SELF_TVAR_NAME, fullname, [], object_type, AnyType(TypeOfAny.from_omitted_generics) |
| 841 | ) |
| 842 | ctx.cls.info.names[SELF_TVAR_NAME] = SymbolTableNode(MDEF, self_tvar_expr) |
| 843 | |
| 844 | for method in ["__lt__", "__le__", "__gt__", "__ge__"]: |
| 845 | namespace = f"{ctx.cls.info.fullname}.{method}" |
| 846 | tvd = tvd.copy_modified(id=TypeVarId(tvd.id.raw_id, namespace=namespace)) |
| 847 | args = [Argument(Var("other", tvd), tvd, None, ARG_POS)] |
| 848 | adder.add_method(method, args, bool_type, self_type=tvd, tvd=tvd) |
| 849 | |
| 850 | |
| 851 | def _make_frozen(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute]) -> None: |
no test coverage detected
searching dependent graphs…