Constraints on generic class with multiple TypeVar's
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 25/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Merge medio
- 1 d 18 h
- PR fusionados (30 d)
- 54
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python/mypy
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
-
documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
bug topic-configuration topic-error-reporting
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Todos los issues de python/mypy
Issues similares
-
area/auth bug comp/agent P3 platform/discord type/security
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
NousResearch/hermes-agent#117848 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
bancolombia/sentinel#23 ·
-
test md AbiertoCI
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100