Skip to content

Parameterized protocol structure ignored as argument #14798

Open
@markedwards

Description

@markedwards

Bug Report

Parameterized protocols seem to be largely or totally ignored when specified as argument types. Simple example:

from typing import Protocol, TypeVar

T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)


class Proto(Protocol[T_co]):
    def __call__(self, *, a: str) -> T_co:
        ...


class InvalidClass:
    def __init__(self, *, a: int) -> None:
        ...


def test(b: Proto[T]) -> T:
    return b(a="0")
    

test(InvalidClass)  # <-- should fail type checking

InvalidClass is accepted here even though it does not conform to the protocol. Here's a more detailed playground gist.

As demonstrated in the gist, the issue does not reproduce when a function is provided, only with a class. It also does not manifest with assignment, only with arguments. However, assigning with the parameter set to Any also produces an unexpected result, which might somehow be related.

  • Mypy version used: 1.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions