Skip to content

Annotation of type[Protocol] causes error: Variable "typing.Protocol" is not valid as a type #14611

Closed as not planned
@daniil-berg

Description

@daniil-berg

Bug Report

Attempting to annotate something as any Protocol causes an error.

To Reproduce

from typing import Protocol

def f(p: type[Protocol]) -> None: ...

Expected Behavior

No problem.

Actual Behavior

error: Variable "typing.Protocol" is not valid as a type [valid-type]

Your Environment

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

Context

I realize that declaring x: Protocol is nonsensical because the base Protocol does not define any interface, so I might as well use Any. But I was expecting that I could express type[Protocol] to indicate that x should be a subclass (so nominal here) of Protocol, i.e. not a class that is a structural subtype of some protocol, but just one that inherits from Protocol.

I noticed that this may related to the request for higher-kinded types and the error when trying to express type[Generic] (#7791) is the same.

Is there a reason, why annotating with type[Protocol] (or type[Generic]) is not allowed? Also, the error message seems strange to me since Protocol (and Generic) are clearly not variables but regular old classes, even though they are also special typing constructs.

Not a big issue IMO, just thought I'd mention it.

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