Skip to content

Commit cb8b028

Browse files
committed
Can understand functools.total_ordering
1 parent 952ca23 commit cb8b028

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed

mypy/plugins/default.py

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def get_class_decorator_hook(self, fullname: str
9393
) -> Optional[Callable[[ClassDefContext], None]]:
9494
from mypy.plugins import attrs
9595
from mypy.plugins import dataclasses
96+
from mypy.plugins import functools
9697

9798
if fullname in attrs.attr_class_makers:
9899
return attrs.attr_class_maker_callback
@@ -103,6 +104,9 @@ def get_class_decorator_hook(self, fullname: str
103104
)
104105
elif fullname in dataclasses.dataclass_makers:
105106
return dataclasses.dataclass_class_maker_callback
107+
elif fullname in functools.functools_total_ordering_makers:
108+
return functools.functools_total_ordering_maker_callback
109+
106110
return None
107111

108112

mypy/plugins/functools.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

mypy/test/testcheck.py

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
'check-errorcodes.test',
9292
'check-annotated.test',
9393
'check-parameter-specification.test',
94+
'check-functools.test',
9495
]
9596

9697
# Tests that use Python 3.8-only AST features (like expression-scoped ignores):

test-data/unit/check-functools.test

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
[case testTotalOrderingEqLt]
2+
from functools import total_ordering
3+
4+
@total_ordering
5+
class Ord:
6+
def __eq__(self, other: object) -> bool:
7+
return False
8+
9+
def __lt__(self, other: "Ord") -> bool:
10+
return False
11+
12+
reveal_type(Ord() < Ord()) # N: Revealed type is 'builtins.bool'
13+
reveal_type(Ord() <= Ord()) # N: Revealed type is 'builtins.bool'
14+
reveal_type(Ord() == Ord()) # N: Revealed type is 'builtins.bool'
15+
reveal_type(Ord() > Ord()) # N: Revealed type is 'builtins.bool'
16+
reveal_type(Ord() >= Ord()) # N: Revealed type is 'builtins.bool'
17+
18+
Ord() < 1 # E: Unsupported operand types for < ("Ord" and "int")
19+
Ord() <= 1 # E: Unsupported operand types for <= ("Ord" and "int")
20+
Ord() == 1
21+
Ord() > 1 # E: Unsupported operand types for > ("Ord" and "int")
22+
Ord() >= 1 # E: Unsupported operand types for >= ("Ord" and "int")
23+
[builtins fixtures/ops.pyi]
24+
[builtins fixtures/dict.pyi]
25+
26+
[case testTotalOrderingLambda]
27+
from functools import total_ordering
28+
from typing import Any, Callable
29+
30+
@total_ordering
31+
class Ord:
32+
__eq__: Callable[[Any, object], bool] = lambda self, other: False
33+
__lt__: Callable[[Any, "Ord"], bool] = lambda self, other: False
34+
35+
reveal_type(Ord() < Ord()) # N: Revealed type is 'builtins.bool'
36+
reveal_type(Ord() <= Ord()) # N: Revealed type is 'builtins.bool'
37+
reveal_type(Ord() == Ord()) # N: Revealed type is 'builtins.bool'
38+
reveal_type(Ord() > Ord()) # N: Revealed type is 'builtins.bool'
39+
reveal_type(Ord() >= Ord()) # N: Revealed type is 'builtins.bool'
40+
41+
Ord() < 1 # E: Argument 1 has incompatible type "int"; expected "Ord"
42+
Ord() <= 1 # E: Unsupported operand types for <= ("Ord" and "int")
43+
Ord() == 1
44+
Ord() > 1 # E: Unsupported operand types for > ("Ord" and "int")
45+
Ord() >= 1 # E: Unsupported operand types for >= ("Ord" and "int")
46+
[builtins fixtures/ops.pyi]
47+
[builtins fixtures/dict.pyi]
48+
49+
[case testTotalOrderingNonCallable]
50+
from functools import total_ordering
51+
52+
@total_ordering
53+
class Ord(object):
54+
def __eq__(self, other: object) -> bool:
55+
return False
56+
57+
__lt__ = 5
58+
59+
Ord() <= Ord() # E: Unsupported left operand type for <= ("Ord")
60+
Ord() > Ord() # E: "int" not callable
61+
Ord() >= Ord() # E: Unsupported left operand type for >= ("Ord")
62+
63+
[builtins fixtures/ops.pyi]
64+
[builtins fixtures/dict.pyi]
65+
66+
[case testTotalOrderingReturnNotBool]
67+
from functools import total_ordering
68+
69+
@total_ordering
70+
class Ord:
71+
def __eq__(self, other: object) -> bool:
72+
return False
73+
74+
def __lt__(self, other: "Ord") -> str:
75+
return "blah"
76+
77+
reveal_type(Ord() < Ord()) # N: Revealed type is 'builtins.str'
78+
reveal_type(Ord() <= Ord()) # N: Revealed type is 'Any'
79+
reveal_type(Ord() == Ord()) # N: Revealed type is 'builtins.bool'
80+
reveal_type(Ord() > Ord()) # N: Revealed type is 'Any'
81+
reveal_type(Ord() >= Ord()) # N: Revealed type is 'Any'
82+
83+
[builtins fixtures/ops.pyi]
84+
[builtins fixtures/dict.pyi]
85+
86+
[case testTotalOrderingAllowsAny]
87+
from functools import total_ordering
88+
89+
@total_ordering
90+
class Ord:
91+
def __eq__(self, other):
92+
return False
93+
94+
def __gt__(self, other):
95+
return False
96+
97+
reveal_type(Ord() < Ord()) # N: Revealed type is 'Any'
98+
Ord() <= Ord() # E: Unsupported left operand type for <= ("Ord")
99+
reveal_type(Ord() == Ord()) # N: Revealed type is 'Any'
100+
reveal_type(Ord() > Ord()) # N: Revealed type is 'Any'
101+
Ord() >= Ord() # E: Unsupported left operand type for >= ("Ord")
102+
103+
Ord() < 1 # E: Unsupported left operand type for < ("Ord")
104+
Ord() <= 1 # E: Unsupported left operand type for <= ("Ord")
105+
Ord() == 1
106+
Ord() > 1
107+
Ord() >= 1 # E: Unsupported left operand type for >= ("Ord")
108+
[builtins fixtures/ops.pyi]
109+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)