Pyright: Type error with protocol method w/ both fewer and named params

Created on 18 Dec 2020  路  1Comment  路  Source: microsoft/pyright

Describe the bug

Pyright doesn't consider a protocol method that uses keyword parameters
to be compatible with a passed in type that uses positional parameters.

To Reproduce

from typing import Optional

from typing_extensions import Protocol


class CollectionProtocol(Protocol):
    def watch(
        self,
        *,
        max_time: Optional[int] = ...,
    ) -> None:
        ...


class Collection:
    def watch(self, key: Optional[str] = None, max_time: Optional[int] = None) -> None:
        ...


def watch_collection(col: CollectionProtocol) -> None:
    col.watch(max_time=100)


watch_collection(Collection())

Produces the following error:

  24:18 - error: Argument of type "Collection" cannot be assigned to parameter "col" of type "CollectionProtocol" in function "watch_collection"
  聽聽"watch" is an incompatible type
  聽聽聽聽Type "(self: Collection, key: str | None = None, max_time: int | None = None) -> None" cannot be assigned to type "(self: CollectionProtocol, *, max_time: int | None = ...) -> None"
  聽聽聽聽聽聽Named parameter "max_time" is missing in source (reportGeneralTypeIssues)
1 error, 0 warnings, 0 infos 

Expected behavior

I would expect this to pass the type checker since the protocol uses keyword parameters and calling the positional arguments as keyword arguments works at runtime.

VS Code extension or command-line

pyright 1.1.95

addressed in next version bug

Most helpful comment

This is now addressed in Pyright 1.1.96, which I just published. It will also be in the next published release of Pylance.

>All comments

This is now addressed in Pyright 1.1.96, which I just published. It will also be in the next published release of Pylance.

Was this page helpful?
0 / 5 - 0 ratings