should report ambiguous type variables in self types
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the supplied Python example with mypy, focusing on the merge_method self type and its type-variable diagnostics. Trace the type-checking path that produces the shown Callable error; done means the example reports the ambiguity clearly and no longer suggests Tuple[int, int] as the expected callback type.
Written by the indexing model from the issue text.
Description
Example code:
from typing import TypeVar , Tuple, Generic, Iterable, Callable
T = TypeVar('T', covariant=True)
S = TypeVar('S')
class _Base(Generic[T]):
@classmethod
def split(self, left, right):
# type: (_Base[S], _Base[T]) -> _Base[Tuple[S, T]]
return Split(left, right)
@classmethod
def merge_classmethod(cls, base, f):
# type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> Merge[T]
return Merge(base, f)
# the type should use "S", not "T".
def merge_method(self, f):
# type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> Merge[T]
return Merge(self, f)
class Split(_Base[Tuple[S, T]]):
def __init__(self, left, right):
# type: (_Base[S], _Base[T]) -> None
pass
class Merge(_Base[T]):
def __init__(self, base, f):
# type: (_Base[Iterable[T]], Callable[[Iterable[T]], T]) -> None
pass
def _sum(xs):
# type: (Iterable[int]) -> int
return sum(xs)
x = _Base() # type: _Base[int]
# this typechecks
_Base.merge_classmethod(_Base.split(x, x), _sum)
# this doesn't:
# Argument 1 to "merge_method" of "_Base" has incompatible type "Callable[[Iterable[int]], int]"; expected "Callable[[Iterable[Tuple[int, int]]], Tuple[int, int]]"
_Base.split(x, x).merge_method(_sum)
the merge_method type annotation should be using a variable other than T—a more informative error message should go here.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 54
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/mypy
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug topic-configuration topic-error-reporting
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100