python / python/mypy

Mypy signals error with Protocol and __rpow__ method with three arguments

Open
#10,786 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug good-first-issue topic-protocols
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Okay, this is a head-scratcher. 😕

To Reproduce

# test_case.py
from typing import Protocol, overload, runtime_checkable
@runtime_checkable
class PowableProto(Protocol):
    @overload
    def __pow__(self, other: "PowableProto") -> "PowableProto":
        ...
    @overload
    def __pow__(self, other: "PowableProto", modulo: "PowableProto") -> "PowableProto":  # <- fine
        ...
    @overload
    def __rpow__(self, other: "PowableProto") -> "PowableProto":
        ...
    @overload
    def __rpow__(self, other: "PowableProto", modulo: "PowableProto") -> "PowableProto":  # <- this (of all things) breaks
        ...
    @overload
    def __rpOw__(self, other: "PowableProto") -> "PowableProto":
        ...
    @overload
    def __rpOw__(self, other: "PowableProto", modulo: "PowableProto") -> "PowableProto":  # <- fine
        ...
% mypy --version
mypy 0.910
% mypy --config-file=/dev/null test_case.py
/dev/null: No [mypy] section in config file
test_case.py:15: error: Invalid signature "def (test_case.PowableProto, test_case.PowableProto, test_case.PowableProto) -> test_case.PowableProto"
Found 1 error in 1 file (checked 1 source file)

Expected vs. Actual Behavior

I would expect at least consistent behavior among the overridden methods. This only crops up with __rpow__ for some reason. (I have tested many others.)

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: --config-file=/dev/null (i.e., force default configuration)
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.9.6 (from python --version)
  • Operating system and version: Darwin Kernel Version 19.6.0: Thu May 6 00:48:39 PDT 2021; root:xnu-6153.141.33~1/RELEASE_X86_64 (from uname -v)

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 by running the supplied mypy command against test_case.py and compare how the three-argument pow and rpow overloads are handled. Trace the signature validation that produces the error, then make the behavior consistent and verify the reproducer no longer reports an invalid signature.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.