Constraints on generic class with multiple TypeVar's
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
The issue provides a Python reproducer but names no source file or test. Start by running the example with mypy and locating the TypeVar, Generic, and overload-checking tests. Done means establishing whether paired constraints can be expressed and, if supported, adding coverage for the allowed and rejected combinations.
Written by the indexing model from the issue text.
Description
Is there a way to limit the possible combinations of TypeVar's instantiations for Generics with multiple TypeVar's?
Let's look at the following example:
import sys
from typing import TypeVar, Generic, overload
assert sys.version_info >= (3, 5)
class Foo(object):
def __init__(self, dummy):
self.dummy = dummy
class Bar(object):
def __init__(self, dummy):
self.dummy = dummy
class Apple(object):
def __init__(self, dummy):
self.dummy = dummy
class Pear(object):
def __init__(self, dummy):
self.dummy = dummy
Var1 = TypeVar('Var1', Foo, Apple)
Var2 = TypeVar('Var2', Bar, Pear)
@overload
def combine(left: Foo, right: Bar) -> None:
...
@overload
def combine(left: Apple, right: Pear) -> None:
...
def combine(left, right):
print(left.dummy + " and " + right.dummy)
class MyClass(Generic[Var1, Var2]):
def __init__(self, a: Var1) -> None:
self.dummy = dummy # type: Var1
def add(self, other: Var2) -> None:
combine(self.dummy, other)
Here, mypy produces this error output:
mypy_example.py:50: error: No overload variant of "combine" matches argument types [mypy_example.Foo*, mypy_example.Pear*]
mypy_example.py:50: error: No overload variant of "combine" matches argument types [mypy_example.Apple*, mypy_example.Bar*]
Is there any possibility to restrict MyClass to MyClass[Apple, Pear] and MyClass[Foo, Bar] but disallow MyClass[Apple, Bar] and MyClass[Foo, Pear]? This is for python 3.5.
- 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