MCPcopy Create free account
hub / github.com/python/mypy / _add_order

Function _add_order

mypy/plugins/attrs.py:821–848  ·  view source on GitHub ↗

Generate all the ordering methods for this class.

(ctx: mypy.plugin.ClassDefContext, adder: MethodAdder)

Source from the content-addressed store, hash-verified

819
820
821def _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
851def _make_frozen(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute]) -> None:

Callers 1

Calls 10

copy_modifiedMethod · 0.95
TypeVarTypeClass · 0.90
TypeVarIdClass · 0.90
AnyTypeClass · 0.90
TypeVarExprClass · 0.90
SymbolTableNodeClass · 0.90
ArgumentClass · 0.90
VarClass · 0.90
named_typeMethod · 0.45
add_methodMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…