Skip to content

Keyword-only protocols are not checked #1295

Open
@mrahtz

Description

Hi all!

Here's an example of a simple Protocol being used to detect that we're passing a function with the wrong arguments:

from typing import Protocol

class FuncTemplate(Protocol):
  def __call__(self, x: str): pass

def run_func(func: FuncTemplate):
  return func(x='1.0')

def func():
  pass

run_func(func)

As expected, this gives an error:

File "foo.py", line 12, in <module>: Function run_func was called with the wrong arguments [wrong-arg-types]
         Expected: (func: FuncTemplate)
  Actually passed: (func: Callable[[], Any])

But if we make the Protocol kwarg-only:

class FuncTemplate(Protocol):
  def __call__(self, *, x: str): pass

Then the Protocol seems to be ignored:

Success: no errors found

It also seems to be ignored when there's a positional arg in additional to a kwarg-only arg:

class FuncTemplate(Protocol):
  def __call__(self, y: int, *, x: str): pass

def run_func(func: FuncTemplate):
  return func(y=1, x='1.0')

Any idea what might be going on here? Is this a bug, or is there some subtlety about how kwarg-only functions work?

Thanks!

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions