Skip to content

Static protocols' (type[IFoo[T]]]) type parameters (T) are unknown #11171

@Peilonrayz

Description

@Peilonrayz

Basically type[IFoo[T]] causes T to become Unknown, where IFoo[T] doesn't. The problem is I'm passing a type not an instance so any issubclass checks then are invalid.

Pyright Playground link.

from typing import Protocol


class IFoo[T](Protocol):
    @classmethod
    def my_method(cls, value: T) -> None: ...


class Foo[T](IFoo[T]):
    @classmethod
    def my_method(cls, value: T) -> None: ...


class TestInstance[T]:
    def __init__(self, interface: IFoo[T]) -> None: ...


class TestClass[T]:
    def __init__(self, interface: type[IFoo[T]]) -> None: ...


f0 = TestInstance(Foo)
f1 = TestClass(Foo)  #  Type of "f1" is "TestClass[Unknown]"  (reportUnknownVariableType)

To get the issubclass checks to work I then have to immediately cast to the correct type: interface = cast(type[IFoo[T]], interface).

Oddly when using @overload with multiple static interfaces type matches correctly. Just the type parameters aren't set correctly. Using IFoo rather than type[IFoo] causes wrong matches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions