Skip to content

Commit a630c67

Browse files
committed
Adapt __eq__ note:
* Use textwrap.dedent to have a nicer multiline string * Only show the note for __eq__ * Use the actual class name in the shown code snippet * Adapt the test
1 parent 16178b2 commit a630c67

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

mypy/messages.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from collections import OrderedDict
1414
import re
1515
import difflib
16+
from textwrap import dedent
1617

1718
from typing import cast, List, Dict, Any, Sequence, Iterable, Tuple, Set, Optional, Union
1819

@@ -868,20 +869,19 @@ def argument_incompatible_with_supertype(
868869
self.fail('Argument {} of "{}" incompatible with {}'
869870
.format(arg_num, name, target), context)
870871

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())
873875
self.note_multiline(multiline_msg, context)
874876

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))
885885

886886
def return_type_incompatible_with_supertype(
887887
self, name: str, name_in_supertype: str, supertype: str,

test-data/unit/check-classes.test

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -282,32 +282,17 @@ class B(A):
282282
main:7: error: Argument 1 of "f" incompatible with supertype "A"
283283
main:9: error: Return type of "h" incompatible with supertype "A"
284284

285-
[case testEqNeMethodsOverridingWithNonObjects]
285+
[case testEqMethodsOverridingWithNonObjects]
286286
class A:
287287
def __eq__(self, other: A) -> bool: pass # Fail
288-
def __ne__(self, other: A) -> bool: pass # Fail
289288
[builtins fixtures/attr.pyi]
290289
[out]
291290
main:2: error: Argument 1 of "__eq__" incompatible with supertype "object"
292-
main:2: note: It is recommended for "__eq__" to work with arbitrary objects.
293-
main:2: note: The snippet below shows an example of how you can implement "__eq__":
294-
main:2: note:
295-
main:2: note: class Foo(...):
296-
main:2: note: ...
291+
main:2: note: It is recommended for "__eq__" to work with arbitrary objects, for example:
297292
main:2: note: def __eq__(self, other: object) -> bool:
298-
main:2: note: if not isinstance(other, Foo):
293+
main:2: note: if not isinstance(other, A):
299294
main:2: note: raise NotImplementedError
300-
main:2: note: return <logic to compare two Foo instances>
301-
main:3: error: Argument 1 of "__ne__" incompatible with supertype "object"
302-
main:3: note: It is recommended for "__ne__" to work with arbitrary objects.
303-
main:3: note: The snippet below shows an example of how you can implement "__ne__":
304-
main:3: note:
305-
main:3: note: class Foo(...):
306-
main:3: note: ...
307-
main:3: note: def __ne__(self, other: object) -> bool:
308-
main:3: note: if not isinstance(other, Foo):
309-
main:3: note: raise NotImplementedError
310-
main:3: note: return <logic to compare two Foo instances>
295+
main:2: note: return <logic to compare two A instances>
311296

312297
[case testMethodOverridingWithIncompatibleArgumentCount]
313298
import typing

0 commit comments

Comments
 (0)