Skip to content

Conversation

Gatsik
Copy link

@Gatsik Gatsik commented Aug 13, 2025

Reporting missing members can't confuse user even more -- it can only help to see what's missing. Also, sometimes implementing missing member helps mypy to correctly infer the type it couldn't infer before

I'm not sure if a separate test is needed for that, but here's a silly example:

from typing import Protocol, reveal_type

class CMD[T](Protocol):
    def command(self) -> T:
        ...
    def arguments(self) -> list[T]:
        ...

class CMDImpl:
    def command(self) -> str:
        return ""
    # def arguments(self) -> list[str]:
    #     return []

class CMDRunner[T]:
    def __init__(self, cmd: CMD[T]) -> None:
        self.cmd = cmd


r = CMDRunner(CMDImpl())
reveal_type(r)
$ mypy t.py

t.py:20: error: Need type annotation for "r"  [var-annotated]
t.py:20: error: Argument 1 to "CMDRunner" has incompatible type "CMDImpl"; expected "CMD[Never]"  [arg-type]
t.py:21: note: Revealed type is "t.CMDRunner[Any]"
Found 2 errors in 1 file (checked 1 source file)

whereas after uncommenting missing protocol member implementation:

$ mypy t.py

t.py:21: note: Revealed type is "t.CMDRunner[builtins.str]"
Success: no issues found in 1 source file

and the note about this missing member would've had pointed to the cause:

t.py:20: error: Need type annotation for "r"  [var-annotated]
t.py:20: error: Argument 1 to "CMDRunner" has incompatible type "CMDImpl"; expected "CMD[Never]"  [arg-type]
t.py:20: note: "CMDImpl" is missing following "CMD" protocol member:
t.py:20: note:     arguments
t.py:21: note: Revealed type is "t.CMDRunner[Any]"
Found 2 errors in 1 file (checked 1 source file)

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant