Skip to content

Require argument names to match in Protocol #18680

Open
@sid-kap

Description

@sid-kap

Feature

Currently mypy does not check that argument names in functions that implement protocols match the argument names in the protocol. This fails when arguments are passed as keywords.

For example, this passes mypy:

from typing import Protocol

class MyProtocol(Protocol):
    def foo(self, a: int) -> None: ...

class MyObject:
    def foo(self, b: int) -> None:
        pass

def call_foo(obj: MyProtocol) -> None:
    obj.foo(a=1)

call_foo(MyObject())

but fails at runtime:

Traceback (most recent call last):
  File "/Users/sidharthkapur/protocol_test.py", line 13, in <module>
    call_foo(MyObject())
  File "/Users/sidharthkapur/protocol_test.py", line 11, in call_foo
    obj.foo(a=1)
TypeError: MyObject.foo() got an unexpected keyword argument 'a'

Pitch

If we checked this in mypy, we could prevent runtime errors like the one above.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions