|
| 1 | +"""Plugin for supporting the functools standard library module.""" |
| 2 | +from typing import Dict, NamedTuple, Optional |
| 3 | +from typing_extensions import Final |
| 4 | + |
| 5 | +import mypy.plugin |
| 6 | +from mypy.nodes import ARG_OPT, ARG_POS, ARG_STAR2, Argument, FuncItem, Var |
| 7 | +from mypy.plugins.common import add_method_to_class |
| 8 | +from mypy.types import AnyType, CallableType, Type, TypeOfAny, UnboundType |
| 9 | + |
| 10 | + |
| 11 | +functools_total_ordering_makers: Final = { |
| 12 | + 'functools.total_ordering', |
| 13 | +} |
| 14 | + |
| 15 | +_ORDERING_METHODS: Final = { |
| 16 | + '__lt__', |
| 17 | + '__le__', |
| 18 | + '__gt__', |
| 19 | + '__ge__', |
| 20 | +} |
| 21 | + |
| 22 | +class _MethodInfo(NamedTuple): |
| 23 | + is_static: bool |
| 24 | + type: CallableType |
| 25 | + |
| 26 | + |
| 27 | +def functools_total_ordering_maker_callback(ctx: mypy.plugin.ClassDefContext, |
| 28 | + auto_attribs_default: bool = False) -> None: |
| 29 | + """Add dunder methods to classes decorated with functools.total_ordering.""" |
| 30 | + if ctx.api.options.python_version < (3, 2): |
| 31 | + ctx.api.fail('"functools.total_ordering" is not supported in Python 2 or 3.1', ctx.reason) |
| 32 | + return |
| 33 | + |
| 34 | + comparison_methods = _analyze_class(ctx) |
| 35 | + if not comparison_methods: |
| 36 | + ctx.api.fail( |
| 37 | + 'No ordering operation defined when using "functools.total_ordering": < > <= >=', |
| 38 | + ctx.reason) |
| 39 | + return |
| 40 | + |
| 41 | + # prefer __lt__ to __le__ to __gt__ to __ge__ |
| 42 | + root = max(comparison_methods, key=lambda k: (comparison_methods[k] is None, k)) |
| 43 | + root_method = comparison_methods[root] |
| 44 | + if not root_method: |
| 45 | + # None of the defined comparison methods can be analysed |
| 46 | + return |
| 47 | + |
| 48 | + other_type = _find_other_type(root_method) |
| 49 | + bool_type = ctx.api.named_type('__builtins__.bool') |
| 50 | + if (root_method.type.ret_type != ctx.api.named_type('__builtins__.bool') |
| 51 | + and not ( |
| 52 | + isinstance(root_method.type.ret_type, UnboundType) |
| 53 | + and root_method.type.ret_type.name.endswith('bool'))): |
| 54 | + ret_type = AnyType(TypeOfAny.implementation_artifact) |
| 55 | + else: |
| 56 | + ret_type = bool_type |
| 57 | + for additional_op in _ORDERING_METHODS: |
| 58 | + # Either the method is not implemented |
| 59 | + # or has an unknown signature that we can now extrapolate. |
| 60 | + if not comparison_methods.get(additional_op): |
| 61 | + args = [Argument(Var('other', other_type), other_type, None, ARG_POS)] |
| 62 | + add_method_to_class(ctx.api, ctx.cls, additional_op, args, ret_type) |
| 63 | + |
| 64 | + |
| 65 | +def _find_other_type(method: _MethodInfo) -> Type: |
| 66 | + """Find the type of the ``other`` argument in a comparision method.""" |
| 67 | + first_arg_pos = 0 if method.is_static else 1 |
| 68 | + cur_pos_arg = 0 |
| 69 | + other_arg = None |
| 70 | + for arg_kind, arg_type in zip(method.type.arg_kinds, method.type.arg_types): |
| 71 | + if arg_kind in (ARG_POS, ARG_OPT): |
| 72 | + if cur_pos_arg == first_arg_pos: |
| 73 | + other_arg = arg_type |
| 74 | + break |
| 75 | + |
| 76 | + cur_pos_arg += 1 |
| 77 | + elif arg_kind != ARG_STAR2: |
| 78 | + other_arg = arg_type |
| 79 | + break |
| 80 | + |
| 81 | + if other_arg is None: |
| 82 | + return AnyType(TypeOfAny.implementation_artifact) |
| 83 | + |
| 84 | + return other_arg |
| 85 | + |
| 86 | + |
| 87 | +def _analyze_class(ctx: mypy.plugin.ClassDefContext) -> Dict[str, Optional[_MethodInfo]]: |
| 88 | + """Analyze the class body, its parents, and return the comparison methods found.""" |
| 89 | + # Traverse the MRO and collect ordering methods. |
| 90 | + comparison_methods = {} # type: Dict[str, FuncItem] |
| 91 | + # Skip object because total_ordering does not use methods from object |
| 92 | + for cls in ctx.cls.info.mro[:-1]: |
| 93 | + for name in _ORDERING_METHODS: |
| 94 | + if name in cls.names and name not in comparison_methods: |
| 95 | + node = cls.names[name].node |
| 96 | + if isinstance(node, FuncItem) and isinstance(node.type, CallableType): |
| 97 | + comparison_methods[name] = _MethodInfo(node.is_static, node.type) |
| 98 | + elif isinstance(node, Var) and isinstance(node.type, CallableType): |
| 99 | + comparison_methods[name] = _MethodInfo(node.is_staticmethod, node.type) |
| 100 | + else: |
| 101 | + comparison_methods[name] = None |
| 102 | + |
| 103 | + return comparison_methods |
0 commit comments