Kinded type behaving differently from non-kinded type in an invariant wrapper
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 32/100
Línea de trabajo
Empieza ejecutando el reproductor proporcionado con Python 3.10.8, returns 0.24.0 y mypy 1.15.0; después inspecciona returns.primitives.hkt y el comportamiento del manejo de listas invariantes de mypy. Se considera terminado cuando las tres llamadas pasan la comprobación de tipos como se muestra y reveal_type informa de list[int] para cada resultado.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Hi Nikita, long time! Thanks for the interesting library bringing Haskell-like FP patterns to Python.
Bug report
What's wrong
I'm trying to extract the type T from a Kind1 type (say, Foo[T]) that is wrapped in an invariant type, e.g., list[Foo[T]]. However, that's leading to some unexpected behavior.
Repro:
from typing import Any, TypeVar
from returns.primitives import hkt
T = TypeVar("T")
class Foo(hkt.SupportsKind1["Foo[Any]", T]):
pass
# These are just helper functions to extract the type `T`.
def expect_list_foo(foo_list: list[Foo[T]]) -> list[T]:
return []
@hkt.kinded
def expect_list_foo__kinded(foo_list: list[hkt.Kind1[Foo[Any], T]]) -> list[T]:
return []
@hkt.kinded
def expect_tuple_foo__kinded(foo_tuple: tuple[hkt.Kind1[Foo[Any], T], str]) -> list[T]:
return []
def inferred_types(foo_list: list[Foo[int]], foo_tuple: tuple[Foo[int], str]) -> None:
x = expect_list_foo(foo_list)
y = expect_list_foo__kinded(foo_list) # <-- Problem
z = expect_tuple_foo__kinded(foo_tuple)
reveal_type(x)
reveal_type(y)
reveal_type(z)
print(x, y, z)
How is that should be
I expected all three calls to work and return list[int].
Actual output:
30: error: Argument 1 to "expect_list_foo__kinded" has incompatible type "list[Foo[int]]"; expected "list[KindN[Foo[Any], int, Any, Any]]" [arg-type]
30: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
30: note: Consider using "Sequence" instead, which is covariant
34: note: Revealed type is "builtins.list[builtins.int]"
35: note: Revealed type is "builtins.list[builtins.int]"
36: note: Revealed type is "builtins.list[builtins.int]"
Found 1 error in 1 file (checked 1 source file)
Seems like list[Foo[int]] is not treated as compatible with list[hkt.Kind1[Foo[Any], T]]. tuple[Foo[int], str] is treated as compatible with tuple[hkt.Kind1[Foo[Any], T], str]. So, it seems like this might be due to the invariance of list.
Is this expected behavior? How can I get this to work?
System information
-
pythonversion: 3.10.8 -
returnsversion: 0.24.0 -
mypyversion: 1.15.0 -
hypothesisversion (if any): -
pytestversion (if any):
- Lenguaje dominante
- Python
- Estrellas
- 4.4k
- Forks
- 154
- Merge medio
- 3 h 5 min
- PR fusionados (30 d)
- 22
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 dry-python/returns
-
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
dry-python/returns#2394 · 1 reacción ·
-
Composing 0-argument functions Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 52/100
dry-python/returns#2365 ·
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 25/100
dry-python/returns#2355 · 1 comentario ·
-
bug
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
dry-python/returns#2295 · 4 comentarios · 1 reacción ·
-
bug
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
dry-python/returns#2253 · 2 comentarios ·
Todos los issues de dry-python/returns
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
anthropics/skills#1811 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
speaches-ai/speaches#678 ·
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
datalayer/mcp-compose#42 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
conda-forge/spacy-feedstock#177 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
UKGovernmentBEIS/inspect_evals#2523 ·