Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Higher Kinded Types Example Refactored

未关闭
#1,343 0 条评论 2 个 reaction 已指派 2 人 在 GitHub 查看

@thepabloaguilar 已经在做这个了。

开始于 2022年4月1日。

评估

这个 Issue 还没有评估数据。

描述

bug documentation

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

  • python version: 3.7.6

  • returns version: 0.19.0

  • mypy version: 0.941

  • mypy-extensions version: 0.4.3

  • hypothesis version (if any):

  • pytest version (if any):

主要语言
Python
星标
4.4k
派生
154
平均合并
3 小时 5 分钟
30 天内合并 PR
22

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dry-python/returns 的其他 Issue

查看 dry-python/returns 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。