python / python/mypy

Default value of keyword parameter in custom Protocol is not used

Open
#10,484 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Consider the code below:

from typing import Protocol


class P(Protocol):
    def foo(self, n: int = 666) -> None: ...


class F:
    def foo(self, n: int = 666) -> None: ...


class S:
    def foo(self, n: int = 999) -> None: ...


def bar(x: P) -> None: ...


bar(F())
bar(S())

Expected Behavior

Warning in the last code line about incompatibility of class S and protocol P, because the default value of parameter n in P.foo is not ellipsis and the numerical value of n in S.foo is different.

Actual Behavior

Mypy does not generate any warnings.

Your Environment

  • Mypy version used:

8.12
0.820+dev.8642b351cff94c471333256f9cd6867807384f01

  • Mypy command-line flags:

none

  • Mypy configuration options from mypy.ini (and other config files):
[mypy]

pretty = True
show_error_codes = True
strict = True
disallow_any_unimported = True
disallow_any_expr = True
disallow_any_decorated = True
disallow_any_explicit = True
no_warn_no_return = True
warn_unreachable = True
  • Python version used:

3.8.10 (64-bit), 3.9.5 (64-bit), 3.10 beta (64-bit)

  • Operating system and version:

Windows 10 64-bit

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 mypy on the reproducer in the issue and trace the protocol compatibility check for the default value of foo's n parameter. Done means mypy reports the incompatibility between S and P while continuing to accept F as compatible.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.