Improve error message on incomplete protocol implementation
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Feature
When a class does not fully implement a protocol, more useful error messages should be printed.
Example 1: When a protocol is used only implicitly, the "incompatible type" [arg-type] message should print which function/property is missing/incompatible. (Similar to the end of Example 2.)
Example 2: When a protocol is used explicitly, an error message should printed near the class definition. not (only) near the instantiation (which can be far away in a different module).
from typing import Callable, Protocol
class Proto(Protocol):
fun: Callable[[], None]
def proto_fun(proto: Proto) -> None:
proto.fun()
class Class1:
pass
# Example 1
# In this case, we get a mypy error
# > Argument 1 to "proto_fun" has incompatible type "Class"; expected "Proto" [arg-type]
# which is expected both in terms of content and location.
# However, the error message should say why Class and Proto are not compatible.
proto_fun(Class1())
# Example 2
# Here, I would expect a mypy error already, as Class does not fully implement Proto.
# Instead, we do not get any error at all:
class Class2(Proto):
pass
# At least get a more information error message as in the case above:
# > Cannot instantiate abstract class "Class" with abstract attribute "fun" [abstract]
proto_fun(Class2())
Output:
bug.py:17: error: Argument 1 to "proto_fun" has incompatible type "Class1"; expected "Proto" [arg-type]
bug.py:27: error: Cannot instantiate abstract class "Class2" with abstract attribute "fun" [abstract]
Found 2 errors in 1 file (checked 1 source file)
Expected (just an idea):
bug.py:17: error: Argument 1 to "proto_fun" has incompatible type "Class1"; expected "Proto" which has abstract attribute "fun" [arg-protocol]
bug.py:22: error: "Class2" does not implement "Proto" which has abstract attribute "fun" [protocol]
bug.py:27: error: Cannot instantiate abstract class "Class2" with abstract attribute "fun" [abstract]
Found 3 errors in 1 file (checked 1 source file)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source files or tests are named. Start by reproducing the two protocol examples in bug.py and inspect the existing diagnostics for implicit and explicit protocol use; done means reporting the missing or incompatible member and emitting the explicit-implementation error near Class2, with the existing instantiation error preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100