Skip to content
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

Keyword-only protocols are not checked #1295

Open
mrahtz opened this issue Sep 29, 2022 · 3 comments
Open

Keyword-only protocols are not checked #1295

mrahtz opened this issue Sep 29, 2022 · 3 comments
Labels
bug cat: callables functions and decorators

Comments

@mrahtz
Copy link

mrahtz commented Sep 29, 2022

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!

@rchen152 rchen152 added bug cat: callables functions and decorators labels Sep 29, 2022
@rchen152 rchen152 self-assigned this Sep 30, 2022
@rchen152
Copy link
Contributor

This is happening because we convert the __call__ method to a Callable type for matching. But if the method contains any elements (like kwonly parameters) that can't be represented with a Callable, we just lose that information. We already have some better method-matching code that we're using for signature compatibility checks; let me see if I can reuse that here instead of the Callable approach.

@rchen152
Copy link
Contributor

This is a little more work than I hoped it would be =/ The signature compatibility checks mostly work as a drop-in replacement, but they don't handle TypeVar substitutions, which are the trickiest part of matching. I'm going to unassign myself for now.

@rchen152 rchen152 removed their assignment Nov 18, 2022
@mrahtz
Copy link
Author

mrahtz commented Nov 19, 2022

Fair enough. Thanks for looking anyway Rebecca!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cat: callables functions and decorators
Projects
None yet
Development

No branches or pull requests

2 participants