|
13 | 13 | from collections import OrderedDict
|
14 | 14 | import re
|
15 | 15 | import difflib
|
| 16 | +from textwrap import dedent |
16 | 17 |
|
17 | 18 | from typing import cast, List, Dict, Any, Sequence, Iterable, Tuple, Set, Optional, Union
|
18 | 19 |
|
@@ -868,20 +869,19 @@ def argument_incompatible_with_supertype(
|
868 | 869 | self.fail('Argument {} of "{}" incompatible with {}'
|
869 | 870 | .format(arg_num, name, target), context)
|
870 | 871 |
|
871 |
| - if name in ("__eq__", "__ne__"): |
872 |
| - multiline_msg = self.comparison_method_example_msg(name) |
| 872 | + if name == "__eq__": |
| 873 | + assert isinstance(context, FuncDef) |
| 874 | + multiline_msg = self.comparison_method_example_msg(class_name=context.info.name()) |
873 | 875 | self.note_multiline(multiline_msg, context)
|
874 | 876 |
|
875 |
| - def comparison_method_example_msg(self, method_name: str) -> str: |
876 |
| - return '''It is recommended for "{method_name}" to work with arbitrary objects. |
877 |
| -The snippet below shows an example of how you can implement "{method_name}": |
878 |
| -
|
879 |
| -class Foo(...): |
880 |
| - ... |
881 |
| - def {method_name}(self, other: object) -> bool: |
882 |
| - if not isinstance(other, Foo): |
883 |
| - raise NotImplementedError |
884 |
| - return <logic to compare two Foo instances>'''.format(method_name=method_name) |
| 877 | + def comparison_method_example_msg(self, class_name: str) -> str: |
| 878 | + return dedent('''\ |
| 879 | + It is recommended for "__eq__" to work with arbitrary objects, for example: |
| 880 | + def __eq__(self, other: object) -> bool: |
| 881 | + if not isinstance(other, {class_name}): |
| 882 | + raise NotImplementedError |
| 883 | + return <logic to compare two {class_name} instances> |
| 884 | + '''.format(class_name=class_name)) |
885 | 885 |
|
886 | 886 | def return_type_incompatible_with_supertype(
|
887 | 887 | self, name: str, name_in_supertype: str, supertype: str,
|
|
0 commit comments