Skip to content

Reject extra mandatory keyword arguments in protocol methods - #597

Closed
Eric3-jp wants to merge 2 commits into
agronholm:masterfrom
Eric3-jp:fix-protocol-required-keyword-arguments
Closed

Eric3-jp wants to merge 2 commits into
agronholm:masterfrom
Eric3-jp:fix-protocol-required-keyword-arguments

Conversation

@Eric3-jp

Copy link
Copy Markdown

Changes

Protocol method checks currently accept an implementation with an extra required
keyword-only parameter whenever the protocol method accepts **kwargs. For example,
meth(self, **kwargs) permits meth(), but an implementation declared as
meth(self, *, x, **kwargs) cannot handle that call. check_type() nevertheless
accepts both the instance and its class against the corresponding protocol.

Run the existing extra mandatory keyword-only parameter check even when the protocol
accepts arbitrary keywords. This preserves acceptance of extra parameters with defaults
and required parameters explicitly present in both signatures.

The regression tests cover typing.Protocol and typing_extensions.Protocol, checking
both instances and classes. Before the fix, the four rejection cases fail; all eight
compatibility controls already pass. The incompatibility was also checked against an
actual zero-argument call and mypy 2.1.0.

Validation

  • Python 3.12.3: coverage run -m pytest -q — 547 passed, 9 skipped, 9 xfailed.
  • Pinned Ruff 0.15.20: ruff check . and ruff format --check . pass.
  • The repository's pre-commit hooks pass on all three changed files, including the
    configured mypy hook (which uses --ignore-missing-imports).
  • Configured mypy 2.1.0 reports one existing error in _decorators.py:22 for the
    unresolved typeshed.stdlib.types import. The same error occurs on an unmodified
    checkout of base commit 123f1abc7e82bbe980c779005de858533e1786c3.

Checklist

  • Added regression tests that fail without the patch.
  • Added an entry to docs/versionhistory.rst.
  • No user-guide changes are needed: this fixes the existing protocol signature check.

AI assistance disclosure: OpenAI Codex identified the issue through code inspection,
implemented the change, and ran the checks above. This is not a report of a production
incident experienced by the submitter.

@coveralls

coveralls commented Sep 22, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 94.85% (-0.003%) from 94.853% — Eric3-jp:fix-protocol-required-keyword-arguments into agronholm:master

@Eric3-jp Eric3-jp closed this by deleting the head repository Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants