Constraints on generic class with multiple TypeVar's
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 25/100
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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.
- Linguagem predominante
- Python
- Estrelas
- 20.6k
- Forks
- 3.3k
- Merge médio
- 1d 18h
- PRs com merge (30d)
- 54
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de python/mypy
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
-
documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
-
bug topic-configuration topic-error-reporting
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
Todas as issues de python/mypy
Issues semelhantes
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 90/100
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 86/100
zostera/django-bootstrap4#894 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
use-agent-os/agent-os#3276 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
NousResearch/hermes-agent#117848 ·