Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
daniil-berg opened this issue Feb 4, 2023 · 1 comment
Labels
bug mypy got something wrong

Comments

@daniil-berg
Copy link

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.

@daniil-berg daniil-berg added the bug mypy got something wrong label Feb 4, 2023
@hauntsaninja
Copy link
Collaborator

Thanks! I don't think there is really behaviour here that makes sense (e.g. easy to come up with examples here that would break transitivity or other properties that are important for reasoning about types). In general, static type checkers don't try to mirror the implementation details of the runtime (as evidenced by the fact that mypy doesn't know the details of what Protocol is). The runtime implementations of typing features are liable to change and highly complex, modelling them isn't useful for most users (and can result in usability regressions). The users who want to do this kind of thing are typically already doing a lot of very magical runtime type checking and can just add more runtime type checks.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants