python / python/mypy

Unexpected incompatible types in assignment (Failed in recursive type checking, nested protocol with generic type)

Open
#16,612 9 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
Here I write some nested protocol with generic type.

The basic protocol classes with generic type succeed to match the classes written in the pyi file, like this:

t: Tempo[int] = core.TempoTick(0, 0) #  pass
t: Temop = core.TemoTick(0,0)        #  pass

However, for upper layer class, list List[Tempo], it just started to fail (with generic), like this:

Note that List is a self defined protocol here for classes generated by pybind11 bind_vector

tempos2: List[Tempo] = core.TempoTickList()     #  not pass
tempos3: List = core.TempoTickList()            #  pass

图片

The full errors are listed here:

Incompatible types in assignment (expression has type "TempoTickList", variable has type "symusic.types.List[Tempo[Any]]")Mypy[assignment](https://mypy.readthedocs.io/en/latest/_refs.html#code-assignment)
Following member(s) of "TempoTickList" have conflicts:
    Expected:
        @overload
        def __setitem__(self, int, Tempo[Any], /) -> None
        @overload
        def __setitem__(self, slice, TempoTickList, /) -> None
    Got:
        @overload
        def __setitem__(self, int, TempoTick, /) -> None
        @overload
        def __setitem__(self, slice, TempoTickList, /) -> None
    Expected:
        def append(self, x: Tempo[Any]) -> None
    Got:
        def append(self, x: TempoTick) -> None
    <1 more conflict(s) not shown>Mypy

Although Tempo and Tempo[int] have matched TempoTick, they are said failed to match in the errors.

Even if I remove some of the nested generic type like below, it still failed:

class Score(Protocol[TimeDtype]):
    ticks_per_quarter: int
    tracks: List
    key_signatures: List
    time_signatures: List
    tempos: List
    lyrics: List
    markers: List

图片

By the way, in pycharm, it says, all of these classes fail to match, quite different from mypy. And different from mypy, pycharm don't offer me any useful debug information. (But pycharm works as expected in my following test while mypy failed. So I guess there are at least 2 problems in total.)

图片

To Reproduce
I put my codes here (just a few files):
symusic.zip

And the code in picture is at the bottom of the types.py. I just use the vscode MyPy Type Checker plugin.

Expected Behavior

Well, I just expect it to match.

Actual Behavior

They don't match when the protocol with generic type is nested

Your Environment

  • Mypy version used: mypy 1.7.1 (compiled: yes)
  • Mypy command-line flags: default in vscode
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.11.6

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 attached symusic.zip reproduction, especially the code at the bottom of types.py, and run it with mypy 1.7.1 using the default configuration. Compare the nested generic protocol assignments with the reported errors; done means the TempoTickList and related protocol assignments are accepted consistently with the simpler cases.

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.