KEMBAR78
Helpful detailed error message on Protocol structural subtyping missing when arg is Optional · Issue #10129 · python/mypy · GitHub
Skip to content

Helpful detailed error message on Protocol structural subtyping missing when arg is Optional #10129

@nipunn1313

Description

@nipunn1313

Bug Report

from typing import Any, Optional, Protocol

class Session(Protocol):
    def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None: ...

class CoolSession(Protocol):
    def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None: ...
    def cool(self) -> None: ...

def func1(arg: Session) -> None: ...        
def func2(arg: Optional[Session]) -> None: ...

x: CoolSession
func1(x)
func2(x)

I would expect the same error message for both func1 and func2. Instead we see a useful message on 14 and a less useful message on 15

main.py:14: error: Argument 1 to "func1" has incompatible type "CoolSession"; expected "Session"
main.py:14: note: Following member(s) of "CoolSession" have conflicts:
main.py:14: note:     Expected:
main.py:14: note:         def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None
main.py:14: note:     Got:
main.py:14: note:         def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None
main.py:15: error: Argument 1 to "func2" has incompatible type "CoolSession"; expected "Optional[Session]"
Found 2 errors in 1 file (checked 1 source file)

mypy-playground repro:
https://mypy-play.net/?mypy=latest&python=3.9&gist=d00664f08c00a13896bfa4e45f6e2bf1

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions