python / python/mypy

Require argument names to match in Protocol

Open
#18,680 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature needs discussion
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Protocol and MyObject reproducer in the issue and run it through mypy to confirm the current behavior. Trace protocol implementation compatibility checks, then add coverage for mismatched argument names and keyword calls; done means mypy rejects the incompatible implementation while preserving valid Protocol implementations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.