python / python/typeshed

singledispatch and TypeVar return type

Open
#5,560 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

It seems that singledispatch typing is not very accurate. In particular, it does not respect TypeVar used in the return type:


T = TypeVar("T")

@singledispatch
def my_fun(
    val: T,
) -> List[T]:
    return list(val)

my_fun(5)
my_fun("Ĝis la revido!")

Here the type of the returned value would always be revealed as List[T] instead of different types for different inputs.

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 example with Python's singledispatch typing and inspect the relevant typeshed definitions. Determine how the TypeVar in the return type is handled; done means calls with different argument types reveal correspondingly specialized List return types.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.