Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for implicitly abstract functions #13776

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,15 +1341,16 @@ def cannot_instantiate_abstract_class(
return
if len(attrs_with_none) == 1:
note = (
"The following method was marked implicitly abstract because it has an empty "
"function body: {}. If it is not meant to be abstract, explicitly return None."
f"{attrs_with_none[0]} is implicitly abstract because it has an empty function "
"body. If it is not meant to be abstract, explicitly `return` or `return None`."
)
else:
note = (
"The following methods were marked implicitly abstract because they have empty "
"function bodies: {}. If they are not meant to be abstract, explicitly return None."
f"function bodies: {format_string_list(attrs_with_none)}. "
"If they are not meant to be abstract, explicitly `return` or `return None`."
)
self.note(note.format(format_string_list(attrs_with_none)), context, code=codes.ABSTRACT)
self.note(note, context, code=codes.ABSTRACT)

def base_class_definitions_incompatible(
self, name: str, base1: TypeInfo, base2: TypeInfo, context: Context
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -3105,14 +3105,14 @@ class NoneCompatible(Protocol):

class A(NoneCompatible): ...
A() # E: Cannot instantiate abstract class "A" with abstract attributes "f", "g", "h", "i" and "j" \
# 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.
# 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`.

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

class B(NoneCompatible2): ...
B() # E: Cannot instantiate abstract class "B" with abstract attribute "f" \
# 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.
# 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`.

class NoneCompatible3(Protocol):
@abstractmethod
Expand Down