type: ignore for “incompatible with supertype” error in subclass should suffice for subsubclasses too
还没有人认领这个 Issue。
评估
调研方向
在链接的 mypy playground 中重现子类检查行为,然后将其与 stubs/Markdown/markdown/inlinepatterns.pyi 中的 typeshed 存根以及 upstream 的 markdown/treeprocessors.py 实现进行比较。当对 InlineProcessor.handleMatch 使用 type: ignore 也能阻止兼容子类重写产生不必要的 incompatible-with-supertype 错误,同时原始的 B.foo 诊断仍被抑制时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
class A:
def foo(self, s: str) -> str: ...
class B(A):
def foo(self, n: float) -> float: ... # type: ignore
class C(B):
def foo(self, n: float) -> float: ...
Mypy is correct to report an error for B.foo that necessitated a type: ignore comment. However, having added that type: ignore for B.foo, mypy should not also report an error for C.foo. Although C.foo is inconsistent with A.foo, it’s consistent with B.foo, and I have already asked mypy to ignore the inconsistency between B.foo and A.foo.
To put this another way, I recognize that mypy would fail to catch the runtime error in a: A = C(); a.foo("str"), but this would still be a runtime error if C.foo did not override B.foo, so the blame should be assigned solely to B.foo and not to C.foo.
This comes up in the typeshed stubs for markdown.inlinepatterns:
class Pattern:
def handleMatch(self, m: Match) -> Optional[Union[str, Element]]: ...
class InlineProcessor(Pattern):
def handleMatch(self, m: Match, data) -> Union[Tuple[Element, int, int], Tuple[None, None, None]]: ... # type: ignore
These stubs correctly reflect the upstream API which uses handleMatch with a different signature in subclasses of InlineProcessor than in other subclasses of Pattern. That wasn’t a great API design decision, but now that it’s made and the type: ignore was added to InlineProcessor.handleMatch, ideally I shouldn’t have to repeat the same type: ignore in order to write a custom subclass of InlineProcessor whose handleMatch satisfies the signature of InlineProcessor.handleMatch.
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/mypy 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
-
documentation
难度 2/5 1-3 小时 新手友好度 72/100
-
bug topic-configuration topic-error-reporting
难度 2/5 1-3 小时 新手友好度 68/100
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
zostera/django-bootstrap4#894 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
use-agent-os/agent-os#3276 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
zilliztech/memsearch#759 ·