typing icon indicating copy to clipboard operation
typing copied to clipboard

[spec] a protocol type should be assignable to object?

Open carljm opened this issue 1 year ago • 1 comments

See https://github.com/python/typing/pull/1743#discussion_r1640641003

carljm avatar Jun 17 '24 22:06 carljm

Mypy, pyre, and pyright all already allow protocol types to be assigned to object. Test program:

from typing import Protocol

def f(x: object) -> None:
    pass

def g(x: int) -> None:
    pass

class X(Protocol):
    foo: int

def h(x: X) -> None:
    f(x)  # OK: protocol is assignable to object
    g(x)  # E: protocol is not assignable to int
  • pyright: https://pyright-play.net/?strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAK4MYAxmADYBQVAJgKbBTAAUAHgFxRgBGAVvVIwAlFAC0APigA5MCnocqUZVAQBDAM4aaDJmnZdUI8VNnzFK1Zu1VSFa1AAaLImBLkKwiyuBgwhlBgdRigACwMnUUkZOQUlH3ZheOV9NiSgA
  • mypy: https://mypy-play.net/?mypy=latest&python=3.10&gist=4fdb9e9e55ca4dac48d44adf95da3ba6
  • pyre: https://pyre-check.org/play?input=from%20typing%20import%20Protocol%0A%0Adef%20f(x%3A%20object)%20-%3E%20None%3A%0A%20%20%20%20pass%0A%0Adef%20g(x%3A%20int)%20-%3E%20None%3A%0A%20%20%20%20pass%0A%0Aclass%20X(Protocol)%3A%0A%20%20%20%20foo%3A%20int%0A%0Adef%20h(x%3A%20X)%20-%3E%20None%3A%0A%20%20%20%20f(x)%0A%20%20%20%20g(x)%0A

JelleZijlstra avatar Jun 17 '24 22:06 JelleZijlstra