reachable code reported as unreachable using __init__ vs Protocol vs decorators
还没有人认领这个 Issue。
评估
调研方向
从 pp.py 的复现开始,并使用所示的 Protocol、装饰器和 wrapped 检查运行 mypy pp.py --warn-unreachable。将运行时输出与 mypy 关于不可达语句的诊断进行比较;完成的标准是,已到达的 Protocol 分支不再被报告为不可达。
由索引模型根据 Issue 内容生成。
描述
Please provide more information to help us understand the issue:
- Are you reporting a bug, or opening a feature request?
bug
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
This code decorates init with typeguard, which gives the decorated init a wrapped attribute referring to the original init. This code attempts to unwrap that to get to the original init definition (in my real world use case, to inspect the argument definitions for constructing an instance of a class). At runtime, a Protocol can be used to detect if init has a __wrapped__ attribute, but this does not match up with what mypy believes.
from typing import cast, Any
from typing_extensions import Protocol, runtime_checkable
import typeguard
class Foo():
@typeguard.typechecked
def __init__(self, x: int, y: int,z: int = 3):
self.x = x
self.y = y
self.z = z
print("init")
f = Foo(9,10)
print(f)
func = type(f).__init__
print("__init__ func is {}".format(func))
if hasattr(func, '__wrapped__'):
print("Path 1: is wrapped according to hasattr")
print(cast(Any, type(f).__init__).__wrapped__)
else:
print("Path 1: is not wrapped")
@runtime_checkable
class W(Protocol):
__wrapped__: object
if isinstance(func, W):
print("Path 2: is wrapped according to Protocol - mypy says this is unreachable")
print(type(f).__init__.__wrapped__)
else:
print("Path 2: is not wrapped")
- What is the actual behavior/output?
Execution prints my code's message:Path 2: is wrapped according to Protocol - mypy says this is unreachablefrom line 32.
mypy reports:pp.py:32: error: Statement is unreachablefor that print statement.
Mypy is reporting a statement as unreachable which is actually reached.
-
What is the behavior/output you expect?
Mypy should not report that line as unreachable. -
What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
I have tried mypy 0.770 and git master:
$ mypy --version
mypy 0.770+dev.52c0a63f4604a2bfa8c0edd6589ab25fab4bdaec
- What are the mypy flags you are using? (For example --strict-optional)
$ mypy pp.py --warn-unreachable
with no mypy.ini
- 主要语言
- 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
-
难度 2/5 1-3 小时 新手友好度 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
难度 2/5 1-3 小时 新手友好度 88/100
OpenHands/extensions#626 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
CSCfi/sd-search-api#39 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
-
难度 2/5 1-3 小时 新手友好度 68/100
StevenBlack/hosts#3255 ·