Skip to content

Commit 7819085

Browse files
authored
Improve error message for implicitly abstract functions (#13776)
Fixes #13770
1 parent a1eeddb commit 7819085

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

mypy/messages.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,15 +1341,16 @@ def cannot_instantiate_abstract_class(
13411341
return
13421342
if len(attrs_with_none) == 1:
13431343
note = (
1344-
"The following method was marked implicitly abstract because it has an empty "
1345-
"function body: {}. If it is not meant to be abstract, explicitly return None."
1344+
f"{attrs_with_none[0]} is implicitly abstract because it has an empty function "
1345+
"body. If it is not meant to be abstract, explicitly `return` or `return None`."
13461346
)
13471347
else:
13481348
note = (
13491349
"The following methods were marked implicitly abstract because they have empty "
1350-
"function bodies: {}. If they are not meant to be abstract, explicitly return None."
1350+
f"function bodies: {format_string_list(attrs_with_none)}. "
1351+
"If they are not meant to be abstract, explicitly `return` or `return None`."
13511352
)
1352-
self.note(note.format(format_string_list(attrs_with_none)), context, code=codes.ABSTRACT)
1353+
self.note(note, context, code=codes.ABSTRACT)
13531354

13541355
def base_class_definitions_incompatible(
13551356
self, name: str, base1: TypeInfo, base2: TypeInfo, context: Context

test-data/unit/check-protocols.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,14 +3105,14 @@ class NoneCompatible(Protocol):
31053105

31063106
class A(NoneCompatible): ...
31073107
A() # E: Cannot instantiate abstract class "A" with abstract attributes "f", "g", "h", "i" and "j" \
3108-
# N: The following methods were marked implicitly abstract because they have empty function bodies: "f", "g", "h", "i" and "j". If they are not meant to be abstract, explicitly return None.
3108+
# N: The following methods were marked implicitly abstract because they have empty function bodies: "f", "g", "h", "i" and "j". If they are not meant to be abstract, explicitly `return` or `return None`.
31093109

31103110
class NoneCompatible2(Protocol):
31113111
def f(self, x: int): ...
31123112

31133113
class B(NoneCompatible2): ...
31143114
B() # E: Cannot instantiate abstract class "B" with abstract attribute "f" \
3115-
# N: The following method was marked implicitly abstract because it has an empty function body: "f". If it is not meant to be abstract, explicitly return None.
3115+
# N: "f" is implicitly abstract because it has an empty function body. If it is not meant to be abstract, explicitly `return` or `return None`.
31163116

31173117
class NoneCompatible3(Protocol):
31183118
@abstractmethod

0 commit comments

Comments
 (0)