Higher Kinded Types Example Refactored
@thepabloaguilar ya está trabajando en esto.
Desde el 1/4/2022.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
Bug report
The typing inferred via higher-kinded types does not seem to be correct
What's wrong
I've started to play around with the Higher-Kinded Types (I'd really love to be able to type even with type classes) that are provided in the library, following the blog post here (By the way...terrific blog!). One (seemingly basic) thing that I tried to do, was to turn the apply_function into a class method of HasValue, rather than a stand-alone function. However, I've then stumbled into a number of issues.
If one simply puts the function into the class, making it a method, you'd end up with the following conflict:
The erased type of self "returns.primitives.hkt.KindN[_InstanceKind
-1, _ValueType1, Any, Any]" is not a supertype of its class "test_mypy_working.HasValue[_ValueType`1]"
yes, right. The HasValue class does not inherit from KindN. Therefore, I have made HasValue extends the KindSupports, also avoiding to re-inherit this in the child classes Bag and Box .
class HasValue(SupportsKind1[_InstanceKind, _ValueType], Generic[_InstanceKind, _ValueType]):
@abc.abstractmethod
@property
def value(self) -> _ValueType:
"""Returns a value property."""
@abc.abstractmethod
def with_value(
self: _InstanceKind,
new_value: _NewValueType,
) -> Kind1[_InstanceKind, _NewValueType]:
"""Creates a new instance with a changed value."""
@kinded
def apply_function(
self: Kind1[_InstanceKind, _ValueType],
callback: Callable[[_ValueType], _NewValueType],
) -> Kind1[_InstanceKind, _NewValueType]:
new_value = callback(self.value)
return self.with_value(new_value)
@dataclasses.dataclass
class Box(HasValue["Box", _ValueType]):
value: _ValueType
length: int
width: int
height: int
def with_value(self, new_value: _NewValueType) -> "Box[_NewValueType]":
return Box(new_value, self.length, self.width, self.height)
@dataclasses.dataclass
class Bag(HasValue["Bag", _ValueType]):
value: _ValueType
brand: str
model: str
def with_value(self: "Bag", new_value: _NewValueType) -> "Bag[_NewValueType]":
return Bag(new_value, self.brand, self.model)
When doing this, however mypy complains with the following message:
error: Incompatible return value type (got "KindN[_ValueType, _NewValueType, Any, Any]", expected "KindN[_InstanceKind, _NewValueType, Any, Any]")
which I can't quite understand. How does it get the KindN[_ValueType, ...] from the self.with_value method that returns KindN[_InstanceKind, ...]
Despite the error, if in the script I place the following lines, mypy nevertheless INDEED DOES the correct inference
box = Box(value=10, length=1, width=2, height=3)
reveal_type(box.apply_function(str))
Revealed type is "hkt.Box[builtins.str]"
How is that should be
I suppose the error should not appear, unless I'm missing some points here. I'm still new to these features of this library and I'm still wrapping my head around it
Love dry-python? ❤️
Do you want your bugs to be solved faster?
Please consider supporting us:
👉 https://github.com/sponsors/dry-python
-->
System information
-
pythonversion: 3.7.6 -
returnsversion: 0.19.0 -
mypyversion: 0.941 -
mypy-extensionsversion: 0.4.3 -
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
-
bug confirmed issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
open-webui/open-webui#30750 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
-
good first issue
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100