python / python/mypy

False positive on manual overloaded `Foo[T].__init__(self, arg)` call

Open
#20,557 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

https://mypy-play.net/?mypy=latest&python=3.12&gist=8fb7e4ecda19d973ed94ee4487bc3ff9

from collections.abc import Sequence, Iterable
from typing import Never, overload

class Foo: ...
class Bar(Foo): ...

class FooSequence[F: Foo](Foo, Sequence[F]):
    @overload
    def __init__(self: "FooSequence[Never]", /) -> None: ...
    @overload
    def __init__(self, modules: Iterable[F], /) -> None: ...
    def __init__(self, modules: Iterable[F] = (), /) -> None: ...
    

class BarSequence[B: Bar](Bar, FooSequence[B]):
    def __init__(self, modules: Iterable[B] = (), /) -> None:
        FooSequence[B].__init__(self, modules)  # ❌️
main.py:17: error: Argument 1 to "__init__" of "FooSequence" has incompatible type "BarSequence[B]"; expected "FooSequence[Never]"  [arg-type]
main.py:17: error: Argument 2 to "__init__" of "FooSequence" has incompatible type "Iterable[B]"; expected "Iterable[Never]"  [arg-type]

This looks like a false positive to me; if the overloads are removed, it type checks without error: https://mypy-play.net/?mypy=latest&python=3.12&gist=ac42aa23c117ba5e2f2ea815a23cb134

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 reproducing the error in main.py using the linked mypy-play example, then trace overload resolution for the explicit FooSequence[B].init(self, modules) call. Done means mypy accepts this call while retaining the overload behavior shown in the example.

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
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.