mypy icon indicating copy to clipboard operation
mypy copied to clipboard

False Negative when Enforcing Positional Arguments in regards to Protocols

Open max-muoto opened this issue 1 year ago • 0 comments

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

max-muoto avatar Jun 22 '24 19:06 max-muoto