python / python/mypy

First overload picked overeagerly when overloading on self type

Open
#14,070 1 comment 1 reaction 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

Consider this example:

from typing import Generic, TypeVar, Any, overload

AnyStr = TypeVar("AnyStr", str, bytes)

class Popen(Generic[AnyStr]):
    @overload
    def communicate(self: Popen[str], buf: str = ...) -> float: ...
    @overload
    def communicate(self: Popen[bytes], buf: memoryview | bytes = ...) -> range: ...
    def communicate(self, buf: object = None) -> Any: raise NotImplementedError

p = Popen()
reveal_type(p.communicate())

This currently reveals float. I think it should either be the union float | range or Any. This came up in python/typeshed#9100.

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 supplied Generic[Popen] example and reproduce the revealed type for communicate(). Trace overload resolution for methods overloaded on the self type, then compare the result with the expected float | range or Any behavior. Done means the example no longer reveals only float and regression coverage protects the corrected behavior.

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.