python / python/mypy

False Negative when Enforcing Positional Arguments in regards to Protocols

Open
#17,421 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

MyPy doesn't raise a warning if you pass in an class implementation that enforces positional arguments on a method, for a protocol that does not.

To Reproduce

Example of a false negative. Executing this code leads to a runtime error, as ActualReadable.read will raise an error when trying to pass in a key-word argument.

from typing import Protocol


class Readable(Protocol):
    def read(self, n: int = -1) -> bytes: ...

class ActualReadable:
    def read(self, n: int = -1, /) -> bytes:
        return b""


def foo(readable: Readable) -> None:
    readable.read(n=1)


foo(ActualReadable())

MyPy Playground

Expected Behavior

Pyright's behavior would be good to model off of in terms of expected behavior. That is, raising a warning that the implementation isn't compatible with the protocol, and that n must not be positional-only.

Pyright Playground

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: Strict
  • Python version used: 3.12

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 supplied Python reproducer under mypy 1.10.0 and strict mode, then investigate protocol implementation compatibility checks for positional-only parameters. Compare the result with the linked Pyright behavior. Done means mypy reports that the implementation is incompatible with the protocol and identifies the positional-only parameter issue.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.