Skip to content

Incorrect checking of protocol methods with **kwargs but no *args #465

@comex

Description

@comex

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.3.0

Python version

3.12

What happened?

The below example should pass checking, but instead gives:

Traceback (most recent call last):
  File "/Users/comex/src/typeguard/test.py", line 12, in <module>
    check_type(C(), P)
  File "/Users/comex/src/typeguard/src/typeguard/_functions.py", line 106, in check_type
    check_type_internal(value, expected_type, memo)
  File "/Users/comex/src/typeguard/src/typeguard/_checkers.py", line 861, in check_type_internal
    checker(value, origin_type, args, memo)
  File "/Users/comex/src/typeguard/src/typeguard/_checkers.py", line 729, in check_protocol
    raise TypeCheckError(
typeguard.TypeCheckError: __main__.C is not compatible with the P protocol because its 'foo' method has too few arguments in its declaration; expected 2 but 1 argument(s) declared

How can we reproduce the bug?

from typeguard import check_type
from typing import Protocol

class P(Protocol):
    def foo(self, **kwargs):
        ...

class C:
    def foo(self, **kwargs):
        pass

check_type(C(), P)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions